1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
46  public class StringServletResponse extends HttpServletResponseWrapper {
47  
48      public StringServletResponse(HttpServletResponse response) {
49          super(response);
50      }
51  
52      public int getBufferSize() {
53          return _bufferSize;
54      }
55  
56      public ByteArrayMaker getByteArrayMaker() {
57          return _bam;
58      }
59  
60      public String getContentType() {
61          return _contentType;
62      }
63  
64      public ServletOutputStream getOutputStream() {
65          /*if (_callGetWriter) {
66              throw new IllegalStateException();
67          }*/
68  
69          _callGetOutputStream = true;
70  
71          return _sos;
72      }
73  
74      public int getStatus() {
75          return _status;
76      }
77  
78      public String getString() {
79          if (_string != null) {
80              return _string;
81          }
82          else if (_callGetOutputStream) {
83              try {
84                  return _bam.toString(StringPool.UTF8);
85              }
86              catch (UnsupportedEncodingException uee) {
87                  _log.error(uee, uee);
88  
89                  return StringPool.BLANK;
90              }
91          }
92          else if (_callGetWriter) {
93              return _sw.toString();
94          }
95          else {
96              return StringPool.BLANK;
97          }
98      }
99  
100     public PrintWriter getWriter() {
101         /*if (_callGetOutputStream) {
102             throw new IllegalStateException();
103         }*/
104 
105         _callGetWriter = true;
106 
107         return _pw;
108     }
109 
110     public boolean isCalledGetOutputStream() {
111         return _callGetOutputStream;
112     }
113 
114     public void recycle() {
115         _bam.reset();
116         //_sos = new StringServletOutputStream(_bam);
117         _status = SC_OK;
118         _sw = new StringWriter();
119         _pw = new PrintWriter(_sw);
120         _callGetOutputStream = false;
121         _callGetWriter = false;
122         _string = null;
123     }
124 
125     public void resetBuffer() {
126         if (_callGetOutputStream) {
127             _bam.reset();
128         }
129         else if (_callGetWriter) {
130             StringBuffer sb = _sw.getBuffer();
131 
132             sb.delete(0, sb.length());
133         }
134     }
135 
136     public void setBufferSize(int size) {
137         _bufferSize = size;
138     }
139 
140     public void setContentType(String contentType) {
141         _contentType = contentType;
142 
143         super.setContentType(contentType);
144     }
145 
146     public void setLocale(Locale locale) {
147     }
148 
149     public void setStatus(int status) {
150         _status = status;
151     }
152 
153     public void setString(String string) {
154         _string = string;
155     }
156 
157     private static Log _log =
158         LogFactoryUtil.getLog(StringServletResponse.class);
159 
160     private String _contentType;
161     private int _status = SC_OK;
162     private ByteArrayMaker _bam = new ByteArrayMaker();
163     private ServletOutputStream _sos = new StringServletOutputStream(_bam);
164     private StringWriter _sw = new StringWriter();
165     private PrintWriter _pw = new PrintWriter(_sw);
166     private int _bufferSize;
167     private boolean _callGetOutputStream;
168     private boolean _callGetWriter;
169     private String _string = null;
170 
171 }