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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.connection;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.FreeList;
033: import com.caucho.vfs.FlushBuffer;
034:
035: import javax.servlet.ServletOutputStream;
036: import javax.servlet.ServletResponse;
037: import javax.servlet.http.Cookie;
038: import javax.servlet.http.HttpServletResponse;
039: import java.io.IOException;
040: import java.io.PrintWriter;
041: import java.io.UnsupportedEncodingException;
042: import java.util.logging.Logger;
043:
044: public class ResponseAdapter extends ResponseWrapper implements
045: CauchoResponse {
046: private static final Logger log = Logger
047: .getLogger(ResponseAdapter.class.getName());
048:
049: private static final FreeList<ResponseAdapter> _freeList = new FreeList<ResponseAdapter>(
050: 32);
051:
052: protected RequestAdapter _request;
053:
054: protected FlushBuffer _flushBuffer;
055:
056: protected AbstractResponseStream _originalResponseStream;
057: protected AbstractResponseStream _responseStream;
058:
059: private ServletOutputStreamImpl _os;
060: private ResponseWriter _writer;
061:
062: private boolean _hasOutputStream;
063: private boolean _hasError;
064:
065: private ResponseAdapter() {
066: }
067:
068: protected ResponseAdapter(HttpServletResponse response) {
069: setResponse(response);
070:
071: _originalResponseStream = createWrapperResponseStream();
072:
073: _os = new ServletOutputStreamImpl();
074: _writer = new ResponseWriter();
075: }
076:
077: /**
078: * Creates a new ResponseAdapter.
079: */
080: public static ResponseAdapter create(HttpServletResponse response) {
081: ResponseAdapter resAdapt = _freeList.allocate();
082:
083: if (resAdapt == null)
084: resAdapt = new ResponseAdapter(response);
085: else
086: resAdapt.setResponse(response);
087:
088: resAdapt.init(response);
089:
090: return resAdapt;
091: }
092:
093: void setRequest(RequestAdapter request) {
094: _request = request;
095: }
096:
097: protected AbstractResponseStream createWrapperResponseStream() {
098: return new WrapperResponseStream();
099: }
100:
101: public void init(HttpServletResponse response) {
102: setResponse(response);
103: _hasError = false;
104:
105: _responseStream = _originalResponseStream;
106: if (_originalResponseStream instanceof WrapperResponseStream) {
107: WrapperResponseStream wrapper = (WrapperResponseStream) _originalResponseStream;
108: wrapper.init(response);
109: }
110:
111: _originalResponseStream.start();
112:
113: _os.init(_originalResponseStream);
114: _writer.init(_originalResponseStream);
115: }
116:
117: public AbstractResponseStream getResponseStream() {
118: return _responseStream;
119: }
120:
121: public boolean isCauchoResponseStream() {
122: return false;
123: }
124:
125: public void setResponseStream(AbstractResponseStream responseStream) {
126: _responseStream = responseStream;
127:
128: _os.init(responseStream);
129: _writer.init(responseStream);
130: }
131:
132: public boolean isTop() {
133: return false;
134: }
135:
136: public void resetBuffer() {
137: super .resetBuffer();
138:
139: _responseStream.clearBuffer();
140: }
141:
142: public void sendRedirect(String url) throws IOException {
143: resetBuffer();
144:
145: super .sendRedirect(url);
146: }
147:
148: @Override
149: public int getBufferSize() {
150: return _responseStream.getBufferSize();
151: }
152:
153: @Override
154: public void setBufferSize(int size) {
155: _responseStream.setBufferSize(size);
156: }
157:
158: public ServletOutputStream getOutputStream() throws IOException {
159: return _os;
160: }
161:
162: /**
163: * Sets the flush buffer
164: */
165: public void setFlushBuffer(FlushBuffer flushBuffer) {
166: _flushBuffer = flushBuffer;
167: }
168:
169: /**
170: * Gets the flush buffer
171: */
172: public FlushBuffer getFlushBuffer() {
173: return _flushBuffer;
174: }
175:
176: public PrintWriter getWriter() throws IOException {
177: return _writer;
178: }
179:
180: public void setContentType(String value) {
181: super .setContentType(value);
182:
183: try {
184: _responseStream.setEncoding(getCharacterEncoding());
185: } catch (UnsupportedEncodingException e) {
186: }
187: }
188:
189: public void addCookie(Cookie cookie) {
190: if (_request != null)
191: _request.setHasCookie();
192:
193: super .addCookie(cookie);
194: }
195:
196: /*
197: * caucho
198: */
199:
200: public String getHeader(String key) {
201: return null;
202: }
203:
204: public boolean disableHeaders(boolean disable) {
205: return false;
206: }
207:
208: public void setFooter(String key, String value) {
209: }
210:
211: public void addFooter(String key, String value) {
212: }
213:
214: public int getRemaining() {
215: return _responseStream.getRemaining();
216: }
217:
218: /**
219: * When set to true, RequestDispatcher.forward() is disallowed on
220: * this stream.
221: */
222: public void setForbidForward(boolean forbid) {
223: }
224:
225: /**
226: * Returns true if RequestDispatcher.forward() is disallowed on
227: * this stream.
228: */
229: public boolean getForbidForward() {
230: return false;
231: }
232:
233: /**
234: * Set to true while processing an error.
235: */
236: public void setHasError(boolean hasError) {
237: _hasError = hasError;
238: }
239:
240: /**
241: * Returns true if we're processing an error.
242: */
243: public boolean hasError() {
244: return _hasError;
245: }
246:
247: /**
248: * Kills the cache for an error.
249: */
250: public void killCache() {
251: if (getResponse() instanceof CauchoResponse)
252: ((CauchoResponse) getResponse()).killCache();
253: }
254:
255: /**
256: * Sets private caching
257: */
258: public void setPrivateCache(boolean isPrivate) {
259: if (getResponse() instanceof CauchoResponse)
260: ((CauchoResponse) getResponse()).setPrivateCache(isPrivate);
261: }
262:
263: /**
264: * Sets no caching
265: */
266: public void setNoCache(boolean isPrivate) {
267: if (getResponse() instanceof CauchoResponse)
268: ((CauchoResponse) getResponse()).setNoCache(isPrivate);
269: }
270:
271: public void setSessionId(String id) {
272: if (getResponse() instanceof CauchoResponse)
273: ((CauchoResponse) getResponse()).setSessionId(id);
274: }
275:
276: public void finish() throws IOException {
277: if (_responseStream != null)
278: _responseStream.flushBuffer();
279:
280: _responseStream = _originalResponseStream;
281: }
282:
283: public void close() throws IOException {
284: ServletResponse response = getResponse();
285:
286: AbstractResponseStream responseStream = _responseStream;
287: _responseStream = _originalResponseStream;
288:
289: if (responseStream != null)
290: responseStream.close();
291:
292: if (_originalResponseStream != responseStream)
293: _originalResponseStream.close();
294:
295: if (response instanceof CauchoResponse) {
296: ((CauchoResponse) response).close();
297: }
298: /*
299: else {
300: try {
301: PrintWriter writer = response.getWriter();
302: writer.close();
303: } catch (Throwable e) {
304: }
305:
306: try {
307: OutputStream os = response.getOutputStream();
308: os.close();
309: } catch (Throwable e) {
310: }
311: }
312: */
313: }
314:
315: public static void free(ResponseAdapter resAdapt) {
316: resAdapt.free();
317:
318: _freeList.free(resAdapt);
319: }
320:
321: /**
322: * Clears the adapter.
323: */
324: protected void free() {
325: _request = null;
326: _responseStream = null;
327:
328: setResponse(null);
329: }
330: }
|