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 a8abbeec0..b7509f039 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java @@ -39,9 +39,12 @@ import com.opensymphony.xwork2.util.logging.LoggerFactory; * * * 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. + * *

* * In the 'STORE' mode, the interceptor will store the {@link ValidationAware} action's message / errors @@ -56,7 +59,7 @@ import com.opensymphony.xwork2.util.logging.LoggerFactory; * * 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.. * *

@@ -147,7 +150,7 @@ import com.opensymphony.xwork2.util.logging.LoggerFactory; */ 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 extends AbstractInterceptor { 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 extends AbstractInterceptor { // 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 "); } 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 0cda04e4f..717c40aea 100644 --- a/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java +++ b/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java @@ -113,6 +113,45 @@ public class MessageStoreInterceptorTest extends StrutsTestCase { 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);