mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-4600 Adds additional check if session was invalidated
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+113
-3
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user