KickJava   Java API By Example, From Geeks To Geeks.

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


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.jca.UserTransactionProxy;
32 import com.caucho.log.Log;
33 import com.caucho.server.connection.AbstractHttpRequest;
34 import com.caucho.server.connection.AbstractHttpResponse;
35 import com.caucho.server.log.AbstractAccessLog;
36 import com.caucho.transaction.TransactionManagerImpl;
37
38 import javax.servlet.FilterChain JavaDoc;
39 import javax.servlet.ServletException JavaDoc;
40 import javax.servlet.ServletRequest JavaDoc;
41 import javax.servlet.ServletRequestEvent JavaDoc;
42 import javax.servlet.ServletRequestListener JavaDoc;
43 import javax.servlet.ServletResponse JavaDoc;
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45 import javax.servlet.http.HttpServletResponse JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.logging.Level JavaDoc;
49 import java.util.logging.Logger JavaDoc;
50
51 /**
52  * Represents the next filter in a filter chain. The final filter will
53  * be the servlet itself.
54  */

55 public class WebAppFilterChain implements FilterChain JavaDoc {
56   private static final Logger JavaDoc log = Log.open(WebAppFilterChain.class);
57   
58   // Next filter chain
59
private FilterChain JavaDoc _next;
60
61   // app
62
private WebApp _app;
63   // transaction manager
64
private TransactionManagerImpl _tm;
65   // transaction proxy
66
private UserTransactionProxy _utm;
67   // error page manager
68
private ErrorPageManager _errorPageManager;
69
70   private ServletRequestListener JavaDoc []_requestListeners;
71
72   private HashMap JavaDoc<String JavaDoc,String JavaDoc> _securityRoleMap;
73
74   private AbstractAccessLog _accessLog;
75
76   // true it's the top
77
private boolean _isTop = true;
78
79
80   /**
81    * Creates a new FilterChainFilter.
82    *
83    * @param next the next filterChain
84    * @param filter the user's filter
85    */

86   public WebAppFilterChain(FilterChain JavaDoc next, WebApp app)
87   {
88     this(next, app, true);
89   }
90
91   /**
92    * Creates a new FilterChainFilter.
93    *
94    * @param next the next filterChain
95    * @param filter the user's filter
96    */

97   public WebAppFilterChain(FilterChain JavaDoc next, WebApp app, boolean isTop)
98   {
99     _next = next;
100     _app = app;
101     _errorPageManager = app.getErrorPageManager();
102     _isTop = isTop;
103     _requestListeners = app.getRequestListeners();
104
105     if (_isTop)
106       _accessLog = app.getAccessLog();
107
108     try {
109       if (_isTop) {
110     _tm = TransactionManagerImpl.getInstance();
111     _utm = UserTransactionProxy.getInstance();
112       }
113     } catch (Throwable JavaDoc e) {
114       log.log(Level.WARNING, e.toString(), e);
115     }
116   }
117
118   /**
119    * Sets the security map.
120    */

121   public void setSecurityRoleMap(HashMap JavaDoc<String JavaDoc,String JavaDoc> map)
122   {
123     _securityRoleMap = map;
124   }
125   
126   /**
127    * Invokes the next filter in the chain or the final servlet at
128    * the end of the chain.
129    *
130    * @param request the servlet request
131    * @param response the servlet response
132    * @since Servlet 2.3
133    */

134   public void doFilter(ServletRequest JavaDoc request,
135                        ServletResponse JavaDoc response)
136     throws ServletException JavaDoc, IOException JavaDoc
137   {
138     Thread JavaDoc thread = Thread.currentThread();
139     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
140
141     WebApp app = _app;
142     
143     try {
144       thread.setContextClassLoader(app.getClassLoader());
145
146       if (! app.enterWebApp() && app.getConfigException() == null) {
147     if (response instanceof HttpServletResponse JavaDoc) {
148       HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
149
150       res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
151     }
152     
153     return;
154       }
155
156       /*
157       if (_securityRoleMap != null && request instanceof AbstractHttpRequest)
158     ((AbstractHttpRequest) request).setRoleMap(_securityRoleMap);
159       */

160
161       for (int i = 0; i < _requestListeners.length; i++) {
162     ServletRequestEvent JavaDoc event = new ServletRequestEvent JavaDoc(_app, request);
163     
164     _requestListeners[i].requestInitialized(event);
165       }
166
167       _next.doFilter(request, response);
168     } catch (Throwable JavaDoc e) {
169       _errorPageManager.sendServletError(e, request, response);
170     } finally {
171       app.exitWebApp();
172
173       for (int i = _requestListeners.length - 1; i >= 0; i--) {
174     try {
175       ServletRequestEvent JavaDoc event = new ServletRequestEvent JavaDoc(_app, request);
176     
177       _requestListeners[i].requestDestroyed(event);
178     } catch (Throwable JavaDoc e) {
179       log.log(Level.WARNING, e.toString(), e);
180     }
181       }
182
183       if (_isTop) {
184     ((AbstractHttpResponse) response).close();
185     
186     try {
187       _utm.abortTransaction();
188     } catch (Throwable JavaDoc e) {
189       _app.log(e.getMessage(), e);
190     }
191       }
192         
193       try {
194     if (_accessLog != null) {
195       _accessLog.log((HttpServletRequest JavaDoc) request,
196              (HttpServletResponse JavaDoc) response,
197              _app);
198     }
199       } catch (Throwable JavaDoc e) {
200     log.log(Level.FINE, e.toString(), e);
201       }
202
203       // needed for things like closing the session
204
if (request instanceof AbstractHttpRequest)
205         ((AbstractHttpRequest) request).finish();
206       
207       thread.setContextClassLoader(oldLoader);
208     }
209   }
210 }
211
Popular Tags