1   /**
2    * Copyright (c) 2000-2008 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  
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              return _bam.toString();
81          }
82          else if (_callGetWriter) {
83              return _sw.toString();
84          }
85          else {
86              return StringPool.BLANK;
87          }
88      }
89  
90      public PrintWriter getWriter() {
91          /*if (_callGetOutputStream) {
92              throw new IllegalStateException();
93          }*/
94  
95          _callGetWriter = true;
96  
97          return _pw;
98      }
99  
100     public boolean isCalledGetOutputStream() {
101         return _callGetOutputStream;
102     }
103 
104     public void recycle() {
105         _bam.reset();
106         //_sos = new StringServletOutputStream(_bam);
107         _status = SC_OK;
108         _sw = new StringWriter();
109         _pw = new PrintWriter(_sw);
110         _callGetOutputStream = false;
111         _callGetWriter = false;
112         _string = null;
113     }
114 
115     public void resetBuffer() {
116         if (_callGetOutputStream) {
117             _bam.reset();
118         }
119         else if (_callGetWriter) {
120             StringBuffer sb = _sw.getBuffer();
121 
122             sb.delete(0, sb.length());
123         }
124     }
125 
126     public void setBufferSize(int size) {
127         _bufferSize = size;
128     }
129 
130     public void setContentType(String contentType) {
131         _contentType = contentType;
132 
133         super.setContentType(contentType);
134     }
135 
136     public void setLocale(Locale locale) {
137     }
138 
139     public void setStatus(int status) {
140         _status = status;
141     }
142 
143     public void setString(String string) {
144         _string = string;
145     }
146 
147     private String _contentType;
148     private int _status = SC_OK;
149     private ByteArrayMaker _bam = new ByteArrayMaker();
150     private ServletOutputStream _sos = new StringServletOutputStream(_bam);
151     private StringWriter _sw = new StringWriter();
152     private PrintWriter _pw = new PrintWriter(_sw);
153     private int _bufferSize;
154     private boolean _callGetOutputStream;
155     private boolean _callGetWriter;
156     private String _string = null;
157 
158 }