You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2016/02/03 09:33:58 UTC
struts git commit: WW-4600 Adds additional check if session was
invalidated
Repository: struts
Updated Branches:
refs/heads/support-2-3 c44566bd2 -> 85373951b
WW-4600 Adds additional check if session was invalidated
Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/85373951
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/85373951
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/85373951
Branch: refs/heads/support-2-3
Commit: 85373951b7dfe55a92a76c87ee8faf50233bf07c
Parents: c44566b
Author: Lukasz Lenart <lu...@apache.org>
Authored: Wed Feb 3 09:28:21 2016 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Wed Feb 3 09:33:50 2016 +0100
----------------------------------------------------------------------
.../interceptor/MessageStoreInterceptor.java | 43 +++----
.../MessageStoreInterceptorTest.java | 116 ++++++++++++++++++-
2 files changed, 135 insertions(+), 24 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/struts/blob/85373951/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 f34cee0..97e1693 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
@@ -272,41 +272,42 @@ 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 [#0] 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) {
- 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 ");
- }
+ if (action instanceof ValidationAware) {
+ LOG.debug("Storing action [#0] error/messages into session ", action);
ValidationAware validationAwareAction = (ValidationAware) action;
session.put(actionErrorsSessionKey, validationAwareAction.getActionErrors());
session.put(actionMessagesSessionKey, validationAwareAction.getActionMessages());
session.put(fieldErrorsSessionKey, validationAwareAction.getFieldErrors());
- } else if(LOG.isDebugEnabled()) {
- if (isCommitted) {
- LOG.debug("Response was already committed, cannot store messages!");
- } else {
- LOG.debug("Action [" + action + "] is not ValidationAware, no message / error that are storeable");
- }
+ } else {
+ LOG.debug("Action [#0] is not ValidationAware, no message / error that are storeable", action);
}
}
}
http://git-wip-us.apache.org/repos/asf/struts/blob/85373951/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 d5e339a..f85dcbb 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.dispatcher.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);
+ }
}