1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.servlet.filters.strip;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
18  import com.liferay.portal.kernel.servlet.StringServletOutputStream;
19  import com.liferay.portal.kernel.util.StringPool;
20  
21  import java.io.IOException;
22  import java.io.OutputStreamWriter;
23  import java.io.PrintWriter;
24  
25  import javax.servlet.ServletOutputStream;
26  import javax.servlet.http.HttpServletResponse;
27  import javax.servlet.http.HttpServletResponseWrapper;
28  
29  /**
30   * <a href="StripResponse.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class StripResponse extends HttpServletResponseWrapper {
35  
36      public StripResponse(HttpServletResponse response) {
37          super(response);
38      }
39  
40      public void finishResponse() {
41          try {
42              if (_printWriter != null) {
43                  _printWriter.close();
44              }
45              else if (_servletOutputStream != null) {
46                  _servletOutputStream.close();
47              }
48          }
49          catch (IOException ioe) {
50          }
51      }
52  
53      public void flushBuffer() throws IOException {
54          if (_servletOutputStream != null) {
55              _servletOutputStream.flush();
56          }
57      }
58  
59      public String getContentType() {
60          return _contentType;
61      }
62  
63      public byte[] getData() {
64          finishResponse();
65  
66          if (_unsyncByteArrayOutputStream != null) {
67              return _unsyncByteArrayOutputStream.toByteArray();
68          }
69  
70          return null;
71      }
72  
73      public ServletOutputStream getOutputStream() {
74          if (_printWriter != null) {
75              throw new IllegalStateException();
76          }
77  
78          if (_servletOutputStream == null) {
79              _servletOutputStream = createOutputStream();
80          }
81  
82          return _servletOutputStream;
83      }
84  
85      public PrintWriter getWriter() throws IOException {
86          if (_printWriter != null) {
87              return _printWriter;
88          }
89  
90          if (_servletOutputStream != null) {
91              throw new IllegalStateException();
92          }
93  
94          _servletOutputStream = createOutputStream();
95  
96          _printWriter = new PrintWriter(
97              new OutputStreamWriter(_servletOutputStream, StringPool.UTF8));
98  
99          return _printWriter;
100     }
101 
102     public boolean isCommitted() {
103         if (_servletOutputStream != null) {
104             return true;
105         }
106         else {
107             return super.isCommitted();
108         }
109     }
110 
111     public void setContentType(String contentType) {
112         _contentType = contentType;
113 
114         super.setContentType(contentType);
115     }
116 
117     protected ServletOutputStream createOutputStream() {
118         _unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
119 
120         return new StringServletOutputStream(_unsyncByteArrayOutputStream);
121     }
122 
123     private String _contentType;
124     private PrintWriter _printWriter;
125     private ServletOutputStream _servletOutputStream;
126     private UnsyncByteArrayOutputStream _unsyncByteArrayOutputStream;
127 
128 }