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.kernel.servlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.ByteArrayMaker;
25  import com.liferay.portal.kernel.util.StringPool;
26  
27  import java.io.PrintWriter;
28  import java.io.StringWriter;
29  import java.io.UnsupportedEncodingException;
30  
31  import java.util.Locale;
32  
33  import javax.servlet.ServletOutputStream;
34  import javax.servlet.http.HttpServletResponse;
35  import javax.servlet.http.HttpServletResponseWrapper;
36  
37  /**
38   * <a href="StringServletResponse.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class StringServletResponse extends HttpServletResponseWrapper {
44  
45      public StringServletResponse(HttpServletResponse response) {
46          super(response);
47      }
48  
49      public int getBufferSize() {
50          return _bufferSize;
51      }
52  
53      public ByteArrayMaker getByteArrayMaker() {
54          return _bam;
55      }
56  
57      public String getContentType() {
58          return _contentType;
59      }
60  
61      public ServletOutputStream getOutputStream() {
62          /*if (_callGetWriter) {
63              throw new IllegalStateException();
64          }*/
65  
66          _callGetOutputStream = true;
67  
68          return _sos;
69      }
70  
71      public int getStatus() {
72          return _status;
73      }
74  
75      public String getString() {
76          if (_string != null) {
77              return _string;
78          }
79          else if (_callGetOutputStream) {
80              try {
81                  return _bam.toString(StringPool.UTF8);
82              }
83              catch (UnsupportedEncodingException uee) {
84                  _log.error(uee, uee);
85  
86                  return StringPool.BLANK;
87              }
88          }
89          else if (_callGetWriter) {
90              return _sw.toString();
91          }
92          else {
93              return StringPool.BLANK;
94          }
95      }
96  
97      public PrintWriter getWriter() {
98          /*if (_callGetOutputStream) {
99              throw new IllegalStateException();
100         }*/
101 
102         _callGetWriter = true;
103 
104         return _pw;
105     }
106 
107     public boolean isCalledGetOutputStream() {
108         return _callGetOutputStream;
109     }
110 
111     public void recycle() {
112         _bam.reset();
113         //_sos = new StringServletOutputStream(_bam);
114         _status = SC_OK;
115         _sw = new StringWriter();
116         _pw = new PrintWriter(_sw);
117         _callGetOutputStream = false;
118         _callGetWriter = false;
119         _string = null;
120     }
121 
122     public void resetBuffer() {
123         if (_callGetOutputStream) {
124             _bam.reset();
125         }
126         else if (_callGetWriter) {
127             StringBuffer sb = _sw.getBuffer();
128 
129             sb.delete(0, sb.length());
130         }
131     }
132 
133     public void setBufferSize(int size) {
134         _bufferSize = size;
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 String _contentType;
158     private int _status = SC_OK;
159     private ByteArrayMaker _bam = new ByteArrayMaker();
160     private ServletOutputStream _sos = new StringServletOutputStream(_bam);
161     private StringWriter _sw = new StringWriter();
162     private PrintWriter _pw = new PrintWriter(_sw);
163     private int _bufferSize;
164     private boolean _callGetOutputStream;
165     private boolean _callGetWriter;
166     private String _string = null;
167 
168 }