Author: grobmeier
Date: Mon Jul 15 10:32:37 2013
New Revision: 1503167

URL: http://svn.apache.org/r1503167
Log:
WW-4134: MessageStoreInterceptor java.lang.IllegalStateException if there is no 
session

Modified:
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
    
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java?rev=1503167&r1=1503166&r2=1503167&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
 Mon Jul 15 10:32:37 2013
@@ -39,9 +39,12 @@ import com.opensymphony.xwork2.util.logg
  * <!-- START SNIPPET: description -->
  *
  * An interceptor to store a {@link ValidationAware} action's messages / 
errors and field errors into
- * HTTP Session, such that it will be retrieveable at a later stage. This 
allows the action's message /
+ * HTTP Session, such that it will be retrievable at a later stage. This 
allows the action's message /
  * errors and field errors to be available longer that just the particular 
HTTP request.
  *
+ * If no session exists, nothing will be stored and can be retrieved later. In 
other terms,
+ * the application is responsible to open the session.
+ *
  * <p/>
  *
  * In the 'STORE' mode, the interceptor will store the {@link ValidationAware} 
action's message / errors
@@ -56,7 +59,7 @@ import com.opensymphony.xwork2.util.logg
  *
  * In the 'AUTOMATIC' mode, the interceptor will always retrieve the stored 
action's message / errors 
  * and field errors and put them back into the {@link ValidationAware} action, 
and after Action execution, 
- * if the {@link Result} is an instance of {@link ServletRedirectResult}, the 
action's message / errors 
+ * if the {@link com.opensymphony.xwork2.Result} is an instance of {@link 
ServletRedirectResult}, the action's message / errors
  * and field errors into automatically be stored in the HTTP session..
  *
  * <p/>
@@ -147,7 +150,7 @@ import com.opensymphony.xwork2.util.logg
  */
 public class MessageStoreInterceptor extends AbstractInterceptor {
 
-    private static final long serialVersionUID = 4491997514314242420L;
+    private static final long serialVersionUID = 9161650888603380164L;
 
     private static final Logger LOG = 
LoggerFactory.getLogger(MessageStoreInterceptor.class);
 
@@ -219,6 +222,14 @@ public class MessageStoreInterceptor ext
             if (action instanceof ValidationAware) {
                 // retrieve error / message from session
                 Map session = (Map) 
invocation.getInvocationContext().get(ActionContext.SESSION);
+
+                if (session == null) {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Session is not open, no errors / messages 
could be retrieve for action ["+action+"]");
+                    }
+                    return;
+                }
+
                 ValidationAware validationAwareAction = (ValidationAware) 
action;
 
                 if (LOG.isDebugEnabled()) {
@@ -271,6 +282,13 @@ public class MessageStoreInterceptor ext
                 // store error / messages into session
                 Map session = (Map) 
invocation.getInvocationContext().get(ActionContext.SESSION);
 
+                if (session == null) {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Could not store action ["+action+"] 
error/messages into session, because session hasn't been opened yet.");
+                    }
+                    return;
+                }
+
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("store action ["+action+"] error/messages into 
session ");
                 }

Modified: 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java?rev=1503167&r1=1503166&r2=1503167&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java
 (original)
+++ 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java
 Mon Jul 15 10:32:37 2013
@@ -113,6 +113,45 @@ public class MessageStoreInterceptorTest
         EasyMock.verify(mockActionInvocation);
     }
 
+    public void testIgnoreMessageWithoutSession() throws Exception {
+        MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
+        interceptor.setAllowRequestParameterSwitch(true);
+        interceptor.setOperationMode(MessageStoreInterceptor.STORE_MODE);
+
+        Map paramMap = new LinkedHashMap();
+
+        ActionSupport action = new ActionSupport();
+        action.addActionError("some action error 1");
+        action.addActionMessage("some action message 1");
+        action.addFieldError("field2", "some field error 2");
+
+        ActionContext actionContext = new ActionContext(new HashMap());
+        actionContext.put(ActionContext.PARAMETERS, paramMap);
+
+        // Mock (ActionInvocation)
+        ActionInvocation mockActionInvocation = 
EasyMock.createControl().createMock(ActionInvocation.class);
+        mockActionInvocation.getInvocationContext();
+        EasyMock.expectLastCall().andReturn(actionContext);
+        EasyMock.expectLastCall().anyTimes();
+
+        mockActionInvocation.invoke();
+        EasyMock.expectLastCall().andReturn(Action.SUCCESS);
+
+        mockActionInvocation.getAction();
+        EasyMock.expectLastCall().andReturn(action);
+
+        mockActionInvocation.getResult();
+        EasyMock.expectLastCall().andReturn(new ServletActionRedirectResult());
+
+        EasyMock.replay(mockActionInvocation);
+
+        interceptor.init();
+        interceptor.intercept(mockActionInvocation);
+        interceptor.destroy();
+
+        EasyMock.verify(mockActionInvocation);
+    }
+
     public void testRetrieveMessage() throws Exception {
         MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
         interceptor.setOperationMode(MessageStoreInterceptor.RETRIEVE_MODE);


Reply via email to