1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet.filters.strip;
24  
25  import com.liferay.portal.kernel.util.ByteArrayMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  
28  import java.io.IOException;
29  import java.io.OutputStreamWriter;
30  import java.io.PrintWriter;
31  
32  import javax.servlet.ServletOutputStream;
33  import javax.servlet.http.HttpServletResponse;
34  import javax.servlet.http.HttpServletResponseWrapper;
35  
36  /**
37   * <a href="StripResponse.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class StripResponse extends HttpServletResponseWrapper {
43  
44      public StripResponse(HttpServletResponse response) {
45          super(response);
46      }
47  
48      public void finishResponse() {
49          try {
50              if (_writer != null) {
51                  _writer.close();
52              }
53              else if (_stream != null) {
54                  _stream.close();
55              }
56          }
57          catch (IOException e) {
58          }
59      }
60  
61      public void flushBuffer() throws IOException {
62          if (_stream != null) {
63              _stream.flush();
64          }
65      }
66  
67      public ServletOutputStream getOutputStream() {
68          if (_writer != null) {
69              throw new IllegalStateException();
70          }
71  
72          if (_stream == null) {
73              _stream = createOutputStream();
74          }
75  
76          return _stream;
77      }
78  
79      public PrintWriter getWriter() throws IOException {
80          if (_writer != null) {
81              return _writer;
82          }
83  
84          if (_stream != null) {
85              throw new IllegalStateException();
86          }
87  
88          _stream = createOutputStream();
89  
90          _writer = new PrintWriter(new OutputStreamWriter(
91              //_stream, _res.getCharacterEncoding()));
92              _stream, StringPool.UTF8));
93  
94          return _writer;
95      }
96  
97      public boolean isCommitted() {
98          if (_stream != null) {
99              return true;
100         }
101         else {
102             return super.isCommitted();
103         }
104     }
105 
106     public String getContentType() {
107         return _contentType;
108     }
109 
110     public void setContentType(String contentType) {
111         _contentType = contentType;
112 
113         super.setContentType(contentType);
114     }
115 
116     public byte[] getData() {
117         finishResponse();
118 
119         return _bam.toByteArray();
120     }
121 
122     protected ServletOutputStream createOutputStream() {
123         return new StripStream(_bam);
124     }
125 
126     private ByteArrayMaker _bam = new ByteArrayMaker();
127     private ServletOutputStream _stream = null;
128     private PrintWriter _writer = null;
129     private String _contentType;
130 
131 }