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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ByteArrayMaker;
28  import com.liferay.portal.kernel.util.StringPool;
29  
30  import java.io.PrintWriter;
31  import java.io.StringWriter;
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  
51      public int getBufferSize() {
52          return _bufferSize;
53      }
54  
55      public ByteArrayMaker getByteArrayMaker() {
56          return _bam;
57      }
58  
59      public String getContentType() {
60          return _contentType;
61      }
62  
63      public ServletOutputStream getOutputStream() {
64          /*if (_callGetWriter) {
65              throw new IllegalStateException();
66          }*/
67  
68          _callGetOutputStream = true;
69  
70          return _sos;
71      }
72  
73      public int getStatus() {
74          return _status;
75      }
76  
77      public String getString() {
78          if (_string != null) {
79              return _string;
80          }
81          else if (_callGetOutputStream) {
82              try {
83                  return _bam.toString(StringPool.UTF8);
84              }
85              catch (UnsupportedEncodingException uee) {
86                  _log.error(uee, uee);
87  
88                  return StringPool.BLANK;
89              }
90          }
91          else if (_callGetWriter) {
92              return _sw.toString();
93          }
94          else {
95              return StringPool.BLANK;
96          }
97      }
98  
99      public PrintWriter getWriter() {
100         /*if (_callGetOutputStream) {
101             throw new IllegalStateException();
102         }*/
103 
104         _callGetWriter = true;
105 
106         return _pw;
107     }
108 
109     public boolean isCalledGetOutputStream() {
110         return _callGetOutputStream;
111     }
112 
113     public void recycle() {
114         _bam.reset();
115         //_sos = new StringServletOutputStream(_bam);
116         _status = SC_OK;
117         _sw = new StringWriter();
118         _pw = new PrintWriter(_sw);
119         _callGetOutputStream = false;
120         _callGetWriter = false;
121         _string = null;
122     }
123 
124     public void resetBuffer() {
125         if (_callGetOutputStream) {
126             _bam.reset();
127         }
128         else if (_callGetWriter) {
129             StringBuffer sb = _sw.getBuffer();
130 
131             sb.delete(0, sb.length());
132         }
133     }
134 
135     public void setBufferSize(int bufferSize) {
136         _bufferSize = bufferSize;
137     }
138 
139     public void setContentType(String contentType) {
140         _contentType = contentType;
141 
142         super.setContentType(contentType);
143     }
144 
145     public void setLocale(Locale locale) {
146     }
147 
148     public void setStatus(int status) {
149         _status = status;
150     }
151 
152     public void setString(String string) {
153         _string = string;
154     }
155 
156     private static Log _log =
157         LogFactoryUtil.getLog(StringServletResponse.class);
158 
159     private String _contentType;
160     private int _status = SC_OK;
161     private ByteArrayMaker _bam = new ByteArrayMaker();
162     private ServletOutputStream _sos = new StringServletOutputStream(_bam);
163     private StringWriter _sw = new StringWriter();
164     private PrintWriter _pw = new PrintWriter(_sw);
165     private int _bufferSize;
166     private boolean _callGetOutputStream;
167     private boolean _callGetWriter;
168     private String _string = null;
169 
170 }