WW-4134 Checks if Response wasn't committed to allow store messages

This commit is contained in:
Lukasz Lenart
2015-10-27 09:01:09 +01:00
parent 7848534e2f
commit 0958077762
2 changed files with 27 additions and 6 deletions
@@ -26,6 +26,7 @@ import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.ServletRedirectResult;
import com.opensymphony.xwork2.ActionContext;
@@ -273,14 +274,16 @@ public class MessageStoreInterceptor extends AbstractInterceptor {
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) {
if (action instanceof ValidationAware && !isCommitted) {
// store error / messages into session
Map session = (Map) invocation.getInvocationContext().get(ActionContext.SESSION);
Map<String, Object> session = invocation.getInvocationContext().getSession();
if (session == null) {
if (LOG.isDebugEnabled()) {
@@ -297,14 +300,17 @@ public class MessageStoreInterceptor extends AbstractInterceptor {
session.put(actionErrorsSessionKey, validationAwareAction.getActionErrors());
session.put(actionMessagesSessionKey, validationAwareAction.getActionMessages());
session.put(fieldErrorsSessionKey, validationAwareAction.getFieldErrors());
}
else if(LOG.isDebugEnabled()) {
LOG.debug("Action ["+action+"] is not ValidationAware, no message / error that are storeable");
} 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");
}
}
}
}
/**
* Get the operationMode through request paramter, if <code>allowRequestParameterSwitch</code>
* is 'true', else it simply returns 'NONE', meaning its neither in the 'STORE_MODE' nor
@@ -27,6 +27,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsInternalTestCase;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;
import org.easymock.EasyMock;
@@ -36,6 +37,8 @@ import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletResponse;
/**
* Test case for MessageStoreInterceptor.
@@ -44,6 +47,18 @@ import com.opensymphony.xwork2.ActionSupport;
*/
public class MessageStoreInterceptorTest extends StrutsInternalTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
HttpServletResponse response = EasyMock.createNiceControl().createMock(HttpServletResponse.class);
response.isCommitted();
EasyMock.expectLastCall().andReturn(Boolean.FALSE);
EasyMock.replay(response);
ServletActionContext.setResponse(response);
}
public void testStoreMessage() throws Exception {
MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
interceptor.setAllowRequestParameterSwitch(true);