1 18 package org.apache.beehive.netui.pageflow; 19 20 import javax.servlet.Filter ; 21 import javax.servlet.FilterConfig ; 22 import javax.servlet.ServletException ; 23 import javax.servlet.ServletRequest ; 24 import javax.servlet.ServletResponse ; 25 import javax.servlet.FilterChain ; 26 import javax.servlet.ServletContext ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 import java.io.IOException ; 30 import java.util.Set ; 31 import java.util.Map ; 32 33 import org.apache.struts.util.RequestUtils; 34 import org.apache.struts.util.MessageResources; 35 import org.apache.struts.action.ActionServlet; 36 import org.apache.struts.action.ActionMapping; 37 import org.apache.struts.action.ActionForm; 38 import org.apache.struts.action.ActionForward; 39 import org.apache.struts.config.ModuleConfig; 40 import org.apache.struts.Globals; 41 42 import org.apache.beehive.netui.core.urls.URLRewriterService; 43 import org.apache.beehive.netui.util.logging.Logger; 44 import org.apache.beehive.netui.util.internal.FileUtils; 45 import org.apache.beehive.netui.util.internal.ServletUtils; 46 import org.apache.beehive.netui.pageflow.internal.DefaultURLRewriter; 47 import org.apache.beehive.netui.pageflow.internal.JavaControlUtils; 48 import org.apache.beehive.netui.pageflow.internal.InternalUtils; 49 import org.apache.beehive.netui.pageflow.internal.InternalConstants; 50 import org.apache.beehive.netui.pageflow.internal.AdapterManager; 51 import org.apache.beehive.netui.pageflow.internal.PageFlowRequestWrapper; 52 import org.apache.beehive.netui.script.common.ImplicitObjectUtil; 53 54 55 58 public abstract class PageFlowPageFilter implements Filter 59 { 60 private ServletContext _servletContext; 61 private ServletContainerAdapter _servletContainerAdapter; 62 private FlowControllerFactory _flowControllerFactory; 63 64 private static final Logger _log = Logger.getInstance( PageFlowPageFilter.class ); 65 66 private static final String PREVENT_CACHE_ATTR = InternalConstants.ATTR_PREFIX + "preventCache"; 67 68 69 protected PageFlowPageFilter() 70 { 71 } 72 73 PageFlowPageFilter( ServletContext servletContext ) 74 { 75 _servletContext = servletContext; 76 _servletContainerAdapter = AdapterManager.getServletContainerAdapter( _servletContext ); 77 _flowControllerFactory = FlowControllerFactory.get( servletContext ); 78 } 79 80 public void init( FilterConfig filterConfig ) throws ServletException 81 { 82 _servletContext = filterConfig.getServletContext(); 83 84 if ( ! PageFlowContextListener.isInit( _servletContext ) ) 85 { 86 PageFlowContextListener.performInitializations( _servletContext ); 87 } 88 89 _servletContainerAdapter = AdapterManager.getServletContainerAdapter( _servletContext ); 90 _flowControllerFactory = FlowControllerFactory.get( _servletContext ); 91 } 92 93 protected abstract Set getValidFileExtensions(); 94 95 public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) 96 throws IOException , ServletException 97 { 98 if ( request instanceof HttpServletRequest && response instanceof HttpServletResponse ) 99 { 100 HttpServletRequest httpRequest = ( HttpServletRequest ) request; 101 HttpServletResponse httpResponse = ( HttpServletResponse ) response; 102 103 Object errStatusCode = request.getAttribute( "javax.servlet.error.status_code" ); 107 if ( errStatusCode != null ) 108 { 109 if ( _log.isDebugEnabled() ) 110 { 111 _log.debug( "Request has error status code " + errStatusCode + ". Skipping filter." ); 112 } 113 114 chain.doFilter( request, response ); 115 return; 116 } 117 118 String servletPath = InternalUtils.getDecodedServletPath( httpRequest ); 119 String extension = FileUtils.getFileExtension( servletPath ); 120 Set validFileExtensions = getValidFileExtensions(); 121 122 if ( validFileExtensions != null && ! validFileExtensions.contains( extension ) ) 123 { 124 if ( _log.isDebugEnabled() ) 125 { 126 _log.debug( "Path " + servletPath + 127 " does not have an appropriate file extension. Skipping filter." ); 128 } 129 130 chain.doFilter( request, response ); 131 return; 132 } 133 134 if ( _log.isDebugEnabled() ) _log.debug( "Filtering request for path " + servletPath ); 135 136 if ( request.getAttribute( PREVENT_CACHE_ATTR ) != null ) ServletUtils.preventCache( httpResponse ); 141 142 InternalUtils.setServletContext( httpRequest, _servletContext ); 147 148 PageFlowEventReporter er = _servletContainerAdapter.getEventReporter(); 152 _servletContainerAdapter.beginRequest( httpRequest, httpResponse ); 153 er.beginPageRequest( httpRequest, httpResponse ); 154 long startTime = System.currentTimeMillis(); 155 156 JavaControlUtils.initializeControlContext( httpRequest, httpResponse, _servletContext ); 160 161 URLRewriterService.registerURLRewriter( 0, request, new DefaultURLRewriter() ); 165 166 try 167 { 168 ModuleConfig prevModuleConfig = RequestUtils.getRequestModuleConfig( httpRequest ); 169 MessageResources prevMessageResources = ( MessageResources ) request.getAttribute( Globals.MESSAGES_KEY ); 170 initializeModule( httpRequest, httpResponse ); 171 172 try 173 { 174 RequestContext requestContext = new RequestContext( request, response ); 178 Map sharedFlows = 179 _flowControllerFactory.getSharedFlowsForRequest( requestContext ); 180 ImplicitObjectUtil.loadSharedFlow( request, sharedFlows ); 181 ImplicitObjectUtil.loadGlobalApp( request, PageFlowUtils.getGlobalApp( httpRequest ) ); 182 183 PageFlowController curJpf = _flowControllerFactory.getPageFlowForRequest( requestContext ); 187 188 if ( curJpf == null ) 193 { 194 InternalUtils.setCurrentModule( prevModuleConfig, request ); 195 request.setAttribute( Globals.MESSAGES_KEY, prevMessageResources ); 196 } 197 198 199 if ( _log.isDebugEnabled() ) 200 { 201 _log.debug( "Current PageFlowController is: " + curJpf ); 202 _log.debug( "Continuing with filter chain..." ); 203 } 204 205 runPage( curJpf, httpRequest, httpResponse, chain ); 206 } 207 catch ( ClassNotFoundException e ) 208 { 209 throw new ServletException ( e ); 210 } 211 catch ( InstantiationException e ) 212 { 213 throw new ServletException ( e ); 214 } 215 catch ( IllegalAccessException e ) 216 { 217 throw new ServletException ( e ); 218 } 219 } 220 finally 221 { 222 JavaControlUtils.uninitializeControlContext( httpRequest, httpResponse, _servletContext ); 226 227 _servletContainerAdapter.endRequest( httpRequest, httpResponse ); 231 long timeTaken = System.currentTimeMillis() - startTime; 232 er.endPageRequest( httpRequest, httpResponse, timeTaken ); 233 } 234 } 235 else 236 { 237 chain.doFilter( request, response ); 238 } 239 } 240 241 private void runPage( PageFlowController curJpf, HttpServletRequest request, HttpServletResponse response, 242 FilterChain chain ) 243 throws IOException , ServletException 244 { 245 if ( curJpf != null ) 251 { 252 if ( curJpf.incrementRequestCount( request, response, _servletContext ) ) 258 { 259 try 260 { 261 synchronized ( curJpf ) 267 { 268 FlowController.PerRequestState newState = 269 new FlowController.PerRequestState( request, response, null ); 270 FlowController.PerRequestState prevState = curJpf.setPerRequestState( newState ); 271 ImplicitObjectUtil.loadImplicitObjects( request, response, _servletContext, curJpf ); 272 273 curJpf.beforePage(); 278 279 try 280 { 281 chain.doFilter( request, response ); 282 } 283 catch ( ServletException servletEx ) 284 { 285 if ( ! handleException( servletEx, curJpf, request, response ) ) throw servletEx; 289 } 290 catch ( IOException ioe ) 291 { 292 if ( ! handleException( ioe, curJpf, request, response ) ) throw ioe; 296 } 297 catch ( Throwable th ) 298 { 299 if ( ! handleException( th, curJpf, request, response ) ) throw new ServletException ( th ); 303 } 304 finally 305 { 306 curJpf.setPerRequestState( prevState ); 307 } 308 } 309 } 310 finally 311 { 312 curJpf.decrementRequestCount( request ); 313 } 314 } 315 } 316 else 317 { 318 ImplicitObjectUtil.loadImplicitObjects( request, response, _servletContext, null ); 319 320 if ( request instanceof PageFlowRequestWrapper ) 324 { 325 request = ( ( PageFlowRequestWrapper ) request ).getHttpRequest(); 326 } 327 328 chain.doFilter( request, response ); 329 } 330 } 331 332 private boolean handleException( Throwable th, FlowController fc, HttpServletRequest request, 333 HttpServletResponse response ) 334 { 335 try 336 { 337 ActionMapping mapping = InternalUtils.getCurrentActionMapping( request ); 338 ActionForm form = InternalUtils.getCurrentActionForm( request ); 339 ActionForward fwd = fc.handleException( th, mapping, form, request, response ); 340 fc.getRequestProcessor().doActionForward( request, response, fwd ); 341 return true; 342 } 343 catch ( Throwable t ) 344 { 345 _log.error( "Exception while handling exception " + th.getClass().getName() 346 + ". The original exception will be thrown.", th ); 347 return false; 348 } 349 } 350 351 private void initializeModule( HttpServletRequest request, HttpServletResponse response ) 352 throws IOException , ServletException 353 { 354 String curModulePath = PageFlowUtils.getModulePath( request ); 358 ActionServlet as = InternalUtils.getActionServlet( _servletContext ); 359 360 if ( as instanceof AutoRegisterActionServlet ) 361 { 362 AutoRegisterActionServlet das = ( AutoRegisterActionServlet ) as; 363 das.ensureModuleRegistered( curModulePath, request ); 364 } 365 366 ModuleConfig mc = InternalUtils.selectModule( curModulePath, request, _servletContext ); 367 368 if ( mc == null ) 369 { 370 if ( as instanceof AutoRegisterActionServlet ) 374 { 375 AutoRegisterActionServlet das = ( AutoRegisterActionServlet ) as; 376 das.ensureModuleSelected( curModulePath, request, response ); 377 } 378 } 379 } 380 381 386 static void preventCache( HttpServletRequest request ) 387 { 388 request.setAttribute( PREVENT_CACHE_ATTR, Boolean.TRUE ); 389 } 390 391 public void destroy() 392 { 393 _servletContext = null; 394 } 395 } 396 | Popular Tags |