1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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   * @auther Shuyang Zhou
37   */
38  public class StringServletResponse extends HttpServletResponseWrapper {
39  
40      public StringServletResponse(HttpServletResponse response) {
41          super(response);
42      }
43  
44      public int getBufferSize() {
45          return _bufferSize;
46      }
47  
48      public String getContentType() {
49          return _contentType;
50      }
51  
52      public ServletOutputStream getOutputStream() {
53          if (_servletOutputStream == null) {
54              _unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
55              _servletOutputStream = new StringServletOutputStream(
56                  _unsyncByteArrayOutputStream);
57          }
58  
59          return _servletOutputStream;
60      }
61  
62      public int getStatus() {
63          return _status;
64      }
65  
66      public String getString() {
67          if (_string != null) {
68              return _string;
69          }
70          else if (_servletOutputStream != null) {
71              try {
72                  return _unsyncByteArrayOutputStream.toString(StringPool.UTF8);
73              }
74              catch (UnsupportedEncodingException uee) {
75                  _log.error(uee, uee);
76  
77                  return StringPool.BLANK;
78              }
79          }
80          else if (_printWriter != null) {
81              return _unsyncStringWriter.toString();
82          }
83          else {
84              return StringPool.BLANK;
85          }
86      }
87  
88      public UnsyncByteArrayOutputStream getUnsyncByteArrayOutputStream() {
89          return _unsyncByteArrayOutputStream;
90      }
91  
92      public PrintWriter getWriter() {
93          if (_printWriter == null) {
94              _unsyncStringWriter = new UnsyncStringWriter();
95              _printWriter = new PrintWriter(_unsyncStringWriter);
96          }
97  
98          return _printWriter;
99      }
100 
101     public boolean isCalledGetOutputStream() {
102         if (_servletOutputStream != null) {
103             return true;
104         }
105         else {
106             return false;
107         }
108     }
109 
110     public void recycle() {
111         _status = SC_OK;
112         _string = null;
113 
114         resetBuffer();
115     }
116 
117     public void resetBuffer() {
118         if (_servletOutputStream != null) {
119             _unsyncByteArrayOutputStream.reset();
120         }
121         else if (_printWriter != null) {
122             _unsyncStringWriter.reset();
123         }
124     }
125 
126     public void setBufferSize(int bufferSize) {
127         _bufferSize = bufferSize;
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 static Log _log = LogFactoryUtil.getLog(
148         StringServletResponse.class);
149 
150     private int _bufferSize;
151     private String _contentType;
152     private PrintWriter _printWriter;
153     private ServletOutputStream _servletOutputStream;
154     private int _status = SC_OK;
155     private String _string;
156     private UnsyncByteArrayOutputStream _unsyncByteArrayOutputStream;
157     private UnsyncStringWriter _unsyncStringWriter;
158 
159 }