1   /**
2    * Copyright (c) 2000-2007 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.util.ByteArrayMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  
28  import java.io.PrintWriter;
29  import java.io.StringWriter;
30  import java.io.UnsupportedEncodingException;
31  
32  import java.util.Locale;
33  
34  import javax.servlet.ServletOutputStream;
35  import javax.servlet.http.HttpServletResponse;
36  import javax.servlet.http.HttpServletResponseWrapper;
37  
38  /**
39   * <a href="StringServletResponse.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class StringServletResponse extends HttpServletResponseWrapper {
45  
46      public StringServletResponse(HttpServletResponse res) {
47          super(res);
48      }
49  
50      public String getContentType() {
51          return _contentType;
52      }
53  
54      public void setContentType(String contentType) {
55          _contentType = contentType;
56  
57          super.setContentType(contentType);
58      }
59  
60      public void setLocale(Locale locale) {
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 void setStatus(int status) {
78          _status = status;
79      }
80  
81      public String getString() throws UnsupportedEncodingException {
82          if (_string != null) {
83              return _string;
84          }
85          else if (_callGetOutputStream) {
86              return _bam.toString();
87          }
88          else if (_callGetWriter) {
89              return _sw.toString();
90          }
91          else {
92              return StringPool.BLANK;
93          }
94      }
95  
96      public void setString(String string) {
97          _string = string;
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 int getBufferSize() {
111         return _bufferSize;
112     }
113 
114     public void setBufferSize(int size) {
115         _bufferSize = size;
116     }
117 
118     public void resetBuffer() {
119         if (_callGetOutputStream) {
120             _bam.reset();
121         }
122         else if (_callGetWriter) {
123             StringBuffer sb = _sw.getBuffer();
124 
125             sb.delete(0, sb.length());
126         }
127     }
128 
129     public void recycle() {
130         _bam.reset();
131         //_sos = new StringServletOutputStream(_bam);
132         _status = SC_OK;
133         _sw = new StringWriter();
134         _pw = new PrintWriter(_sw);
135         _callGetOutputStream = false;
136         _callGetWriter = false;
137         _string = null;
138     }
139 
140     private String _contentType;
141     private ByteArrayMaker _bam = new ByteArrayMaker();
142     private ServletOutputStream _sos = new StringServletOutputStream(_bam);
143     private int _status = SC_OK;
144     private StringWriter _sw = new StringWriter();
145     private PrintWriter _pw = new PrintWriter(_sw);
146     private int _bufferSize;
147     private boolean _callGetOutputStream;
148     private boolean _callGetWriter;
149     private String _string = null;
150 
151 }