1 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 ; 39 import javax.servlet.ServletException ; 40 import javax.servlet.ServletRequest ; 41 import javax.servlet.ServletRequestEvent ; 42 import javax.servlet.ServletRequestListener ; 43 import javax.servlet.ServletResponse ; 44 import javax.servlet.http.HttpServletRequest ; 45 import javax.servlet.http.HttpServletResponse ; 46 import java.io.IOException ; 47 import java.util.HashMap ; 48 import java.util.logging.Level ; 49 import java.util.logging.Logger ; 50 51 55 public class WebAppFilterChain implements FilterChain { 56 private static final Logger log = Log.open(WebAppFilterChain.class); 57 58 private FilterChain _next; 60 61 private WebApp _app; 63 private TransactionManagerImpl _tm; 65 private UserTransactionProxy _utm; 67 private ErrorPageManager _errorPageManager; 69 70 private ServletRequestListener []_requestListeners; 71 72 private HashMap <String ,String > _securityRoleMap; 73 74 private AbstractAccessLog _accessLog; 75 76 private boolean _isTop = true; 78 79 80 86 public WebAppFilterChain(FilterChain next, WebApp app) 87 { 88 this(next, app, true); 89 } 90 91 97 public WebAppFilterChain(FilterChain 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 e) { 114 log.log(Level.WARNING, e.toString(), e); 115 } 116 } 117 118 121 public void setSecurityRoleMap(HashMap <String ,String > map) 122 { 123 _securityRoleMap = map; 124 } 125 126 134 public void doFilter(ServletRequest request, 135 ServletResponse response) 136 throws ServletException , IOException 137 { 138 Thread thread = Thread.currentThread(); 139 ClassLoader 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 ) { 148 HttpServletResponse res = (HttpServletResponse ) response; 149 150 res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); 151 } 152 153 return; 154 } 155 156 160 161 for (int i = 0; i < _requestListeners.length; i++) { 162 ServletRequestEvent event = new ServletRequestEvent (_app, request); 163 164 _requestListeners[i].requestInitialized(event); 165 } 166 167 _next.doFilter(request, response); 168 } catch (Throwable 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 event = new ServletRequestEvent (_app, request); 176 177 _requestListeners[i].requestDestroyed(event); 178 } catch (Throwable 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 e) { 189 _app.log(e.getMessage(), e); 190 } 191 } 192 193 try { 194 if (_accessLog != null) { 195 _accessLog.log((HttpServletRequest ) request, 196 (HttpServletResponse ) response, 197 _app); 198 } 199 } catch (Throwable e) { 200 log.log(Level.FINE, e.toString(), e); 201 } 202 203 if (request instanceof AbstractHttpRequest) 205 ((AbstractHttpRequest) request).finish(); 206 207 thread.setContextClassLoader(oldLoader); 208 } 209 } 210 } 211 | Popular Tags |