Repository: struts Updated Branches: refs/heads/master 94691e0af -> d8b6602e6
WW-4600 Adds additional check if session was invalidated Conflicts: core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/d8b6602e Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/d8b6602e Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/d8b6602e Branch: refs/heads/master Commit: d8b6602e6fd5d4943217ea98dac6be69db9a615d Parents: 94691e0 Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Wed Feb 3 09:28:21 2016 +0100 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Wed Feb 3 09:37:19 2016 +0100 ---------------------------------------------------------------------- .../interceptor/MessageStoreInterceptor.java | 39 ++++--- .../MessageStoreInterceptorTest.java | 116 ++++++++++++++++++- 2 files changed, 135 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/d8b6602e/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 8ff2522..848c5bf 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java @@ -60,7 +60,7 @@ import java.util.Map; * </p> * * <p> - * In the 'AUTOMATIC' mode, the interceptor will always retrieve the stored action's message / errors + * 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 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.. @@ -269,25 +269,34 @@ public class MessageStoreInterceptor extends AbstractInterceptor { */ protected void after(ActionInvocation invocation, String result) throws Exception { + boolean isCommitted = ServletActionContext.getResponse().isCommitted(); + if (isCommitted) { + LOG.trace("Response was already committed, cannot store messages!"); + return; + } + + boolean isInvalidated = ServletActionContext.getRequest().getSession(false) == null; + if (isInvalidated) { + LOG.trace("Session was invalidated or never created, cannot store messages!"); + return; + } + + Map<String, Object> session = invocation.getInvocationContext().getSession(); + if (session == null) { + LOG.trace("Could not store action [{}] error/messages into session, because session hasn't been opened yet.", invocation.getAction()); + return; + } + 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 && !isCommitted) { - // store error / messages into 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); - return; - } - - LOG.debug("Store action [{}] error/messages into session.", action); + if (action instanceof ValidationAware) { + LOG.debug("Storing action [{}] error/messages into session ", action); ValidationAware validationAwareAction = (ValidationAware) action; session.put(actionErrorsSessionKey, validationAwareAction.getActionErrors()); @@ -295,11 +304,7 @@ public class MessageStoreInterceptor extends AbstractInterceptor { session.put(fieldErrorsSessionKey, validationAwareAction.getFieldErrors()); } else { - 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); - } + LOG.debug("Action [{}] is not ValidationAware, no message / error that are storeable", action); } } } http://git-wip-us.apache.org/repos/asf/struts/blob/d8b6602e/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 2fc6442..77b9da1 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 com.opensymphony.xwork2.ActionProxy; import org.apache.struts2.ServletActionContext; import org.apache.struts2.StrutsInternalTestCase; import org.apache.struts2.result.ServletActionRedirectResult; @@ -37,7 +38,10 @@ import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionSupport; +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; /** @@ -80,6 +84,15 @@ public class MessageStoreInterceptorTest extends StrutsInternalTestCase { actionContext.put(ActionContext.PARAMETERS, paramMap); actionContext.put(ActionContext.SESSION, sessionMap); + HttpSession mockedSession = EasyMock.createControl().createMock(HttpSession.class); + HttpServletRequest mockedRequest = EasyMock.createControl().createMock(HttpServletRequest.class); + mockedRequest.getSession(false); + EasyMock.expectLastCall().andReturn(mockedSession); + EasyMock.expectLastCall().once(); + ServletActionContext.setRequest(mockedRequest); + + EasyMock.replay(mockedRequest); + // Mock (ActionInvocation) ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class); mockActionInvocation.getInvocationContext(); @@ -143,6 +156,15 @@ public class MessageStoreInterceptorTest extends StrutsInternalTestCase { ActionContext actionContext = new ActionContext(new HashMap()); actionContext.put(ActionContext.PARAMETERS, paramMap); + HttpSession mockedSession = EasyMock.createControl().createMock(HttpSession.class); + HttpServletRequest mockedRequest = EasyMock.createControl().createMock(HttpServletRequest.class); + mockedRequest.getSession(false); + EasyMock.expectLastCall().andReturn(mockedSession); + EasyMock.expectLastCall().once(); + ServletActionContext.setRequest(mockedRequest); + + EasyMock.replay(mockedRequest); + // Mock (ActionInvocation) ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class); mockActionInvocation.getInvocationContext(); @@ -155,9 +177,6 @@ public class MessageStoreInterceptorTest extends StrutsInternalTestCase { mockActionInvocation.getAction(); EasyMock.expectLastCall().andReturn(action); - mockActionInvocation.getResult(); - EasyMock.expectLastCall().andReturn(new ServletActionRedirectResult()); - EasyMock.replay(mockActionInvocation); interceptor.init(); @@ -201,6 +220,14 @@ public class MessageStoreInterceptorTest extends StrutsInternalTestCase { sessionMap.put(MessageStoreInterceptor.actionMessagesSessionKey, actionMessages); sessionMap.put(MessageStoreInterceptor.fieldErrorsSessionKey, fieldErrors); + HttpSession mockedSession = EasyMock.createControl().createMock(HttpSession.class); + HttpServletRequest mockedRequest = EasyMock.createControl().createMock(HttpServletRequest.class); + mockedRequest.getSession(false); + EasyMock.expectLastCall().andReturn(mockedSession); + EasyMock.expectLastCall().once(); + ServletActionContext.setRequest(mockedRequest); + + EasyMock.replay(mockedRequest); ActionContext actionContext = new ActionContext(new HashMap()); actionContext.put(ActionContext.PARAMETERS, paramsMap); @@ -259,6 +286,15 @@ public class MessageStoreInterceptorTest extends StrutsInternalTestCase { actionContext.put(ActionContext.PARAMETERS, paramMap); actionContext.put(ActionContext.SESSION, sessionMap); + HttpSession mockedSession = EasyMock.createControl().createMock(HttpSession.class); + HttpServletRequest mockedRequest = EasyMock.createControl().createMock(HttpServletRequest.class); + mockedRequest.getSession(false); + EasyMock.expectLastCall().andReturn(mockedSession); + EasyMock.expectLastCall().once(); + ServletActionContext.setRequest(mockedRequest); + + EasyMock.replay(mockedRequest); + // Mock (ActionInvocation) ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class); mockActionInvocation.getInvocationContext(); @@ -317,6 +353,14 @@ public class MessageStoreInterceptorTest extends StrutsInternalTestCase { sessionMap.put(MessageStoreInterceptor.actionMessagesSessionKey, actionMessages); sessionMap.put(MessageStoreInterceptor.fieldErrorsSessionKey, fieldErrors); + mockedSession = EasyMock.createControl().createMock(HttpSession.class); + mockedRequest = EasyMock.createControl().createMock(HttpServletRequest.class); + mockedRequest.getSession(false); + EasyMock.expectLastCall().andReturn(mockedSession); + EasyMock.expectLastCall().once(); + ServletActionContext.setRequest(mockedRequest); + + EasyMock.replay(mockedRequest); actionContext = new ActionContext(new HashMap()); actionContext.put(ActionContext.PARAMETERS, paramMap); @@ -422,4 +466,70 @@ public class MessageStoreInterceptorTest extends StrutsInternalTestCase { EasyMock.verify(mockActionInvocation); } + + public void testSessionWasInvalidated() throws Exception { + // given + ActionContext actionContext = new ActionContext(new HashMap()); + actionContext.put(ActionContext.PARAMETERS, new LinkedHashMap()); + + ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class); + + mockActionInvocation.getInvocationContext(); + EasyMock.expectLastCall().andReturn(actionContext); + EasyMock.expectLastCall().anyTimes(); + + mockActionInvocation.invoke(); + EasyMock.expectLastCall().andReturn(Action.SUCCESS); + + EasyMock.replay(mockActionInvocation); + + HttpServletRequest mockedRequest = EasyMock.createControl().createMock(HttpServletRequest.class); + mockedRequest.getSession(false); + EasyMock.expectLastCall().andReturn(null); + EasyMock.expectLastCall().once(); + ServletActionContext.setRequest(mockedRequest); + + EasyMock.replay(mockedRequest); + + // when + MessageStoreInterceptor msi = new MessageStoreInterceptor(); + msi.intercept(mockActionInvocation); + + // then + EasyMock.verify(mockActionInvocation); + EasyMock.verify(mockedRequest); + } + + public void testResponseWasComitted() throws Exception { + // given + ActionContext actionContext = new ActionContext(new HashMap()); + actionContext.put(ActionContext.PARAMETERS, new LinkedHashMap()); + + ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class); + + mockActionInvocation.getInvocationContext(); + EasyMock.expectLastCall().andReturn(actionContext); + EasyMock.expectLastCall().anyTimes(); + + mockActionInvocation.invoke(); + EasyMock.expectLastCall().andReturn(Action.SUCCESS); + + EasyMock.replay(mockActionInvocation); + + HttpServletResponse mockedResponse = EasyMock.createControl().createMock(HttpServletResponse.class); + mockedResponse.isCommitted(); + EasyMock.expectLastCall().andReturn(true); + EasyMock.expectLastCall().once(); + ServletActionContext.setResponse(mockedResponse); + + EasyMock.replay(mockedResponse); + + // when + MessageStoreInterceptor msi = new MessageStoreInterceptor(); + msi.intercept(mockActionInvocation); + + // then + EasyMock.verify(mockActionInvocation); + EasyMock.verify(mockedResponse); + } }