Skip to content

Commit

Permalink
Faces 5.0: add StateManager.StateSavingMethod enum
Browse files Browse the repository at this point in the history
  • Loading branch information
BalusC committed Oct 15, 2023
1 parent b0733a2 commit d05853e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.sun.faces.RIConstants.FLOW_DEFINITION_ID_SUFFIX;
import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.FaceletsBufferSize;
import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.FaceletsViewMappings;
import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.StateSavingMethod;
import static com.sun.faces.context.StateContext.getStateContext;
import static com.sun.faces.facelets.tag.ui.UIDebug.debugRequest;
import static com.sun.faces.renderkit.RenderKitUtils.getResponseStateManager;
Expand All @@ -42,7 +41,6 @@
import static jakarta.faces.application.ProjectStage.Development;
import static jakarta.faces.application.Resource.COMPONENT_RESOURCE_KEY;
import static jakarta.faces.application.StateManager.IS_BUILDING_INITIAL_STATE;
import static jakarta.faces.application.StateManager.STATE_SAVING_METHOD_SERVER;
import static jakarta.faces.application.ViewHandler.CHARACTER_ENCODING_KEY;
import static jakarta.faces.application.ViewHandler.DEFAULT_FACELETS_SUFFIX;
import static jakarta.faces.application.ViewVisitOption.RETURN_AS_MINIMAL_IMPLICIT_OUTCOME;
Expand Down Expand Up @@ -80,6 +78,7 @@

