1 16 package org.mortbay.jetty.servlet; 17 18 import java.io.IOException ; 19 20 import javax.servlet.Filter ; 21 import javax.servlet.FilterChain ; 22 import javax.servlet.FilterConfig ; 23 import javax.servlet.ServletContext ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.ServletRequest ; 26 import javax.servlet.ServletRequestAttributeEvent ; 27 import javax.servlet.ServletRequestAttributeListener ; 28 import javax.servlet.ServletRequestEvent ; 29 import javax.servlet.ServletRequestListener ; 30 import javax.servlet.ServletResponse ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletRequestWrapper ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.http.HttpServletResponseWrapper ; 35 36 import org.mortbay.util.LazyList; 37 38 39 55 public class JSR154Filter implements Filter 56 { 57 private static ThreadLocal __states=new ThreadLocal (); 58 private ServletContext _servletContext; 59 private Object _requestListeners; 60 private Object _requestAttributeListeners; 61 private boolean _unwrappedDispatchSupported; 62 63 64 public void init(FilterConfig filterConfig) 65 throws ServletException 66 { 67 _servletContext=filterConfig.getServletContext(); 68 _unwrappedDispatchSupported=Boolean.valueOf(filterConfig.getInitParameter("unwrappedDispatch")).booleanValue(); 69 } 70 71 72 protected void setRequestAttributeListeners(Object list) 73 { 74 _requestAttributeListeners=list; 75 } 76 77 78 protected void setRequestListeners(Object list) 79 { 80 _requestListeners=list; 81 } 82 83 84 public boolean isUnwrappedDispatchSupported() 85 { 86 return _unwrappedDispatchSupported; 87 } 88 89 90 93 public void setUnwrappedDispatchSupported(boolean supportUnwrappedDispatch) 94 { 95 _unwrappedDispatchSupported = supportUnwrappedDispatch; 96 } 97 98 99 public void setDispatch(Dispatcher.DispatcherRequest request, Dispatcher.DispatcherResponse response) 100 { 101 ThreadState state=state(); 102 state.dispatchRequest=request; 103 state.dispatchResponse=response; 104 } 105 106 107 public Dispatcher.DispatcherRequest getDispatchRequest() 108 { 109 ThreadState state=state(); 110 return state.dispatchRequest; 111 } 112 113 114 public void doFilter(ServletRequest request, 115 ServletResponse response, 116 FilterChain chain) 117 throws IOException , ServletException 118 { 119 HttpServletRequest srequest = (HttpServletRequest )request; 120 HttpServletResponse sresponse = (HttpServletResponse )response; 121 Request requestWrapper=null; 122 Response responseWrapper=null; 123 boolean root_filter=false; 124 125 126 ThreadState state = state(); 128 if (_unwrappedDispatchSupported || LazyList.size(_requestAttributeListeners)>0 ) 129 { 130 if (srequest instanceof ServletHttpRequest) 131 { 132 request=state.rootRequest=requestWrapper=new Request (srequest); 133 response=state.rootResponse=responseWrapper=new Response (sresponse); 134 root_filter=true; 135 } 136 else 137 { 138 requestWrapper=state.rootRequest; 139 responseWrapper=state.rootResponse; 140 } 141 } 142 143 boolean first_in_context=root_filter || 145 requestWrapper!=null && 146 requestWrapper.getRequest()!=null && requestWrapper.getRequest() instanceof Dispatcher.DispatcherRequest && 147 ((Dispatcher.DispatcherRequest)requestWrapper.getRequest()).crossContext(); 148 149 if (first_in_context) 150 { 151 requestInitialized(request); 152 153 if (requestWrapper!=null && LazyList.size(_requestAttributeListeners)>0) 154 requestWrapper.addContextFilter(this); 155 } 156 157 boolean dispatch=false; 159 if (_unwrappedDispatchSupported && state.dispatchRequest!=null) 160 { 161 dispatch=true; 162 requestWrapper.pushWrapper(state.dispatchRequest); 163 responseWrapper.pushWrapper(state.dispatchResponse); 164 state.dispatchRequest=null; 165 state.dispatchResponse=null; 166 } 167 168 try 169 { 170 chain.doFilter(request, response); 171 } 172 finally 173 { 174 if (first_in_context) 175 { 176 requestDestroyed(request); 177 if (requestWrapper!=null && LazyList.size(_requestAttributeListeners)>0) 178 requestWrapper.delContextFilter(this); 179 } 180 181 if (root_filter) 182 state.clear(); 183 184 if (dispatch) 185 { 186 requestWrapper.popWrapper(); 187 responseWrapper.popWrapper(); 188 } 189 } 190 } 191 192 193 public void destroy() 194 { 195 } 196 197 198 private void requestInitialized(ServletRequest request) 199 { 200 ServletRequestEvent event = new ServletRequestEvent (_servletContext,request); 201 for (int i=0;i<LazyList.size(_requestListeners);i++) 202 ((ServletRequestListener )LazyList.get(_requestListeners,i)) 203 .requestInitialized(event); 204 } 205 206 207 private void requestDestroyed(ServletRequest request) 208 { 209 ServletRequestEvent event = new ServletRequestEvent (_servletContext,request); 210 for (int i=LazyList.size(_requestListeners);i-->0;) 211 ((ServletRequestListener )LazyList.get(_requestListeners,i)) 212 .requestDestroyed(event); 213 } 214 215 216 private void attributeNotify(ServletRequest request,String name,Object oldValue,Object newValue) 217 { 218 ServletRequestAttributeEvent event = 219 new ServletRequestAttributeEvent (_servletContext,request,name,oldValue==null?newValue:oldValue); 220 for (int i=0;i<LazyList.size(_requestAttributeListeners);i++) 221 { 222 ServletRequestAttributeListener listener = 223 ((ServletRequestAttributeListener )LazyList.get(_requestAttributeListeners,i)); 224 if (oldValue==null) 225 listener.attributeAdded(event); 226 else if (newValue==null) 227 listener.attributeRemoved(event); 228 else 229 listener.attributeReplaced(event); 230 } 231 } 232 233 234 private static ThreadState state() 235 { 236 ThreadState state=(ThreadState)__states.get(); 237 if (state==null) 238 { 239 state=new ThreadState(); 240 __states.set(state); 241 } 242 return state; 243 } 244 245 246 247 248 private static class ThreadState 249 { 250 Request rootRequest; 251 Response rootResponse; 252 Dispatcher.DispatcherRequest dispatchRequest; 253 Dispatcher.DispatcherResponse dispatchResponse; 254 255 void clear() 256 { 257 rootRequest=null; 258 rootResponse=null; 259 dispatchRequest=null; 260 dispatchResponse=null; 261 } 262 } 263 264 265 266 267 private static class Request extends HttpServletRequestWrapper 268 { 269 Object contextFilters; 270 271 272 Request(HttpServletRequest httpServletRequest) 273 { 274 super(httpServletRequest); 275 } 276 277 278 279 282 public void delContextFilter(JSR154Filter filter) 283 { 284 contextFilters=LazyList.remove(contextFilters,filter); 285 } 286 287 288 289 292 public void addContextFilter(JSR154Filter filter) 293 { 294 contextFilters=LazyList.add(contextFilters,filter); 295 } 296 297 298 public void setAttribute(String name, Object value) 299 { 300 Object old=getAttribute(name); 301 super.setAttribute(name,value); 302 for (int i=LazyList.size(contextFilters);i-->0;) 303 ((JSR154Filter)LazyList.get(contextFilters, i)).attributeNotify(this,name,old,value); 304 } 305 306 307 public void removeAttribute(String name) 308 { 309 Object old=getAttribute(name); 310 super.removeAttribute(name); 311 for (int i=LazyList.size(contextFilters);i-->0;) 312 ((JSR154Filter)LazyList.get(contextFilters, i)).attributeNotify(this,name,old,null); 313 } 314 315 316 public void pushWrapper(HttpServletRequestWrapper wrapper) 317 { 318 wrapper.setRequest(getRequest()); 319 setRequest(wrapper); 320 } 321 322 323 public void popWrapper() 324 { 325 HttpServletRequestWrapper wrapper=(HttpServletRequestWrapper )getRequest(); 326 HttpServletRequest request=(HttpServletRequest )wrapper.getRequest(); 327 setRequest(request); 328 } 329 } 330 331 332 333 334 private static class Response extends HttpServletResponseWrapper 335 { 336 337 Response(HttpServletResponse httpServletResponse) 338 { 339 super(httpServletResponse); 340 } 341 342 343 public void pushWrapper(HttpServletResponseWrapper wrapper) 344 { 345 wrapper.setResponse(getResponse()); 346 setResponse(wrapper); 347 } 348 349 350 public void popWrapper() 351 { 352 HttpServletResponseWrapper wrapper=(HttpServletResponseWrapper )getResponse(); 353 HttpServletResponse response=(HttpServletResponse )wrapper.getResponse(); 354 setResponse(response); 355 } 356 } 357 358 } 359 360 | Popular Tags |