You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ar...@apache.org on 2012/02/09 20:05:54 UTC

svn commit: r1242458 - /myfaces/trinidad/branches/1.2.12.6.2-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/application/StateManagerImpl.java

Author: arobinson74
Date: Thu Feb  9 19:05:53 2012
New Revision: 1242458

URL: http://svn.apache.org/viewvc?rev=1242458&view=rev
Log:
TRINIDAD-2206 - commit patch thanks to Gary VanMatre onto the 1.2.12.6.2 branch

Modified:
    myfaces/trinidad/branches/1.2.12.6.2-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/application/StateManagerImpl.java

Modified: myfaces/trinidad/branches/1.2.12.6.2-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/application/StateManagerImpl.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.12.6.2-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/application/StateManagerImpl.java?rev=1242458&r1=1242457&r2=1242458&view=diff
==============================================================================
--- myfaces/trinidad/branches/1.2.12.6.2-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/application/StateManagerImpl.java (original)
+++ myfaces/trinidad/branches/1.2.12.6.2-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/application/StateManagerImpl.java Thu Feb  9 19:05:53 2012
@@ -55,6 +55,7 @@ import org.apache.myfaces.trinidad.compo
 import org.apache.myfaces.trinidad.component.core.CoreDocument;
 import org.apache.myfaces.trinidad.context.RequestContext;
 import org.apache.myfaces.trinidad.context.Window;
+import org.apache.myfaces.trinidad.context.WindowManager;
 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
 import org.apache.myfaces.trinidad.util.ExternalContextUtils;
 import org.apache.myfaces.trinidadinternal.context.RequestContextImpl;
@@ -531,6 +532,44 @@ public class StateManagerImpl extends St
   {
     _delegate.writeState(context, state);
   }
+  /**
+   * Returns whether a token is a currently valid View State Token
+   * @param external The ExternalContext
+   * @param token    The state token to check for validity
+   * @return
+   */
+  public static boolean isValidViewStateToken(ExternalContext external, String token)
+  {
+    if ((token != null) && _calculateTokenStateSaving(external))
+    {
+      return (_getPageState(external, token) != null);
+    }
+    else
+    {
+      return false;
+    }
+  }
+
+
+  /**
+   * Returns the PageState for a state token
+   * @param external
+   * @param token
+   * @return
+   */
+  private static PageState _getPageState(ExternalContext external, String token)
+  {
+    // get view cache key with "." separator suffix to separate the SubKeyMap keys
+    String subkey = _getViewCacheKey(external,
+                                     RequestContext.getCurrentInstance(),
+                                     _SUBKEY_SEPARATOR);
+
+    Map<String, PageState> stateMap = new SubKeyMap<PageState>(
+                     external.getSessionMap(),
+                     subkey);
+    
+    return stateMap.get(token);
+  }
 
   @SuppressWarnings({"unchecked", "deprecation"})
   @Override
@@ -866,7 +905,11 @@ public class StateManagerImpl extends St
     String    prefix,
     Character suffix)
   {
-    Window currWindow = trinContext.getWindowManager().getCurrentWindow(eContext);
+    
+    Window currWindow = null;
+    // RequestContext.getWindowManager() uses the FacesContext
+    if (FacesContext.getCurrentInstance() != null)
+      currWindow = trinContext.getWindowManager().getCurrentWindow(eContext);
 
     // if we have a current window or a suffix, we need a StringBuilder to calculate the cache key
     if ((currWindow != null) || (suffix != null))
@@ -918,6 +961,75 @@ public class StateManagerImpl extends St
   }
 
   /**
+   * Returns <code>true</code> if we should use token state saving rather than client state
+   * saving
+   * @param external
+   * @return
+   * @see #_saveAsToken
+   */
+  private static boolean _calculateTokenStateSaving(ExternalContext external)
+  {
+    Map initParameters = external.getInitParameterMap();
+
+    Object stateSavingMethod = initParameters.get(StateManager.STATE_SAVING_METHOD_PARAM_NAME);
+
+    // on "SERVER" state-saving we return TRUE, since we want send down a token string.
+    if ((stateSavingMethod == null) ||
+        StateManager.STATE_SAVING_METHOD_SERVER.equalsIgnoreCase((String) stateSavingMethod))
+    {
+      return true;
+    }
+
+    // if the user set state-saving to "CLIENT" *and* the client-state-method to "ALL"
+    // we return FALSE, since we want to save the ENTIRE state on the client...
+    Object clientMethod = initParameters.get(CLIENT_STATE_METHOD_PARAM_NAME);
+
+    if ((clientMethod != null) &&
+        CLIENT_STATE_METHOD_ALL.equalsIgnoreCase((String) clientMethod))
+    {
+      return false;
+    }
+
+    // if the user has used the <document> 'stateSaving' attribute to specify
+    // client, we force the state mananger (see above) to render the entire
+    // state on the client. The indicator is stashed on the FacesContext and
+    // is therefore NOT visible during "restoreView" phase. So if we reach this point
+    // here AND we are using "full" client-side-state-saving, this has been tweaked
+    // on the previous page rendering phase...
+    // In this case we return FALSE to indicate to restore the entire (serialized)
+    // state from the client!
+    //
+    // @see setPerViewStateSaving()
+    String viewStateValue =
+                      external.getRequestParameterMap().get(ResponseStateManager.VIEW_STATE_PARAM);
+
+    if (viewStateValue != null && !viewStateValue.startsWith("!"))
+    {
+      return false;
+    }
+
+    // In certain situations this method is called from a filter and there's no facesContext, so 
+    // make sure to check for a null context
+    FacesContext context = FacesContext.getCurrentInstance();
+
+    if (context != null)
+    {
+      // is vanilla JSF used? No Trinidad render-kit-id give? If so, we need to return FALSE,
+      // since we want to save the ENTIRE state on the client...
+      UIViewRoot viewRoot = context.getViewRoot();
+      
+      if (viewRoot != null && RenderKitFactory.HTML_BASIC_RENDER_KIT.equals(viewRoot.getRenderKitId()))
+      {
+        return false;
+      }
+    }
+
+    // Last missing option: state-saving is "CLIENT" and the client-state-method uses
+    // its default (token), so we return TRUE to send down a token string.
+    return true;
+  }
+
+  /**
    * Tests whether to send a small string token, or the entire
    * serialized component state to the client-side.
    * @return true, if the small string token is to be sent to the client-side.