KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.beehive.netui.pageflow.PageFlowUtils;
21 import org.apache.beehive.netui.pageflow.PageFlowController;
22 import org.apache.beehive.netui.pageflow.PreviousPageInfo;
23 import org.apache.beehive.netui.pageflow.FacesBackingBean;
24 import org.apache.beehive.netui.pageflow.internal.PageFlowRequestWrapper;
25 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
26 import org.apache.beehive.netui.util.internal.FileUtils;
27
28 import javax.faces.application.ViewHandler;
29 import javax.faces.context.FacesContext;
30 import javax.faces.component.UIViewRoot;
31 import javax.faces.FacesException;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import java.util.Locale JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.Serializable JavaDoc;
36
37
38 /**
39  * Internal class used in JSF/Page Flow integration. Delegates in all cases except:
40  * <ul>
41  * <li>
42  * {@link #restoreView}, which prevents view restoration if we're in a request forwarded by
43  * {@link PageFlowNavigationHandler}.
44  * </li>
45  * <li>
46  * {@link #createView}, which integrates with the "navigateTo" feature in Page Flow to save/restore the
47  * component tree.
48  * </li>
49  * </ul>
50  *
51  * @see org.apache.beehive.netui.pageflow.faces.PageFlowApplicationFactory
52  */

53 class PageFlowViewHandler
54         extends ViewHandler
55 {
56     private ViewHandler _delegate;
57
58     public PageFlowViewHandler( ViewHandler delegate )
59     {
60         _delegate = delegate;
61     }
62     
63     public Locale JavaDoc calculateLocale(FacesContext context)
64     {
65         return _delegate.calculateLocale( context );
66     }
67
68     public String JavaDoc calculateRenderKitId(FacesContext context)
69     {
70         return _delegate.calculateRenderKitId( context );
71     }
72
73     private static class PageClientState implements Serializable JavaDoc
74     {
75         private UIViewRoot _viewRoot;
76         private FacesBackingBean _backingBean;
77
78         public PageClientState( UIViewRoot viewRoot, FacesBackingBean backingBean )
79         {
80             _viewRoot = viewRoot;
81             _backingBean = backingBean;
82         }
83
84         public UIViewRoot getViewRoot()
85         {
86             return _viewRoot;
87         }
88
89         public FacesBackingBean getBackingBean()
90         {
91             return _backingBean;
92         }
93     }
94     
95     public UIViewRoot createView(FacesContext context, String JavaDoc viewId)
96     {
97         Object JavaDoc request = context.getExternalContext().getRequest();
98         HttpServletRequest JavaDoc httpRequest = null;
99         
100         //
101
// First, if this is a navigateTo=Jpf.NavigateTo.currentPage or a navigateTo=Jpf.NavigateTo.previousPage,
102
// see if we've saved view state from the original page. If so, just restore that.
103
//
104
if ( request instanceof HttpServletRequest JavaDoc )
105         {
106             httpRequest = ( HttpServletRequest JavaDoc ) request;
107             PageFlowRequestWrapper rw = PageFlowRequestWrapper.unwrap( httpRequest );
108
109             if ( rw != null )
110             {
111                 PreviousPageInfo prevPageInfo = rw.getPreviousPageInfo( true );
112                 
113                 if ( prevPageInfo != null )
114                 {
115                     Object JavaDoc clientState = prevPageInfo.getClientState();
116                     
117                     if ( clientState != null && clientState instanceof PageClientState )
118                     {
119                         PageClientState pcs = ( PageClientState ) clientState;
120                         InternalUtils.setFacesBackingBean( httpRequest, pcs.getBackingBean() );
121                         return pcs.getViewRoot();
122                     }
123                 }
124             }
125         }
126         
127         UIViewRoot viewRoot = _delegate.createView( context, viewId );
128         savePreviousPageInfo( httpRequest, viewId, viewRoot );
129         return viewRoot;
130     }
131
132     public String JavaDoc getActionURL(FacesContext context, String JavaDoc viewId)
133     {
134         return _delegate.getActionURL( context, viewId );
135     }
136
137     public String JavaDoc getResourceURL(FacesContext context, String JavaDoc path)
138     {
139         return _delegate.getResourceURL( context, path );
140     }
141
142     public void renderView(FacesContext context, UIViewRoot viewToRender)
143         throws IOException JavaDoc, FacesException
144     {
145         _delegate.renderView( context, viewToRender );
146     }
147
148     /**
149      * If we are in a request forwarded by {@link PageFlowNavigationHandler}, returns <code>null</code>; otherwise,
150      * delegates to the base ViewHandler.
151      */

152     public UIViewRoot restoreView(FacesContext context, String JavaDoc viewId)
153     {
154         Object JavaDoc request = context.getExternalContext().getRequest();
155         HttpServletRequest JavaDoc httpRequest = null;
156         
157         if ( request instanceof HttpServletRequest JavaDoc )
158         {
159             httpRequest = ( HttpServletRequest JavaDoc ) request;
160             
161             //
162
// If we did a forward in PageFlowNavigationHandler, don't try to restore the view.
163
//
164
if ( httpRequest.getAttribute( PageFlowNavigationHandler.ALREADY_FORWARDED_ATTR ) != null )
165             {
166                 return null;
167             }
168         }
169         
170         UIViewRoot viewRoot = _delegate.restoreView( context, viewId );
171         savePreviousPageInfo( httpRequest, viewId, viewRoot );
172         return viewRoot;
173     }
174
175     private static void savePreviousPageInfo( HttpServletRequest JavaDoc request, String JavaDoc viewID, UIViewRoot viewRoot )
176     {
177         //
178
// Save the current view state in the PreviousPageInfo structure of the current page flow.
179
//
180
if ( request != null )
181         {
182             PageFlowController curPageFlow = PageFlowUtils.getCurrentPageFlow( request );
183             
184             if ( curPageFlow != null && ! curPageFlow.isPreviousPageInfoDisabled() )
185             {
186                 //
187
// Only save the previous page info if the JSF view-ID is the same as the current forward path.
188
// Note that we strip the file extension from the view-ID -- different JSF implementations give
189
// us different things (foo.jsp vs. foo.faces).
190
//
191
viewID = FileUtils.stripFileExtension( viewID );
192                 String JavaDoc currentForwardPath = FileUtils.stripFileExtension( curPageFlow.getCurrentForwardPath() );
193                 if ( viewID.equals( currentForwardPath ) )
194                 {
195                     PreviousPageInfo prevPageInfo = curPageFlow.getCurrentPageInfo();
196                     FacesBackingBean backingBean = InternalUtils.getFacesBackingBean( request );
197                     prevPageInfo.setClientState( new PageClientState( viewRoot, backingBean ) );
198                 }
199             }
200         }
201     }
202     
203     public void writeState(FacesContext context) throws IOException JavaDoc
204     {
205         _delegate.writeState( context );
206     }
207 }
208
Popular Tags