001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.filters;
031:
032: import com.caucho.log.Log;
033: import com.caucho.server.connection.AbstractResponseStream;
034: import com.caucho.server.connection.CauchoResponse;
035: import com.caucho.server.connection.ResponseWrapper;
036: import com.caucho.server.connection.ResponseWriter;
037: import com.caucho.server.connection.ServletOutputStreamImpl;
038: import com.caucho.vfs.FlushBuffer;
039:
040: import javax.servlet.ServletOutputStream;
041: import javax.servlet.ServletResponse;
042: import javax.servlet.http.HttpServletResponse;
043: import java.io.IOException;
044: import java.io.OutputStream;
045: import java.io.PrintWriter;
046: import java.io.UnsupportedEncodingException;
047: import java.util.Locale;
048: import java.util.logging.Logger;
049:
050: /**
051: * Response wrapper that can take advantage of Resin's streams.
052: */
053: public class CauchoResponseWrapper extends ResponseWrapper implements
054: CauchoResponse {
055: private static final Logger log = Log
056: .open(CauchoResponseWrapper.class);
057:
058: private FlushBuffer _flushBuffer;
059:
060: private final FilterWrapperResponseStream _originalStream;
061: protected AbstractResponseStream _stream;
062:
063: private ResponseWriter _writer;
064: private ServletOutputStreamImpl _os;
065:
066: private boolean _hasStream;
067:
068: private boolean _hasError;
069:
070: public CauchoResponseWrapper() {
071: _os = new ServletOutputStreamImpl();
072: _writer = new ResponseWriter();
073:
074: _originalStream = new FilterWrapperResponseStream();
075: }
076:
077: public CauchoResponseWrapper(HttpServletResponse response) {
078: this ();
079:
080: init(response);
081: }
082:
083: /**
084: * Initialize the response.
085: */
086: public void init(HttpServletResponse response) {
087: setResponse(response);
088:
089: _stream = _originalStream;
090: _os.init(_originalStream);
091: _writer.init(_originalStream);
092:
093: _hasStream = false;
094: _hasError = false;
095:
096: _originalStream.init(this );
097:
098: _originalStream.start();
099: }
100:
101: /**
102: * complete the response.
103: */
104: public void close() throws IOException {
105: if (_stream != null)
106: _stream.close();
107: else
108: _originalStream.close();
109: }
110:
111: public ServletResponse getResponse() {
112: return _response;
113: }
114:
115: /**
116: * Sets the response content type.
117: */
118: public void setContentType(String value) {
119: _response.setContentType(value);
120:
121: try {
122: _stream.setEncoding(getCharacterEncoding());
123: } catch (UnsupportedEncodingException e) {
124: }
125: }
126:
127: /**
128: * Sets the ResponseStream
129: */
130: public void setResponseStream(AbstractResponseStream stream) {
131: _stream = stream;
132:
133: _os.init(stream);
134: _writer.init(stream);
135: }
136:
137: /**
138: * Gets the response stream.
139: */
140: public AbstractResponseStream getResponseStream() {
141: return _stream;
142: }
143:
144: /**
145: * Returns true for a caucho response stream.
146: */
147: public boolean isCauchoResponseStream() {
148: return true;
149: }
150:
151: /**
152: * Returns the servlet output stream.
153: */
154: public ServletOutputStream getOutputStream() throws IOException {
155: return _os;
156: }
157:
158: /**
159: * Returns the print writer.
160: */
161: public PrintWriter getWriter() throws IOException {
162: return _writer;
163: }
164:
165: /**
166: * Returns the output stream for this wrapper.
167: */
168: protected OutputStream getStream() throws IOException {
169: return _response.getOutputStream();
170: }
171:
172: /**
173: * Sets the flush buffer
174: */
175: public void setFlushBuffer(FlushBuffer flushBuffer) {
176: _flushBuffer = flushBuffer;
177: }
178:
179: /**
180: * Gets the flush buffer
181: */
182: public FlushBuffer getFlushBuffer() {
183: return _flushBuffer;
184: }
185:
186: public void flushBuffer() throws IOException {
187: if (_flushBuffer != null)
188: _flushBuffer.flushBuffer();
189:
190: _stream.flushBuffer();
191:
192: //_response.flushBuffer();
193: }
194:
195: public void reset() {
196: super .reset();
197:
198: resetBuffer();
199: }
200:
201: public void resetBuffer() {
202: if (_stream != null)
203: _stream.clearBuffer();
204:
205: _response.resetBuffer();
206: }
207:
208: public void clearBuffer() {
209: resetBuffer();
210: }
211:
212: public void setLocale(Locale locale) {
213: _response.setLocale(locale);
214:
215: try {
216: _stream.setLocale(_response.getLocale());
217: } catch (UnsupportedEncodingException e) {
218: }
219: }
220:
221: /*
222: * caucho
223: */
224:
225: public String getHeader(String key) {
226: return null;
227: }
228:
229: public boolean disableHeaders(boolean disable) {
230: return false;
231: }
232:
233: public void setFooter(String key, String value) {
234: if (_response instanceof CauchoResponse)
235: ((CauchoResponse) _response).setFooter(key, value);
236: }
237:
238: public void addFooter(String key, String value) {
239: if (_response instanceof CauchoResponse)
240: ((CauchoResponse) _response).addFooter(key, value);
241: }
242:
243: public int getRemaining() {
244: /*
245: if (_response instanceof CauchoResponse)
246: return ((CauchoResponse) _response).getRemaining();
247: else
248: return 0;
249: */
250: return _stream.getRemaining();
251: }
252:
253: /**
254: * When set to true, RequestDispatcher.forward() is disallowed on
255: * this stream.
256: */
257: public void setForbidForward(boolean forbid) {
258: }
259:
260: /**
261: * Returns true if RequestDispatcher.forward() is disallowed on
262: * this stream.
263: */
264: public boolean getForbidForward() {
265: return false;
266: }
267:
268: /**
269: * Set to true while processing an error.
270: */
271: public void setHasError(boolean hasError) {
272: _hasError = hasError;
273: }
274:
275: /**
276: * Returns true if we're processing an error.
277: */
278: public boolean hasError() {
279: return _hasError;
280: }
281:
282: /**
283: * Kills the cache for an error.
284: */
285: public void killCache() {
286: if (_response instanceof CauchoResponse)
287: ((CauchoResponse) _response).killCache();
288: }
289:
290: public void setSessionId(String id) {
291: if (_response instanceof CauchoResponse)
292: ((CauchoResponse) _response).setSessionId(id);
293: }
294:
295: public void setPrivateCache(boolean isPrivate) {
296: if (_response instanceof CauchoResponse)
297: ((CauchoResponse) _response).setPrivateCache(isPrivate);
298: }
299:
300: public void setNoCache(boolean isPrivate) {
301: if (_response instanceof CauchoResponse)
302: ((CauchoResponse) _response).setNoCache(isPrivate);
303: }
304: }
|