1 18 package org.apache.beehive.netui.pageflow.scoping.internal; 19 20 21 import javax.servlet.RequestDispatcher ; 22 import javax.servlet.ServletRequest ; 23 import javax.servlet.ServletResponse ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import java.io.IOException ; 28 29 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils; 30 import org.apache.log4j.Logger; 31 32 33 41 public class ScopedRequestDispatcher 42 implements RequestDispatcher 43 { 44 private String _uri; 45 46 private static final String REQUEST_URI_INCLUDE = "javax.servlet.include.request_uri"; 47 48 private static final Logger logger = Logger.getLogger( ScopedRequestDispatcher.class ); 49 50 51 56 public ScopedRequestDispatcher( String uri ) 57 { 58 _uri = uri; 59 } 60 61 68 public void forward( ServletRequest request, ServletResponse response ) 69 throws ServletException , IOException 70 { 71 ScopedRequestImpl scopedRequest = ( ScopedRequestImpl ) ScopedServletUtils.unwrapRequest( request ); 72 assert scopedRequest != null : request.getClass().getName(); 73 scopedRequest.setForwardedURI( _uri ); 74 scopedRequest.doForward(); 75 } 76 77 83 public void include( ServletRequest request, ServletResponse response ) 84 throws ServletException , IOException 85 { 86 assert request instanceof HttpServletRequest : request.getClass().getName(); 87 HttpServletRequest httpRequest = ( HttpServletRequest ) request; 88 89 HttpServletRequest outerRequest = ScopedServletUtils.getOuterRequest( httpRequest ); 93 94 outerRequest.setAttribute( REQUEST_URI_INCLUDE, httpRequest.getRequestURI()); 100 101 if ( logger.isDebugEnabled() ) 102 { 103 logger.debug( "Delegating to RequestDispatcher for URI " + _uri ); 104 } 105 106 try 107 { 108 RequestDispatcher realDispatcher = outerRequest.getRequestDispatcher( _uri ); 109 110 if ( realDispatcher == null ) 111 { 112 assert response instanceof HttpServletResponse : response.getClass().getName(); 113 ( ( HttpServletResponse ) response ).setStatus( HttpServletResponse.SC_NOT_FOUND ); 114 logger.error( "Could not get RequestDispatcher for URI " + _uri ); 115 } 116 else 117 { 118 realDispatcher.include( request, response ); 119 } 120 } 121 catch ( ServletException e ) 122 { 123 logger.error( "Exception during RequestDispatcher.include().", e.getRootCause() ); 124 125 throw e; 126 } 127 } 128 } 129 | Popular Tags |