struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [3/4] struts git commit: WW-4134 Checks if Response wasn't committed to allow store messages (cherry picked from commit 0958077)
Date Fri, 30 Oct 2015 08:22:18 GMT
WW-4134 Checks if Response wasn't committed to allow store messages
(cherry picked from commit 0958077)


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/0a08f6b6
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/0a08f6b6
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/0a08f6b6

Branch: refs/heads/master
Commit: 0a08f6b673a2334a76ab0ae64ad2956cc4f4947a
Parents: ba361aa
Author: Lukasz Lenart <lukaszlenart@apache.org>
Authored: Tue Oct 27 09:01:09 2015 +0100
Committer: Lukasz Lenart <lukaszlenart@apache.org>
Committed: Tue Oct 27 09:12:24 2015 +0100

----------------------------------------------------------------------
 .../interceptor/MessageStoreInterceptor.java        | 16 ++++++++++++----
 .../interceptor/MessageStoreInterceptorTest.java    | 15 +++++++++++++++
 2 files changed, 27 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/0a08f6b6/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
b/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
index ee78563..8ff2522 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
@@ -27,6 +27,8 @@ import com.opensymphony.xwork2.interceptor.ValidationAware;
 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
+
+import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.result.ServletRedirectResult;
 
 import java.util.ArrayList;
@@ -269,14 +271,16 @@ public class MessageStoreInterceptor extends AbstractInterceptor {
 
         String reqOperationMode = getRequestOperationMode(invocation);
         boolean isRedirect = invocation.getResult() instanceof ServletRedirectResult;
+        boolean isCommitted = ServletActionContext.getResponse().isCommitted();
+
         if (STORE_MODE.equalsIgnoreCase(reqOperationMode) ||
                 STORE_MODE.equalsIgnoreCase(operationMode) ||
                 (AUTOMATIC_MODE.equalsIgnoreCase(operationMode) && isRedirect)) {
 
             Object action = invocation.getAction();
-            if (action instanceof ValidationAware) {
+            if (action instanceof ValidationAware && !isCommitted) {
                 // store error / messages into session
-                Map session = (Map) invocation.getInvocationContext().get(ActionContext.SESSION);
+                Map<String, Object> session = invocation.getInvocationContext().getSession();
 
                 if (session == null) {
                     LOG.debug("Could not store action [{}] error/messages into session, because
session hasn't been opened yet.", action);
@@ -289,13 +293,17 @@ public class MessageStoreInterceptor extends AbstractInterceptor {
                 session.put(actionErrorsSessionKey, validationAwareAction.getActionErrors());
                 session.put(actionMessagesSessionKey, validationAwareAction.getActionMessages());
                 session.put(fieldErrorsSessionKey, validationAwareAction.getFieldErrors());
+
             } else {
-                LOG.debug("Action [{}] is not ValidationAware, no message / error that are
storeable", action);
+                if (isCommitted) {
+                    LOG.debug("Response was already committed, cannot store messages!");
+                } else {
+                    LOG.debug("Action [{}] is not ValidationAware, no message / error that
are storeable", action);
+                }
             }
         }
     }
 
-
     /**
      * Get the operationMode through request parameter, if <code>allowRequestParameterSwitch</code>
      * is 'true', else it simply returns 'NONE', meaning its neither in the 'STORE_MODE'
nor

http://git-wip-us.apache.org/repos/asf/struts/blob/0a08f6b6/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java
b/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java
index a419a18..2fc6442 100644
--- a/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java
+++ b/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java
@@ -27,6 +27,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.StrutsInternalTestCase;
 import org.apache.struts2.result.ServletActionRedirectResult;
 import org.easymock.EasyMock;
@@ -36,6 +37,8 @@ import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.ActionSupport;
 
+import javax.servlet.http.HttpServletResponse;
+
 
 /**
  * Test case for MessageStoreInterceptor.
@@ -44,6 +47,18 @@ import com.opensymphony.xwork2.ActionSupport;
  */
 public class MessageStoreInterceptorTest extends StrutsInternalTestCase {
 
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+
+        HttpServletResponse response = EasyMock.createNiceControl().createMock(HttpServletResponse.class);
+        response.isCommitted();
+        EasyMock.expectLastCall().andReturn(Boolean.FALSE);
+        EasyMock.replay(response);
+
+        ServletActionContext.setResponse(response);
+    }
+
     public void testStoreMessage() throws Exception {
         MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
         interceptor.setAllowRequestParameterSwitch(true);


Mime
View raw message