1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet.filters.strip;
21  
22  import com.liferay.portal.kernel.util.ByteArrayMaker;
23  import com.liferay.portal.kernel.util.StringPool;
24  
25  import java.io.IOException;
26  import java.io.OutputStreamWriter;
27  import java.io.PrintWriter;
28  
29  import javax.servlet.ServletOutputStream;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.http.HttpServletResponseWrapper;
32  
33  /**
34   * <a href="StripResponse.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class StripResponse extends HttpServletResponseWrapper {
40  
41      public StripResponse(HttpServletResponse response) {
42          super(response);
43      }
44  
45      public void finishResponse() {
46          try {
47              if (_writer != null) {
48                  _writer.close();
49              }
50              else if (_stream != null) {
51                  _stream.close();
52              }
53          }
54          catch (IOException e) {
55          }
56      }
57  
58      public void flushBuffer() throws IOException {
59          if (_stream != null) {
60              _stream.flush();
61          }
62      }
63  
64      public ServletOutputStream getOutputStream() {
65          if (_writer != null) {
66              throw new IllegalStateException();
67          }
68  
69          if (_stream == null) {
70              _stream = createOutputStream();
71          }
72  
73          return _stream;
74      }
75  
76      public PrintWriter getWriter() throws IOException {
77          if (_writer != null) {
78              return _writer;
79          }
80  
81          if (_stream != null) {
82              throw new IllegalStateException();
83          }
84  
85          _stream = createOutputStream();
86  
87          _writer = new PrintWriter(new OutputStreamWriter(
88              //_stream, _res.getCharacterEncoding()));
89              _stream, StringPool.UTF8));
90  
91          return _writer;
92      }
93  
94      public boolean isCommitted() {
95          if (_stream != null) {
96              return true;
97          }
98          else {
99              return super.isCommitted();
100         }
101     }
102 
103     public String getContentType() {
104         return _contentType;
105     }
106 
107     public void setContentType(String contentType) {
108         _contentType = contentType;
109 
110         super.setContentType(contentType);
111     }
112 
113     public byte[] getData() {
114         finishResponse();
115 
116         return _bam.toByteArray();
117     }
118 
119     protected ServletOutputStream createOutputStream() {
120         return new StripStream(_bam);
121     }
122 
123     private ByteArrayMaker _bam = new ByteArrayMaker();
124     private ServletOutputStream _stream = null;
125     private PrintWriter _writer = null;
126     private String _contentType;
127 
128 }