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.server.webapp;
031:
032: import com.caucho.server.connection.AbstractHttpResponse;
033: import com.caucho.server.connection.AbstractResponseStream;
034: import com.caucho.server.connection.CauchoResponse;
035: import com.caucho.server.connection.IncludeResponseStream;
036: import com.caucho.util.FreeList;
037: import com.caucho.vfs.WriteStream;
038:
039: import javax.servlet.ServletResponse;
040: import javax.servlet.ServletResponseWrapper;
041: import javax.servlet.http.HttpServletResponse;
042: import java.io.IOException;
043:
044: /**
045: * Internal response for an include() or forward()
046: */
047: class DispatchResponse extends AbstractHttpResponse {
048: private static final FreeList<DispatchResponse> _freeList = new FreeList<DispatchResponse>(
049: 32);
050:
051: private IncludeResponseStream _stream;
052:
053: private HttpServletResponse _next;
054:
055: protected DispatchResponse() {
056: }
057:
058: /**
059: * Creates a dispatch request.
060: */
061: public static DispatchResponse createDispatch() {
062: DispatchResponse res = _freeList.allocate();
063: if (res == null)
064: res = new DispatchResponse();
065:
066: return res;
067: }
068:
069: /**
070: * Creates the response stream.
071: */
072: protected AbstractResponseStream createResponseStream() {
073: _stream = new IncludeResponseStream(this );
074:
075: return _stream;
076: }
077:
078: /**
079: * Sets the next response.
080: */
081: public void setNextResponse(HttpServletResponse next) {
082: _next = next;
083:
084: _stream.init(next);
085: }
086:
087: /**
088: * Gets the next response.
089: */
090: public ServletResponse getResponse() {
091: return _next;
092: }
093:
094: /**
095: * Starts the response.
096: */
097: public void start() throws IOException {
098: super .start();
099:
100: setResponseStream(_stream);
101:
102: _stream.start();
103: }
104:
105: /**
106: * included response can't set the content type.
107: */
108: public void setContentType(String type) {
109: }
110:
111: /**
112: * Wrapped calls.
113: */
114: public String encodeURL(String url) {
115: return _next.encodeURL(url);
116: }
117:
118: /**
119: * Wrapped calls.
120: */
121: public String encodeRedirectURL(String url) {
122: return _next.encodeRedirectURL(url);
123: }
124:
125: /**
126: * included() responses don't print the headers.
127: */
128: protected boolean writeHeadersInt(WriteStream os, int length)
129: throws IOException {
130: return false;
131: }
132:
133: /**
134: * This is not a top response.
135: */
136: public boolean isTop() {
137: return false;
138: }
139:
140: @Override
141: public String getCharacterEncoding() {
142: // jsp/17e1
143: return _next.getCharacterEncoding();
144: }
145:
146: @Override
147: public boolean isCommitted() {
148: // jsp/15m2
149: return _next.isCommitted();
150: }
151:
152: /**
153: * Returns true for a caucho response.
154: */
155: public boolean isCauchoResponse() {
156: return _next instanceof CauchoResponse;
157: }
158:
159: /**
160: * Set true for a caucho response stream.
161: */
162: public void setCauchoResponseStream(boolean isCaucho) {
163: _stream.setCauchoResponseStream(isCaucho);
164: }
165:
166: /**
167: * Kills the cache.
168: */
169: public void killCache() {
170: super .killCache();
171:
172: ServletResponse next = _next;
173: while (next != null && next != this ) {
174: if (next instanceof CauchoResponse) {
175: ((CauchoResponse) next).killCache();
176: break;
177: }
178:
179: if (next instanceof ServletResponseWrapper)
180: next = ((ServletResponseWrapper) next).getResponse();
181: else if (next instanceof DispatchResponse)
182: next = ((DispatchResponse) next).getResponse();
183: else
184: break;
185: }
186: }
187:
188: /**
189: * Frees the response.
190: */
191: public void free() {
192: super .free();
193:
194: _next = null;
195: }
196:
197: /**
198: * Frees the request.
199: */
200: public static void free(DispatchResponse res) {
201: res.free();
202:
203: _freeList.free(res);
204: }
205: }
|