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