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.IOException;
24  import java.io.PrintWriter;
25  import java.io.UnsupportedEncodingException;
26  
27  import java.util.Locale;
28  
29  import javax.servlet.ServletOutputStream;
30  import javax.servlet.http.HttpServletResponse;
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 HeaderCacheServletResponse {
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 void flushBuffer() throws IOException {
53          if (_servletOutputStream != null) {
54              _unsyncByteArrayOutputStream.flush();
55          }
56          else if (_printWriter != null) {
57              _unsyncStringWriter.flush();
58          }
59      }
60  
61      public ServletOutputStream getOutputStream() {
62          if (_printWriter != null) {
63              throw new IllegalStateException(
64                  "Cannot obtain OutputStream because Writer is already in use");
65          }
66  
67          if (_servletOutputStream == null) {
68              _unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
69              _servletOutputStream = new PipingServletOutputStream(
70                  _unsyncByteArrayOutputStream);
71          }
72  
73          return _servletOutputStream;
74      }
75  
76      public int getStatus() {
77          return _status;
78      }
79  
80      public String getString() {
81          if (_string != null) {
82              return _string;
83          }
84  
85          if (_servletOutputStream != null) {
86              try {
87                  _string = _unsyncByteArrayOutputStream.toString(
88                      StringPool.UTF8);
89              }
90              catch (UnsupportedEncodingException uee) {
91                  _log.error(uee, uee);
92  
93                  _string = StringPool.BLANK;
94              }
95          }
96          else if (_printWriter != null) {
97              _string = _unsyncStringWriter.toString();
98          }
99          else {
100             _string = StringPool.BLANK;
101         }
102 
103         return _string;
104     }
105 
106     public UnsyncByteArrayOutputStream getUnsyncByteArrayOutputStream() {
107         return _unsyncByteArrayOutputStream;
108     }
109 
110     public PrintWriter getWriter() {
111         if (_servletOutputStream != null) {
112             throw new IllegalStateException(
113                 "Cannot obtain Writer because OutputStream is already in use");
114         }
115 
116         if (_printWriter == null) {
117             _unsyncStringWriter = new UnsyncStringWriter();
118             _printWriter = new PrintWriter(_unsyncStringWriter);
119         }
120 
121         return _printWriter;
122     }
123 
124     public boolean isCalledGetOutputStream() {
125         if (_servletOutputStream != null) {
126             return true;
127         }
128         else {
129             return false;
130         }
131     }
132 
133     public void recycle() {
134         _status = SC_OK;
135         _string = null;
136 
137         resetBuffer();
138     }
139 
140     public void resetBuffer() {
141         if (_servletOutputStream != null) {
142             _unsyncByteArrayOutputStream.reset();
143         }
144         else if (_printWriter != null) {
145             _unsyncStringWriter.reset();
146         }
147     }
148 
149     public void sendError(int status) throws IOException {
150         _status = status;
151 
152         super.sendError(status);
153     }
154 
155     public void sendError(int status, String msg) throws IOException {
156         _status = status;
157 
158         super.sendError(status, msg);
159     }
160 
161     public void setBufferSize(int bufferSize) {
162         _bufferSize = bufferSize;
163     }
164 
165     public void setContentType(String contentType) {
166         _contentType = contentType;
167 
168         super.setContentType(contentType);
169     }
170 
171     public void setLocale(Locale locale) {
172     }
173 
174     public void setStatus(int status) {
175         _status = status;
176 
177         super.setStatus(_status);
178     }
179 
180     public void setString(String string) {
181         _string = string;
182     }
183 
184     private static Log _log = LogFactoryUtil.getLog(
185         StringServletResponse.class);
186 
187     private int _bufferSize;
188     private String _contentType;
189     private PrintWriter _printWriter;
190     private ServletOutputStream _servletOutputStream;
191     private int _status = SC_OK;
192     private String _string;
193     private UnsyncByteArrayOutputStream _unsyncByteArrayOutputStream;
194     private UnsyncStringWriter _unsyncStringWriter;
195 
196 }