KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > webapp > NamedDispatcherImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.webapp;
30
31 import com.caucho.server.connection.AbstractHttpResponse;
32 import com.caucho.server.connection.CauchoRequest;
33 import com.caucho.server.connection.RequestAdapter;
34
35 import javax.servlet.FilterChain JavaDoc;
36 import javax.servlet.RequestDispatcher JavaDoc;
37 import javax.servlet.ServletException JavaDoc;
38 import javax.servlet.ServletRequest JavaDoc;
39 import javax.servlet.ServletResponse JavaDoc;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.io.PrintWriter JavaDoc;
45
46 class NamedDispatcherImpl implements RequestDispatcher JavaDoc {
47   private WebApp _webApp;
48   private FilterChain JavaDoc _filterChain;
49   private String JavaDoc _queryString;
50
51   NamedDispatcherImpl(FilterChain JavaDoc filterChain, String JavaDoc queryString,
52                       WebApp webApp)
53   {
54     _filterChain = filterChain;
55     _queryString = queryString;
56     _webApp = webApp;
57   }
58
59   public void include(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
60     throws IOException JavaDoc, ServletException JavaDoc
61   {
62     HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
63
64     RequestAdapter reqAdapt = null;
65     if (! (request instanceof CauchoRequest)) {
66       reqAdapt = RequestAdapter.create();
67       reqAdapt.init((HttpServletRequest JavaDoc) request, res, _webApp);
68       request = reqAdapt;
69     }
70     CauchoRequest req = (CauchoRequest) request;
71     
72     //AbstractResponseStream s = res.getResponseStream();
73
// s.setDisableClose(true);
74

75     DispatchResponse subResponse = DispatchResponse.createDispatch();
76     subResponse.setRequest(req);
77     subResponse.setNextResponse(res);
78     // subResponse.init(req, s);
79
subResponse.start();
80     
81     try {
82       _filterChain.doFilter(req, subResponse);
83     } finally {
84       subResponse.finish();
85     }
86
87     if (reqAdapt != null)
88       RequestAdapter.free(reqAdapt);
89
90     DispatchResponse.free(subResponse);
91   }
92
93   /**
94    * Forward the request to the named servlet.
95    */

96   public void forward(ServletRequest JavaDoc req, ServletResponse JavaDoc res)
97     throws ServletException JavaDoc, IOException JavaDoc
98   {
99     res.resetBuffer();
100     
101     res.setContentLength(-1);
102
103     _filterChain.doFilter(req, res);
104
105     // this is not in a finally block so we can return a real error message
106
// if it's not handled.
107
// server/1328
108
if (res instanceof AbstractHttpResponse) {
109       ((AbstractHttpResponse) res).finish();
110     }
111     else {
112         try {
113           OutputStream JavaDoc os = res.getOutputStream();
114       if (os != null)
115         os.close();
116         } catch (IllegalStateException JavaDoc e) {
117         }
118     
119     try {
120       PrintWriter JavaDoc out = res.getWriter();
121       if (out != null)
122         out.close();
123     } catch (IllegalStateException JavaDoc e1) {
124     }
125
126     }
127
128     /*
129     ServletResponse ptr = res;
130     while (ptr instanceof HttpServletResponseWrapper) {
131       ptr = ((HttpServletResponseWrapper) ptr).getResponse();
132
133       if (ptr instanceof AbstractHttpResponse)
134     ((AbstractHttpResponse) ptr).finish();
135     }
136     */

137
138     /*
139     if (res instanceof AbstractHttpResponse)
140       ((AbstractHttpResponse) res).finish(true);
141
142     if (res instanceof Response)
143       ((Response) res).finish(true);
144     */

145   }
146 }
147
148
Popular Tags