1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.servlet;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
18  import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  
23  import java.io.PrintWriter;
24  import java.io.UnsupportedEncodingException;
25  
26  import java.util.Locale;
27  
28  import javax.servlet.ServletOutputStream;
29  import javax.servlet.http.HttpServletResponse;
30  import javax.servlet.http.HttpServletResponseWrapper;
31  
32  /**
33   * <a href="StringServletResponse.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class StringServletResponse extends HttpServletResponseWrapper {
38  
39      public StringServletResponse(HttpServletResponse response) {
40          super(response);
41  
42          _unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
43          _servletOutputStream = new StringServletOutputStream(
44              _unsyncByteArrayOutputStream);
45  
46          _unsyncStringWriter = new UnsyncStringWriter(true);
47          _printWriter = new PrintWriter(_unsyncStringWriter);
48      }
49  
50      public int getBufferSize() {
51          return _bufferSize;
52      }
53  
54      public String getContentType() {
55          return _contentType;
56      }
57  
58      public ServletOutputStream getOutputStream() {
59          _callGetOutputStream = true;
60  
61          return _servletOutputStream;
62      }
63  
64      public int getStatus() {
65          return _status;
66      }
67  
68      public String getString() {
69          if (_string != null) {
70              return _string;
71          }
72          else if (_callGetOutputStream) {
73              try {
74                  return _unsyncByteArrayOutputStream.toString(StringPool.UTF8);
75              }
76              catch (UnsupportedEncodingException uee) {
77                  _log.error(uee, uee);
78  
79                  return StringPool.BLANK;
80              }
81          }
82          else if (_callGetWriter) {
83              return _unsyncStringWriter.toString();
84          }
85          else {
86              return StringPool.BLANK;
87          }
88      }
89  
90      public UnsyncByteArrayOutputStream getUnsyncByteArrayOutputStream() {
91          return _unsyncByteArrayOutputStream;
92      }
93  
94      public PrintWriter getWriter() {
95          _callGetWriter = true;
96  
97          return _printWriter;
98      }
99  
100     public boolean isCalledGetOutputStream() {
101         return _callGetOutputStream;
102     }
103 
104     public void recycle() {
105         _callGetOutputStream = false;
106         _callGetWriter = false;
107         _status = SC_OK;
108         _string = null;
109 
110         _unsyncByteArrayOutputStream.reset();
111 
112         _unsyncStringWriter = new UnsyncStringWriter(true);
113         _printWriter = new PrintWriter(_unsyncStringWriter);
114     }
115 
116     public void resetBuffer() {
117         if (_callGetOutputStream) {
118             _unsyncByteArrayOutputStream.reset();
119         }
120         else if (_callGetWriter) {
121             _unsyncStringWriter.reset();
122         }
123     }
124 
125     public void setBufferSize(int bufferSize) {
126         _bufferSize = bufferSize;
127     }
128 
129     public void setContentType(String contentType) {
130         _contentType = contentType;
131 
132         super.setContentType(contentType);
133     }
134 
135     public void setLocale(Locale locale) {
136     }
137 
138     public void setStatus(int status) {
139         _status = status;
140     }
141 
142     public void setString(String string) {
143         _string = string;
144     }
145 
146     private static Log _log = LogFactoryUtil.getLog(
147         StringServletResponse.class);
148 
149     private int _bufferSize;
150     private boolean _callGetOutputStream;
151     private boolean _callGetWriter;
152     private String _contentType;
153     private PrintWriter _printWriter;
154     private ServletOutputStream _servletOutputStream;
155     private int _status = SC_OK;
156     private String _string;
157     private UnsyncByteArrayOutputStream _unsyncByteArrayOutputStream;
158     private UnsyncStringWriter _unsyncStringWriter;
159 
160 }