1 18 package org.apache.beehive.netui.pageflow.faces.internal; 19 20 import javax.faces.application.NavigationHandler; 21 import javax.faces.context.FacesContext; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletException ; 26 27 import java.io.IOException ; 28 29 import org.apache.beehive.netui.pageflow.PageFlowController; 30 import org.apache.beehive.netui.pageflow.PageFlowUtils; 31 import org.apache.beehive.netui.pageflow.PageFlowConstants; 32 import org.apache.beehive.netui.pageflow.FlowControllerFactory; 33 import org.apache.beehive.netui.pageflow.RequestContext; 34 import org.apache.beehive.netui.pageflow.internal.InternalConstants; 35 import org.apache.beehive.netui.util.logging.Logger; 36 37 38 43 public class PageFlowNavigationHandler 44 extends NavigationHandler 45 { 46 private static final Logger _log = Logger.getInstance( PageFlowNavigationHandler.class ); 47 static final String ALREADY_FORWARDED_ATTR = InternalConstants.ATTR_PREFIX + "navHandled"; 48 49 private NavigationHandler _baseHandler; 50 51 52 public PageFlowNavigationHandler( NavigationHandler base ) 53 { 54 if ( _log.isDebugEnabled() ) 55 { 56 _log.debug( "Adapting NavigationHandler" + base ); 57 } 58 59 _baseHandler = base; 60 } 61 62 public void handleNavigation( FacesContext context, String fromAction, String outcome ) 63 { 64 Object request = context.getExternalContext().getRequest(); 65 Object response = context.getExternalContext().getResponse(); 66 Object extContext = context.getExternalContext().getContext(); 67 68 if ( request instanceof HttpServletRequest && response instanceof HttpServletResponse 69 && extContext instanceof ServletContext ) 70 { 71 HttpServletRequest httpRequest = ( HttpServletRequest ) request; 72 HttpServletResponse httpResponse = ( HttpServletResponse ) response; 73 74 if ( httpRequest.getAttribute( ALREADY_FORWARDED_ATTR ) != null ) 79 { 80 httpRequest.removeAttribute( ALREADY_FORWARDED_ATTR ); 81 return; 82 } 83 84 try 85 { 86 ServletContext servletContext = ( ServletContext ) extContext; 87 FlowControllerFactory fcFactory = FlowControllerFactory.get( servletContext ); 88 PageFlowController pfc = fcFactory.getPageFlowForRequest( new RequestContext( httpRequest, httpResponse ) ); 89 PageFlowUtils.getCurrentPageFlow( httpRequest ); 90 91 if ( pfc != null ) 92 { 93 if ( outcome != null ) 94 { 95 String actionURI = outcome + PageFlowConstants.ACTION_EXTENSION; 96 97 if ( _log.isDebugEnabled() ) 98 { 99 _log.debug( "Forwarding to " + actionURI ); 100 } 101 102 context.responseComplete(); 103 httpRequest.setAttribute( ALREADY_FORWARDED_ATTR, actionURI ); 104 105 try 106 { 107 httpRequest.getRequestDispatcher( actionURI ).forward( httpRequest, httpResponse ); 108 } 109 catch ( IOException e ) 110 { 111 _log.error( "Could not forward to " + actionURI, e ); 112 } 113 catch ( ServletException e ) 114 { 115 _log.error( "Could not forward to " + actionURI, e.getRootCause() ); 116 } 117 } 118 119 return; 120 } 121 } 122 catch ( InstantiationException e ) 123 { 124 _log.error( "Could not instantiate PageFlowController for request " + httpRequest.getRequestURI(), e ); 125 return; 126 } 127 catch ( IllegalAccessException e ) 128 { 129 _log.error( "Could not instantiate PageFlowController for request " + httpRequest.getRequestURI(), e ); 130 return; 131 } 132 } 133 134 _baseHandler.handleNavigation( context, fromAction, outcome ); 136 } 137 } 138 | Popular Tags |