1 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 ; 33 import java.util.Locale ; 34 import java.io.IOException ; 35 import java.io.Serializable ; 36 37 38 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 calculateLocale(FacesContext context) 64 { 65 return _delegate.calculateLocale( context ); 66 } 67 68 public String calculateRenderKitId(FacesContext context) 69 { 70 return _delegate.calculateRenderKitId( context ); 71 } 72 73 private static class PageClientState implements Serializable 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 viewId) 96 { 97 Object request = context.getExternalContext().getRequest(); 98 HttpServletRequest httpRequest = null; 99 100 if ( request instanceof HttpServletRequest ) 105 { 106 httpRequest = ( HttpServletRequest ) 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 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 getActionURL(FacesContext context, String viewId) 133 { 134 return _delegate.getActionURL( context, viewId ); 135 } 136 137 public String getResourceURL(FacesContext context, String path) 138 { 139 return _delegate.getResourceURL( context, path ); 140 } 141 142 public void renderView(FacesContext context, UIViewRoot viewToRender) 143 throws IOException , FacesException 144 { 145 _delegate.renderView( context, viewToRender ); 146 } 147 148 152 public UIViewRoot restoreView(FacesContext context, String viewId) 153 { 154 Object request = context.getExternalContext().getRequest(); 155 HttpServletRequest httpRequest = null; 156 157 if ( request instanceof HttpServletRequest ) 158 { 159 httpRequest = ( HttpServletRequest ) request; 160 161 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 request, String viewID, UIViewRoot viewRoot ) 176 { 177 if ( request != null ) 181 { 182 PageFlowController curPageFlow = PageFlowUtils.getCurrentPageFlow( request ); 183 184 if ( curPageFlow != null && ! curPageFlow.isPreviousPageInfoDisabled() ) 185 { 186 viewID = FileUtils.stripFileExtension( viewID ); 192 String 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 204 { 205 _delegate.writeState( context ); 206 } 207 } 208 | Popular Tags |