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.webapp;
030:
031: import com.caucho.server.connection.AbstractHttpResponse;
032: import com.caucho.server.connection.CauchoRequest;
033: import com.caucho.server.connection.RequestAdapter;
034:
035: import javax.servlet.FilterChain;
036: import javax.servlet.RequestDispatcher;
037: import javax.servlet.ServletException;
038: import javax.servlet.ServletRequest;
039: import javax.servlet.ServletResponse;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import java.io.IOException;
043: import java.io.OutputStream;
044: import java.io.PrintWriter;
045:
046: class NamedDispatcherImpl implements RequestDispatcher {
047: private WebApp _webApp;
048:
049: private FilterChain _includeFilterChain;
050: private FilterChain _forwardFilterChain;
051:
052: private String _queryString;
053:
054: NamedDispatcherImpl(FilterChain includeFilterChain,
055: FilterChain forwardFilterChain, String queryString,
056: WebApp webApp) {
057: _includeFilterChain = includeFilterChain;
058: _forwardFilterChain = forwardFilterChain;
059: _queryString = queryString;
060: _webApp = webApp;
061: }
062:
063: public void include(ServletRequest request, ServletResponse response)
064: throws IOException, ServletException {
065: HttpServletResponse res = (HttpServletResponse) response;
066:
067: RequestAdapter reqAdapt = null;
068: if (!(request instanceof CauchoRequest)) {
069: reqAdapt = RequestAdapter.create();
070: reqAdapt.init((HttpServletRequest) request, res, _webApp);
071: request = reqAdapt;
072: }
073: CauchoRequest req = (CauchoRequest) request;
074:
075: //AbstractResponseStream s = res.getResponseStream();
076: // s.setDisableClose(true);
077:
078: DispatchResponse subResponse = DispatchResponse
079: .createDispatch();
080: subResponse.init(req);
081: subResponse.setNextResponse(res);
082: // subResponse.init(req, s);
083: subResponse.start();
084:
085: try {
086: _includeFilterChain.doFilter(req, subResponse);
087: } finally {
088: subResponse.finish();
089: }
090:
091: if (reqAdapt != null)
092: RequestAdapter.free(reqAdapt);
093:
094: DispatchResponse.free(subResponse);
095: }
096:
097: /**
098: * Forward the request to the named servlet.
099: */
100: public void forward(ServletRequest req, ServletResponse res)
101: throws ServletException, IOException {
102: res.resetBuffer();
103:
104: res.setContentLength(-1);
105:
106: _forwardFilterChain.doFilter(req, res);
107:
108: // this is not in a finally block so we can return a real error message
109: // if it's not handled.
110: // server/1328
111: if (res instanceof AbstractHttpResponse) {
112: ((AbstractHttpResponse) res).finish();
113: } else {
114: try {
115: OutputStream os = res.getOutputStream();
116: if (os != null)
117: os.close();
118: } catch (IllegalStateException e) {
119: }
120:
121: try {
122: PrintWriter out = res.getWriter();
123: if (out != null)
124: out.close();
125: } catch (IllegalStateException e1) {
126: }
127:
128: }
129:
130: /*
131: ServletResponse ptr = res;
132: while (ptr instanceof HttpServletResponseWrapper) {
133: ptr = ((HttpServletResponseWrapper) ptr).getResponse();
134:
135: if (ptr instanceof AbstractHttpResponse)
136: ((AbstractHttpResponse) ptr).finish();
137: }
138: */
139:
140: /*
141: if (res instanceof AbstractHttpResponse)
142: ((AbstractHttpResponse) res).finish(true);
143:
144: if (res instanceof Response)
145: ((Response) res).finish(true);
146: */
147: }
148:
149: public String toString() {
150: return getClass().getSimpleName() + "[" + _includeFilterChain
151: + "]";
152: }
153: }
|