WW-4134: MessageStoreInterceptor java.lang.IllegalStateException if there is no session

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1503167 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Christian Grobmeier
2013-07-15 10:32:37 +00:00
parent 17ded8b44a
commit ef5f7bde36
2 changed files with 60 additions and 3 deletions
@@ -39,9 +39,12 @@ import com.opensymphony.xwork2.util.logging.LoggerFactory;
* <!-- 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.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..
*
* <p/>
@@ -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 ");
}
@@ -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);