1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.kernel.servlet;
24  
25  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
26  import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  
31  import java.io.PrintWriter;
32  import java.io.UnsupportedEncodingException;
33  
34  import java.util.Locale;
35  
36  import javax.servlet.ServletOutputStream;
37  import javax.servlet.http.HttpServletResponse;
38  import javax.servlet.http.HttpServletResponseWrapper;
39  
40  /**
41   * <a href="StringServletResponse.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class StringServletResponse extends HttpServletResponseWrapper {
46  
47      public StringServletResponse(HttpServletResponse response) {
48          super(response);
49  
50          _unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
51          _servletOutputStream = new StringServletOutputStream(
52              _unsyncByteArrayOutputStream);
53  
54          _unsyncStringWriter = new UnsyncStringWriter(true);
55          _printWriter = new PrintWriter(_unsyncStringWriter);
56      }
57  
58      public int getBufferSize() {
59          return _bufferSize;
60      }
61  
62      public String getContentType() {
63          return _contentType;
64      }
65  
66      public ServletOutputStream getOutputStream() {
67          _callGetOutputStream = true;
68  
69          return _servletOutputStream;
70      }
71  
72      public int getStatus() {
73          return _status;
74      }
75  
76      public String getString() {
77          if (_string != null) {
78              return _string;
79          }
80          else if (_callGetOutputStream) {
81              try {
82                  return _unsyncByteArrayOutputStream.toString(StringPool.UTF8);
83              }
84              catch (UnsupportedEncodingException uee) {
85                  _log.error(uee, uee);
86  
87                  return StringPool.BLANK;
88              }
89          }
90          else if (_callGetWriter) {
91              return _unsyncStringWriter.toString();
92          }
93          else {
94              return StringPool.BLANK;
95          }
96      }
97  
98      public UnsyncByteArrayOutputStream getUnsyncByteArrayOutputStream() {
99          return _unsyncByteArrayOutputStream;
100     }
101 
102     public PrintWriter getWriter() {
103         _callGetWriter = true;
104 
105         return _printWriter;
106     }
107 
108     public boolean isCalledGetOutputStream() {
109         return _callGetOutputStream;
110     }
111 
112     public void recycle() {
113         _callGetOutputStream = false;
114         _callGetWriter = false;
115         _status = SC_OK;
116         _string = null;
117 
118         _unsyncByteArrayOutputStream.reset();
119 
120         _unsyncStringWriter = new UnsyncStringWriter(true);
121         _printWriter = new PrintWriter(_unsyncStringWriter);
122     }
123 
124     public void resetBuffer() {
125         if (_callGetOutputStream) {
126             _unsyncByteArrayOutputStream.reset();
127         }
128         else if (_callGetWriter) {
129             _unsyncStringWriter.reset();
130         }
131     }
132 
133     public void setBufferSize(int bufferSize) {
134         _bufferSize = bufferSize;
135     }
136 
137     public void setContentType(String contentType) {
138         _contentType = contentType;
139 
140         super.setContentType(contentType);
141     }
142 
143     public void setLocale(Locale locale) {
144     }
145 
146     public void setStatus(int status) {
147         _status = status;
148     }
149 
150     public void setString(String string) {
151         _string = string;
152     }
153 
154     private static Log _log =
155         LogFactoryUtil.getLog(StringServletResponse.class);
156 
157     private int _bufferSize;
158     private boolean _callGetOutputStream;
159     private boolean _callGetWriter;
160     private String _contentType;
161     private PrintWriter _printWriter;
162     private ServletOutputStream _servletOutputStream;
163     private int _status = SC_OK;
164     private String _string;
165     private UnsyncByteArrayOutputStream _unsyncByteArrayOutputStream;
166     private UnsyncStringWriter _unsyncStringWriter;
167 
168 }