KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > faces > internal > PageFlowNavigationHandler


1 /*
2 * Copyright 2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * $Header:$
17 */

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 JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26
27 import java.io.IOException JavaDoc;
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 /**
39  * Internal class used in JSF/Page Flow integration. This NavigationHandler raises Page Flow actions for JSF pages
40  * that are in Page Flow directories.
41  * @see org.apache.beehive.netui.pageflow.faces.PageFlowApplicationFactory
42  */

43 public class PageFlowNavigationHandler
44     extends NavigationHandler
45 {
46     private static final Logger _log = Logger.getInstance( PageFlowNavigationHandler.class );
47     static final String JavaDoc 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 JavaDoc fromAction, String JavaDoc outcome )
63     {
64         Object JavaDoc request = context.getExternalContext().getRequest();
65         Object JavaDoc response = context.getExternalContext().getResponse();
66         Object JavaDoc extContext = context.getExternalContext().getContext();
67
68         if ( request instanceof HttpServletRequest JavaDoc && response instanceof HttpServletResponse JavaDoc
69             && extContext instanceof ServletContext JavaDoc )
70         {
71             HttpServletRequest JavaDoc httpRequest = ( HttpServletRequest JavaDoc ) request;
72             HttpServletResponse JavaDoc httpResponse = ( HttpServletResponse JavaDoc ) response;
73             
74             //
75
// If we're coming in on a forwarded request from this NavigationHandler, bail out; we don't want to
76
// forward again.
77
//
78
if ( httpRequest.getAttribute( ALREADY_FORWARDED_ATTR ) != null )
79             {
80                 httpRequest.removeAttribute( ALREADY_FORWARDED_ATTR );
81                 return;
82             }
83             
84             try
85             {
86                 ServletContext JavaDoc servletContext = ( ServletContext JavaDoc ) 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 JavaDoc 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 JavaDoc e )
110                         {
111                             _log.error( "Could not forward to " + actionURI, e );
112                         }
113                         catch ( ServletException JavaDoc e )
114                         {
115                             _log.error( "Could not forward to " + actionURI, e.getRootCause() );
116                         }
117                     }
118     
119                     return;
120                 }
121             }
122             catch ( InstantiationException JavaDoc e )
123             {
124                 _log.error( "Could not instantiate PageFlowController for request " + httpRequest.getRequestURI(), e );
125                 return;
126             }
127             catch ( IllegalAccessException JavaDoc e )
128             {
129                 _log.error( "Could not instantiate PageFlowController for request " + httpRequest.getRequestURI(), e );
130                 return;
131             }
132         }
133
134         // Fall back to base JSF navigation.
135
_baseHandler.handleNavigation( context, fromAction, outcome );
136     }
137 }
138
Popular Tags