From 63dc85fd7afc75d0b577f82a40016fd8a00a3502 Mon Sep 17 00:00:00 2001 From: JCgH4164838Gh792C124B5 <43964333+JCgH4164838Gh792C124B5@users.noreply.github.com> Date: Mon, 25 Mar 2019 23:36:07 -0400 Subject: [PATCH] Updated commit with changes suggested by reviewers: - Replace TokenSessionStoreInterceptor inline comment previously added with method comment blocks (better locatioh for explanation) - Eliminate duplicate savedActionContext.setValueStack call in InvocationSessionStore (typo) - Improve InvocationSessionStore.loadInvocation() by reording assignment statements to allow single-line assignments --- .../TokenSessionStoreInterceptor.java | 27 +++++++++++++++++-- .../struts2/util/InvocationSessionStore.java | 12 +++------ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/interceptor/TokenSessionStoreInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/TokenSessionStoreInterceptor.java index 345115f0a..7332bd8fa 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/TokenSessionStoreInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/TokenSessionStoreInterceptor.java @@ -112,6 +112,20 @@ public class TokenSessionStoreInterceptor extends TokenInterceptor { } } + /** + * Handles processing of invalid tokens. If a previously stored invocation is + * available, the method will attempt to return and render its result. Otherwise + * it will return INVALID_TOKEN_CODE. + * + * Note: Because a stored (previously completed) invocation's PageContext will be closed, + * this method must replace the stored PageContext with the current invocation's one (or a null). + * See {@link org.apache.struts2.util.InvocationSessionStore#loadInvocation(String key, String token)} for details. + * + * @param invocation + * + * @return + * @throws Exception + */ @Override protected String handleInvalidToken(ActionInvocation invocation) throws Exception { ActionContext ac = invocation.getInvocationContext(); @@ -130,8 +144,7 @@ public class TokenSessionStoreInterceptor extends TokenInterceptor { ActionInvocation savedInvocation = InvocationSessionStore.loadInvocation(sessionTokenName, token); if (savedInvocation != null) { - // set the valuestack to the request scope - // Note: loadInvocation() restored invocation's PageContext (as savedInvocation's will already be closed) + // set the savedInvocation's valuestack to the request scope ValueStack stack = savedInvocation.getStack(); request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack); @@ -154,6 +167,16 @@ public class TokenSessionStoreInterceptor extends TokenInterceptor { return INVALID_TOKEN_CODE; } + /** + * Handles processing of valid tokens. Stores the current invocation for + * later use by {@link handleInvalidToken}. + * See {@link org.apache.struts2.util.InvocationSessionStore#storeInvocation(String key, String token, ActionInvocation invocation)} for details. + * + * @param invocation + * + * @return + * @throws Exception + */ @Override protected String handleValidToken(ActionInvocation invocation) throws Exception { // we know the token name and token must be there diff --git a/core/src/main/java/org/apache/struts2/util/InvocationSessionStore.java b/core/src/main/java/org/apache/struts2/util/InvocationSessionStore.java index 2e1483415..238df3c56 100644 --- a/core/src/main/java/org/apache/struts2/util/InvocationSessionStore.java +++ b/core/src/main/java/org/apache/struts2/util/InvocationSessionStore.java @@ -56,19 +56,15 @@ public class InvocationSessionStore { return null; } - ActionInvocation savedInvocation = null; - if (invocationContext.invocation != null) { + final ActionInvocation savedInvocation = invocationContext.invocation; + if (savedInvocation != null) { // WW-5026 - Preserve the previous PageContext (even if null) and restore it to the // ActionContext after loading the savedInvocation context. The saved context's PageContext // would already be closed at this point (causing failures if used for output). - final ActionContext savedActionContext; - final ActionContext previousActionContext; - savedInvocation = invocationContext.invocation; - savedActionContext = savedInvocation.getInvocationContext(); - previousActionContext = ActionContext.getContext(); + final ActionContext savedActionContext = savedInvocation.getInvocationContext(); + final ActionContext previousActionContext = ActionContext.getContext(); ActionContext.setContext(savedActionContext); savedActionContext.setValueStack(savedInvocation.getStack()); - savedActionContext.setValueStack(savedInvocation.getStack()); if (previousActionContext != null) { savedActionContext.put(ServletActionContext.PAGE_CONTEXT, previousActionContext.get(ServletActionContext.PAGE_CONTEXT)); } else {