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.dispatch;
030:
031: import javax.servlet.FilterChain;
032: import javax.servlet.Servlet;
033: import javax.servlet.ServletException;
034: import javax.servlet.ServletRequest;
035: import javax.servlet.ServletResponse;
036: import javax.servlet.UnavailableException;
037: import java.io.IOException;
038: import java.util.HashMap;
039:
040: import com.caucho.servlet.comet.CometServlet;
041: import com.caucho.servlet.comet.CometController;
042:
043: import com.caucho.server.connection.AbstractHttpRequest;
044: import com.caucho.server.connection.HttpConnectionController;
045:
046: /**
047: * Represents the final servlet in a filter chain.
048: */
049: public class CometServletFilterChain extends AbstractFilterChain {
050: public static String SERVLET_NAME = "javax.servlet.error.servlet_name";
051:
052: // servlet config
053: private ServletConfigImpl _config;
054: // servlet
055: private CometServlet _servlet;
056:
057: /**
058: * Create the filter chain servlet.
059: *
060: * @param servlet the underlying servlet
061: */
062: public CometServletFilterChain(ServletConfigImpl config) {
063: if (config == null)
064: throw new NullPointerException();
065:
066: _config = config;
067: }
068:
069: /**
070: * Returns the servlet name.
071: */
072: public String getServletName() {
073: return _config.getServletName();
074: }
075:
076: /**
077: * Returns the role map.
078: */
079: public HashMap<String, String> getRoleMap() {
080: return _config.getRoleMap();
081: }
082:
083: /**
084: * Invokes the final servlet at the end of the chain.
085: *
086: * @param request the servlet request
087: * @param response the servlet response
088: *
089: * @since Servlet 2.3
090: */
091: public void doFilter(ServletRequest request,
092: ServletResponse response) throws ServletException,
093: IOException {
094: if (_servlet == null) {
095: try {
096: _servlet = (CometServlet) _config.createServlet(false);
097: } catch (ServletException e) {
098: throw e;
099: } catch (Exception e) {
100: throw new ServletException(e);
101: }
102: }
103:
104: CometController controller = null;
105:
106: try {
107: controller = new HttpConnectionController(request);
108:
109: if (_servlet.service(request, response, controller))
110: controller = null;
111: } catch (UnavailableException e) {
112: _servlet = null;
113: _config.setInitException(e);
114: _config.killServlet();
115: request
116: .setAttribute(SERVLET_NAME, _config
117: .getServletName());
118: throw e;
119: } catch (ServletException e) {
120: request
121: .setAttribute(SERVLET_NAME, _config
122: .getServletName());
123: throw e;
124: } catch (IOException e) {
125: request
126: .setAttribute(SERVLET_NAME, _config
127: .getServletName());
128: throw e;
129: } catch (RuntimeException e) {
130: request
131: .setAttribute(SERVLET_NAME, _config
132: .getServletName());
133: throw e;
134: } finally {
135: if (controller != null)
136: controller.close();
137: }
138: }
139:
140: /**
141: * Resumes the final servlet for a comet request.
142: *
143: * @param request the servlet request
144: * @param response the servlet response
145: *
146: * @since Resin 3.1.3
147: */
148: @Override
149: public boolean doResume(ServletRequest request,
150: ServletResponse response) throws ServletException,
151: IOException {
152: if (_servlet == null) {
153: try {
154: _servlet = (CometServlet) _config.createServlet(false);
155: } catch (ServletException e) {
156: throw e;
157: } catch (Exception e) {
158: throw new ServletException(e);
159: }
160: }
161:
162: CometController controller = null;
163: try {
164: AbstractHttpRequest req = (AbstractHttpRequest) request;
165:
166: controller = (CometController) req.getConnection()
167: .getController();
168:
169: if (controller == null)
170: return false;
171: else if (_servlet.resume(request, response, controller)) {
172: controller = null;
173: return true;
174: } else
175: return false;
176: } catch (UnavailableException e) {
177: _servlet = null;
178: _config.setInitException(e);
179: _config.killServlet();
180: request
181: .setAttribute(SERVLET_NAME, _config
182: .getServletName());
183: throw e;
184: } catch (ServletException e) {
185: request
186: .setAttribute(SERVLET_NAME, _config
187: .getServletName());
188: throw e;
189: } catch (IOException e) {
190: request
191: .setAttribute(SERVLET_NAME, _config
192: .getServletName());
193: throw e;
194: } catch (RuntimeException e) {
195: request
196: .setAttribute(SERVLET_NAME, _config
197: .getServletName());
198: throw e;
199: } finally {
200: if (controller != null)
201: controller.close();
202: }
203: }
204: }
|