1 16 package org.apache.myfaces.application.jsp; 17 18 import org.apache.myfaces.application.MyfacesStateManager; 19 import org.apache.myfaces.application.TreeStructureManager; 20 import org.apache.myfaces.renderkit.MyfacesResponseStateManager; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import javax.faces.FactoryFinder; 26 import javax.faces.application.StateManager; 27 import javax.faces.component.UIViewRoot; 28 import javax.faces.context.ExternalContext; 29 import javax.faces.context.FacesContext; 30 import javax.faces.render.RenderKit; 31 import javax.faces.render.RenderKitFactory; 32 import javax.faces.render.ResponseStateManager; 33 import java.io.IOException ; 34 35 36 81 public class JspStateManagerImpl 82 extends MyfacesStateManager 83 { 84 private static final Log log = LogFactory.getLog(JspStateManagerImpl.class); 85 private static final String SERIALIZED_VIEW_SESSION_ATTR 86 = JspStateManagerImpl.class.getName() + ".SERIALIZED_VIEW"; 87 private static final String SERIALIZED_VIEW_REQUEST_ATTR 88 = JspStateManagerImpl.class.getName() + ".SERIALIZED_VIEW"; 89 90 private RenderKitFactory _renderKitFactory = null; 91 92 public JspStateManagerImpl() 93 { 94 if (log.isTraceEnabled()) log.trace("New JspStateManagerImpl instance created"); 95 } 96 97 98 protected Object getComponentStateToSave(FacesContext facesContext) 99 { 100 UIViewRoot viewRoot = facesContext.getViewRoot(); 101 if (viewRoot.isTransient()) 102 { 103 return null; 104 } 105 106 Object serializedComponentStates = viewRoot.processSaveState(facesContext); 107 return serializedComponentStates; 109 } 110 111 protected Object getTreeStructureToSave(FacesContext facesContext) 112 { 113 UIViewRoot viewRoot = facesContext.getViewRoot(); 114 if (viewRoot.isTransient()) 115 { 116 return null; 117 } 118 TreeStructureManager tsm = new TreeStructureManager(); 119 return tsm.buildTreeStructureToSave(viewRoot); 120 } 121 122 protected void restoreComponentState(FacesContext facesContext, 123 UIViewRoot uiViewRoot, 124 String renderKitId) 125 { 126 Object serializedComponentStates; 127 if (isSavingStateInClient(facesContext)) 128 { 129 RenderKit renderKit = getRenderKitFactory().getRenderKit(facesContext, renderKitId); 130 ResponseStateManager responseStateManager = renderKit.getResponseStateManager(); 131 serializedComponentStates = responseStateManager.getComponentStateToRestore(facesContext); 132 if (serializedComponentStates == null) 133 { 134 log.error("No serialized component state found in client request!"); 135 uiViewRoot.setViewId(null); 137 return; 138 } 139 } 140 else 141 { 142 SerializedView serializedView = getSerializedViewFromServletSession(facesContext.getExternalContext(), 143 uiViewRoot.getViewId()); 144 if (serializedView == null) 145 { 146 log.error("No serialized view found in server session!"); 147 uiViewRoot.setViewId(null); 149 return; 150 } 151 serializedComponentStates = serializedView.getState(); 152 if (serializedComponentStates == null) 153 { 154 log.error("No serialized component state found in server session!"); 155 return; 156 } 157 } 158 159 if (uiViewRoot.getRenderKitId() == null) 160 { 161 uiViewRoot.setRenderKitId(renderKitId); 163 } 164 165 uiViewRoot.processRestoreState(facesContext, serializedComponentStates); 166 } 167 168 protected UIViewRoot restoreTreeStructure(FacesContext facesContext, 169 String viewId, 170 String renderKitId) 171 { 172 UIViewRoot uiViewRoot = null; 173 if (isSavingStateInClient(facesContext)) 174 { 175 RenderKit rk = getRenderKitFactory().getRenderKit(facesContext, renderKitId); 177 ResponseStateManager responseStateManager = rk.getResponseStateManager(); 178 Object treeStructure = responseStateManager.getTreeStructureToRestore(facesContext, viewId); 179 if (treeStructure == null) 180 { 181 if (log.isDebugEnabled()) log.debug("No tree structure state found in client request"); 182 return null; 183 } 184 185 TreeStructureManager tsm = new TreeStructureManager(); 186 uiViewRoot = tsm.restoreTreeStructure((TreeStructureManager.TreeStructComponent)treeStructure); 187 if (log.isTraceEnabled()) log.trace("Tree structure restored from client request"); 188 } 189 else 190 { 191 SerializedView serializedView = getSerializedViewFromServletSession(facesContext.getExternalContext(), 193 viewId); 194 if (serializedView == null) 195 { 196 if (log.isDebugEnabled()) log.debug("No serialized view found in server session!"); 197 return null; 198 } 199 200 Object treeStructure = serializedView.getStructure(); 201 if (treeStructure == null) 202 { 203 if (log.isDebugEnabled()) log.debug("No tree structure state found in server session, former UIViewRoot must have been transient"); 204 return null; 205 } 206 207 TreeStructureManager tsm = new TreeStructureManager(); 208 uiViewRoot = tsm.restoreTreeStructure((TreeStructureManager.TreeStructComponent)serializedView.getStructure()); 209 if (log.isTraceEnabled()) log.trace("Tree structure restored from server session"); 210 } 211 212 return uiViewRoot; 213 } 214 215 public UIViewRoot restoreView(FacesContext facescontext, String viewId, String renderKitId) 216 { 217 UIViewRoot uiViewRoot = restoreTreeStructure(facescontext, viewId, renderKitId); 218 if (uiViewRoot != null) 219 { 220 uiViewRoot.setViewId(viewId); 221 restoreComponentState(facescontext, uiViewRoot, renderKitId); 222 String restoredViewId = uiViewRoot.getViewId(); 223 if (restoredViewId == null || !(restoredViewId.equals(viewId))) 224 { 225 return null; 226 } 227 228 if (!isSavingStateInClient(facescontext)) 229 { 230 removeSerializedViewFromServletSession(facescontext.getExternalContext(), viewId); 231 } 232 } 233 return uiViewRoot; 234 } 235 236 public SerializedView saveSerializedView(FacesContext facesContext) throws IllegalStateException 237 { 238 240 ExternalContext externalContext = facesContext.getExternalContext(); 241 242 SerializedView serializedView = (SerializedView)externalContext.getRequestMap() 244 .get(SERIALIZED_VIEW_REQUEST_ATTR); 245 if (serializedView == null) 246 { 247 Object treeStruct = getTreeStructureToSave(facesContext); 249 Object compStates = getComponentStateToSave(facesContext); 250 serializedView = new StateManager.SerializedView(treeStruct, compStates); 251 externalContext.getRequestMap().put(SERIALIZED_VIEW_REQUEST_ATTR, 252 serializedView); 253 } 254 255 if (isSavingStateInClient(facesContext)) 256 { 257 return serializedView; 258 } 259 else 260 { 261 saveSerializedViewInServletSession(externalContext, 263 facesContext.getViewRoot().getViewId(), 264 serializedView); 265 return null; } 267 } 268 269 public void writeState(FacesContext facesContext, 270 SerializedView serializedView) throws IOException 271 { 272 if (!isSavingStateInClient(facesContext)) 273 { 274 throw new IllegalStateException ("Must not be called in server state saving mode"); 275 } 276 277 UIViewRoot uiViewRoot = facesContext.getViewRoot(); 278 RenderKit renderKit = getRenderKitFactory().getRenderKit(facesContext, uiViewRoot.getRenderKitId()); 280 renderKit.getResponseStateManager().writeState(facesContext, serializedView); 281 } 282 283 289 public void writeStateAsUrlParams(FacesContext facesContext, 290 SerializedView serializedView) throws IOException 291 { 292 if (!isSavingStateInClient(facesContext)) 293 { 294 throw new IllegalStateException ("Must not be called in server state saving mode"); 295 } 296 297 UIViewRoot uiViewRoot = facesContext.getViewRoot(); 298 RenderKit renderKit = getRenderKitFactory().getRenderKit(facesContext, uiViewRoot.getRenderKitId()); 300 ResponseStateManager responseStateManager = renderKit.getResponseStateManager(); 301 if (responseStateManager instanceof MyfacesResponseStateManager) 302 { 303 ((MyfacesResponseStateManager)responseStateManager).writeStateAsUrlParams(facesContext, 304 serializedView); 305 } 306 else 307 { 308 log.error("ResponseStateManager of render kit " + uiViewRoot.getRenderKitId() + " is no MyfacesResponseStateManager and does not support saving state in url parameters."); 309 } 310 } 311 312 313 314 316 protected RenderKitFactory getRenderKitFactory() 317 { 318 if (_renderKitFactory == null) 319 { 320 _renderKitFactory = (RenderKitFactory)FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); 321 } 322 return _renderKitFactory; 323 } 324 325 protected void saveSerializedViewInServletSession(ExternalContext externalContext, 326 String viewId, 327 SerializedView serializedView) 328 { 329 Object sv[] = new Object [] {serializedView.getStructure(), serializedView.getState()}; 332 externalContext.getSessionMap().put(SERIALIZED_VIEW_SESSION_ATTR + "-" + viewId, sv); 333 } 334 335 protected SerializedView getSerializedViewFromServletSession(ExternalContext externalContext, 336 String viewId) 337 { 338 String key = SERIALIZED_VIEW_SESSION_ATTR + "-" + viewId; 339 if (externalContext.getSessionMap().get(key) == null) 340 { 341 return null; } 343 344 Object sv[] = (Object [])externalContext.getSessionMap().get(key); 345 return new SerializedView(sv[0], sv[1]); 346 } 347 348 protected void removeSerializedViewFromServletSession(ExternalContext externalContext, String viewId) 349 { 350 externalContext.getSessionMap().remove(SERIALIZED_VIEW_SESSION_ATTR + "-" + viewId); 351 } 352 353 366 367 368 393 } 394 | Popular Tags |