import com.sun.faces.application.ApplicationAssociate;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.config.WebConfiguration.WebContextInitParameter;
import com.sun.faces.context.StateContext;
import com.sun.faces.facelets.compiler.FaceletDoctype;
import com.sun.faces.facelets.el.ContextualCompositeMethodExpression;
Expand All @@ -106,6 +105,7 @@
import jakarta.faces.FacesException;
import jakarta.faces.FactoryFinder;
import jakarta.faces.application.Resource;
import jakarta.faces.application.StateManager.StateSavingMethod;
import jakarta.faces.application.ViewHandler;
import jakarta.faces.application.ViewVisitOption;
import jakarta.faces.component.ActionSource;
Expand Down Expand Up @@ -1854,7 +1854,7 @@ private void reapplyDynamicRemove(FacesContext context, ComponentStruct struct)
* @return true if we are, false otherwise.
*/
private boolean isServerStateSaving() {
if (STATE_SAVING_METHOD_SERVER.equals(webConfig.getOptionValue(StateSavingMethod))) {
if (StateSavingMethod.SERVER.name().equalsIgnoreCase(webConfig.getOptionValue(WebContextInitParameter.StateSavingMethod))) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@

package com.sun.faces.renderkit;

import static com.sun.faces.config.WebConfiguration.WebContextInitParameter.StateSavingMethod;
import static com.sun.faces.util.RequestStateManager.FACES_VIEW_STATE;
import static jakarta.faces.application.StateManager.STATE_SAVING_METHOD_CLIENT;

import java.io.IOException;

import com.sun.faces.config.WebConfiguration;
import com.sun.faces.config.WebConfiguration.WebContextInitParameter;
import com.sun.faces.renderkit.RenderKitUtils.PredefinedPostbackParameter;
import com.sun.faces.util.RequestStateManager;

import jakarta.faces.FacesException;
import jakarta.faces.application.StateManager.StateSavingMethod;
import jakarta.faces.context.FacesContext;
import jakarta.faces.render.ResponseStateManager;

Expand All @@ -40,8 +40,8 @@ public class ResponseStateManagerImpl extends ResponseStateManager {

public ResponseStateManagerImpl() {
WebConfiguration webConfig = WebConfiguration.getInstance();
String stateMode = webConfig.getOptionValue(StateSavingMethod);
helper = STATE_SAVING_METHOD_CLIENT.equalsIgnoreCase(stateMode) ? new ClientSideStateHelper() : new ServerSideStateHelper();
String stateMode = webConfig.getOptionValue(WebContextInitParameter.StateSavingMethod);
helper = StateSavingMethod.CLIENT.name().equalsIgnoreCase(stateMode) ? new ClientSideStateHelper() : new ServerSideStateHelper();
}

// --------------------------------------- Methods from ResponseStateManager
Expand Down
41 changes: 35 additions & 6 deletions impl/src/main/java/jakarta/faces/application/StateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ public abstract class StateManager {
/**
* <p>
* The <code>ServletContext</code> init parameter consulted by the <code>StateManager</code> to tell where the state
* should be saved. Valid values are given as the values of the constants: {@link #STATE_SAVING_METHOD_CLIENT} or
* {@link #STATE_SAVING_METHOD_SERVER}.
* should be saved. Valid values are given as the values of the <span class="changed_modified_5_0">enum constants
* {@link StateSavingMethod}, case insensitive</span>.
* </p>
*
* <p>
* If this parameter is not specified, the default value is the value of the constant
* {@link #STATE_SAVING_METHOD_CLIENT}.
* If this parameter is not specified, the default value is {@link StateSavingMethod#CLIENT}
* </p>
*/
public static final String STATE_SAVING_METHOD_PARAM_NAME = "jakarta.faces.STATE_SAVING_METHOD";
Expand Down Expand Up @@ -146,17 +145,47 @@ public abstract class StateManager {
* Constant value for the initialization parameter named by the <code>STATE_SAVING_METHOD_PARAM_NAME</code> that
* indicates state saving should take place on the client.
* </p>
*
* @deprecated Use {@link StateSavingMethod#CLIENT} instead.
*/
@Deprecated(since = "5.0", forRemoval = true)
public static final String STATE_SAVING_METHOD_CLIENT = "client";

/**
* <p>
* Constant value for the initialization parameter named by the <code>STATE_SAVING_METHOD_PARAM_NAME</code> that
* indicates state saving should take place on the server.
* </p>
*
* @deprecated Use {@link StateSavingMethod#SERVER} instead.
*/
@Deprecated(since = "5.0", forRemoval = true)
public static final String STATE_SAVING_METHOD_SERVER = "server";

/**
* <p class="changed_added_5_0">
* Allowed values for the initialization parameter named by the {@value StateManager#STATE_SAVING_METHOD_PARAM_NAME}.
* </p>
*
* @since 5.0
*/
public enum StateSavingMethod {

/**
* <p>
* Indicates that state saving should take place on the client.
* </p>
*/
CLIENT,

/**
* <p>
* Indicates that state saving should take place on the server.
* </p>
*/
SERVER;
}


private Boolean savingStateInClient;

Expand Down Expand Up @@ -196,7 +225,7 @@ public void writeState(FacesContext context, Object state) throws IOException {
* @param context the Faces context.
* @return <code>true</code> if and only if the value of the <code>ServletContext</code> init parameter named by the
* value of the constant {@link #STATE_SAVING_METHOD_PARAM_NAME} is equal <span class="changed_modified_2_3">(ignoring
* case)</span> to the value of the constant {@link #STATE_SAVING_METHOD_CLIENT}. <code>false</code> otherwise.
* case)</span> to the value of the constant <span class="changed_modified_5_0">{@link StateSavingMethod#CLIENT}</code>. <code>false</code> otherwise.
*
* @throws NullPointerException if <code>context</code> is <code>null</code>.
*/
Expand All @@ -207,7 +236,7 @@ public boolean isSavingStateInClient(FacesContext context) {
savingStateInClient = false;

String saveStateParam = context.getExternalContext().getInitParameter(STATE_SAVING_METHOD_PARAM_NAME);
if (saveStateParam != null && saveStateParam.equalsIgnoreCase(STATE_SAVING_METHOD_CLIENT)) {
if (StateSavingMethod.CLIENT.name().equalsIgnoreCase(saveStateParam)) {
savingStateInClient = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;

import jakarta.faces.application.StateManager.StateSavingMethod;
import jakarta.faces.context.FacesContext;
import jakarta.faces.context.ResponseWriter;

Expand Down Expand Up @@ -117,15 +118,15 @@ public abstract class ResponseStateManager {
*
* <p>
* If the state saving method for this application is
* {@link jakarta.faces.application.StateManager#STATE_SAVING_METHOD_CLIENT}, the implementation
* <span class="changed_modified_5_0">{@link StateSavingMethod#CLIENT}</span>, the implementation
* <span class="changed_modified_2_2">must</span> encrypt the state to be saved to the client
* <span class="changed_modified_2_2">in a tamper evident manner</span>.
* </p>
*
* <p>
* If the state saving method for this application is
* {@link jakarta.faces.application.StateManager#STATE_SAVING_METHOD_SERVER}, and the current request is an
* <code>Ajax</code> request {@link jakarta.faces.context.PartialViewContext#isAjaxRequest} returns <code>true</code>),
* <span class="changed_modified_5_0">{@link StateSavingMethod#SERVER}</span>, and the current request is an
* <code>Ajax</code> request ({@link jakarta.faces.context.PartialViewContext#isAjaxRequest} returns <code>true</code>),
* use the current view state identifier if it is available (do not generate a new identifier).
* </p>
*
Expand Down Expand Up @@ -207,7 +208,7 @@ public boolean isStateless(FacesContext context, String viewId) {
*
* <p class="changed_added_2_2">
* If the state saving method for this application is
* {@link jakarta.faces.application.StateManager#STATE_SAVING_METHOD_CLIENT}, <code>writeState()</code> will have
* <span class="changed_modified_5_0">{@link StateSavingMethod#CLIENT}</span>, <code>writeState()</code> will have
* encrypted the state in a tamper evident manner. If the state fails to decrypt, or decrypts but indicates evidence of
* tampering, a {@link jakarta.faces.application.ProtectedViewException} must be thrown.
* </p>
Expand Down

0 comments on commit d05853e

Please sign in to comment.