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.jca.UserTransactionProxy;
032: import com.caucho.log.Log;
033: import com.caucho.servlet.comet.CometFilterChain;
034: import com.caucho.server.connection.AbstractHttpRequest;
035: import com.caucho.server.connection.AbstractHttpResponse;
036: import com.caucho.server.dispatch.AbstractFilterChain;
037: import com.caucho.server.log.AbstractAccessLog;
038: import com.caucho.transaction.TransactionManagerImpl;
039:
040: import javax.servlet.FilterChain;
041: import javax.servlet.ServletException;
042: import javax.servlet.ServletRequest;
043: import javax.servlet.ServletRequestEvent;
044: import javax.servlet.ServletRequestListener;
045: import javax.servlet.ServletResponse;
046: import javax.servlet.http.HttpServletRequest;
047: import javax.servlet.http.HttpServletResponse;
048: import java.io.IOException;
049: import java.util.HashMap;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052:
053: /**
054: * Represents the next filter in a filter chain. The final filter will
055: * be the servlet itself.
056: */
057: public class WebAppFilterChain extends AbstractFilterChain {
058: private static final Logger log = Logger
059: .getLogger(WebAppFilterChain.class.getName());
060:
061: // Next filter chain
062: private FilterChain _next;
063:
064: // app
065: private WebApp _app;
066: // transaction manager
067: private TransactionManagerImpl _tm;
068: // transaction proxy
069: private UserTransactionProxy _utm;
070: // error page manager
071: private ErrorPageManager _errorPageManager;
072:
073: private ServletRequestListener[] _requestListeners;
074:
075: private HashMap<String, String> _securityRoleMap;
076:
077: private ThreadLocal<ServletRequest> _requestThreadLocal;
078:
079: private AbstractAccessLog _accessLog;
080:
081: // true it's the top
082: private boolean _isTop = true;
083:
084: /**
085: * Creates a new FilterChainFilter.
086: *
087: * @param next the next filterChain
088: * @param filter the user's filter
089: */
090: public WebAppFilterChain(FilterChain next, WebApp app) {
091: this (next, app, true);
092: }
093:
094: /**
095: * Creates a new FilterChainFilter.
096: *
097: * @param next the next filterChain
098: * @param filter the user's filter
099: */
100: public WebAppFilterChain(FilterChain next, WebApp app, boolean isTop) {
101: _next = next;
102: _app = app;
103: _errorPageManager = app.getErrorPageManager();
104: _isTop = isTop;
105: _requestListeners = app.getRequestListeners();
106:
107: _requestThreadLocal = app.getRequestThreadLocal();
108:
109: if (_isTop)
110: _accessLog = app.getAccessLog();
111:
112: try {
113: if (_isTop) {
114: _tm = TransactionManagerImpl.getInstance();
115: _utm = UserTransactionProxy.getInstance();
116: }
117: } catch (Throwable e) {
118: log.log(Level.WARNING, e.toString(), e);
119: }
120: }
121:
122: /**
123: * Sets the security map.
124: */
125: public void setSecurityRoleMap(HashMap<String, String> map) {
126: _securityRoleMap = map;
127: }
128:
129: /**
130: * Returns true if cacheable.
131: */
132: public FilterChain getNext() {
133: return _next;
134: }
135:
136: /**
137: * Invokes the next filter in the chain or the final servlet at
138: * the end of the chain.
139: *
140: * @param request the servlet request
141: * @param response the servlet response
142: * @since Servlet 2.3
143: */
144: public void doFilter(ServletRequest request,
145: ServletResponse response) throws ServletException,
146: IOException {
147: Thread thread = Thread.currentThread();
148: ClassLoader oldLoader = thread.getContextClassLoader();
149:
150: WebApp app = _app;
151:
152: try {
153: thread.setContextClassLoader(app.getClassLoader());
154:
155: if (!app.enterWebApp() && app.getConfigException() == null) {
156: if (response instanceof HttpServletResponse) {
157: HttpServletResponse res = (HttpServletResponse) response;
158:
159: res
160: .sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
161: }
162:
163: return;
164: }
165:
166: /*
167: if (_securityRoleMap != null && request instanceof AbstractHttpRequest)
168: ((AbstractHttpRequest) request).setRoleMap(_securityRoleMap);
169: */
170:
171: for (int i = 0; i < _requestListeners.length; i++) {
172: ServletRequestEvent event = new ServletRequestEvent(
173: _app, request);
174:
175: _requestListeners[i].requestInitialized(event);
176: }
177:
178: _next.doFilter(request, response);
179: } catch (Throwable e) {
180: _errorPageManager.sendServletError(e, request, response);
181: } finally {
182: app.exitWebApp();
183:
184: for (int i = _requestListeners.length - 1; i >= 0; i--) {
185: try {
186: ServletRequestEvent event = new ServletRequestEvent(
187: _app, request);
188:
189: _requestListeners[i].requestDestroyed(event);
190: } catch (Throwable e) {
191: log.log(Level.WARNING, e.toString(), e);
192: }
193: }
194:
195: if (_isTop) {
196: ((AbstractHttpResponse) response).close();
197:
198: try {
199: _utm.abortTransaction();
200: } catch (Throwable e) {
201: log.log(Level.WARNING, e.toString(), e);
202: }
203: }
204:
205: // put finish() before access log so the session isn't tied up while
206: // logging
207:
208: // needed for things like closing the session
209: if (request instanceof AbstractHttpRequest)
210: ((AbstractHttpRequest) request).finish();
211:
212: try {
213: if (_accessLog != null) {
214: _accessLog.log((HttpServletRequest) request,
215: (HttpServletResponse) response, _app);
216: }
217: } catch (Throwable e) {
218: log.log(Level.FINE, e.toString(), e);
219: }
220:
221: thread.setContextClassLoader(oldLoader);
222: }
223: }
224:
225: /**
226: * Resumes the request for comet-style.
227: *
228: * @param request the servlet request
229: * @param response the servlet response
230: * @since Resin 3.1.3
231: */
232: @Override
233: public boolean doResume(ServletRequest request,
234: ServletResponse response) throws ServletException,
235: IOException {
236: Thread thread = Thread.currentThread();
237: ClassLoader oldLoader = thread.getContextClassLoader();
238:
239: WebApp app = _app;
240:
241: try {
242: thread.setContextClassLoader(app.getClassLoader());
243:
244: if (!app.enterWebApp())
245: return false;
246:
247: if (_next instanceof CometFilterChain) {
248: CometFilterChain next = (CometFilterChain) _next;
249:
250: return next.doResume(request, response);
251: } else
252: return false;
253: } catch (Throwable e) {
254: _errorPageManager.sendServletError(e, request, response);
255:
256: return false;
257: } finally {
258: app.exitWebApp();
259:
260: if (_isTop) {
261: ((AbstractHttpResponse) response).close();
262:
263: try {
264: _utm.abortTransaction();
265: } catch (Throwable e) {
266: log.log(Level.WARNING, e.toString(), e);
267: }
268: }
269:
270: // put finish() before access log so the session isn't tied up while
271: // logging
272:
273: // needed for things like closing the session
274: if (request instanceof AbstractHttpRequest)
275: ((AbstractHttpRequest) request).finish();
276:
277: /*
278: try {
279: if (_accessLog != null) {
280: _accessLog.log((HttpServletRequest) request,
281: (HttpServletResponse) response,
282: _app);
283: }
284: } catch (Throwable e) {
285: log.log(Level.FINE, e.toString(), e);
286: }
287: */
288:
289: thread.setContextClassLoader(oldLoader);
290: }
291: }
292:
293: public String toString() {
294: return getClass().getSimpleName() + "[" + _app.getURL()
295: + ", next=" + _next + "]";
296: }
297: }
|