mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-4134 Checks if Response wasn't committed to allow store messages
This commit is contained in:
@@ -26,6 +26,7 @@ import java.util.Collection;
|
|||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.struts2.ServletActionContext;
|
||||||
import org.apache.struts2.dispatcher.ServletRedirectResult;
|
import org.apache.struts2.dispatcher.ServletRedirectResult;
|
||||||
|
|
||||||
import com.opensymphony.xwork2.ActionContext;
|
import com.opensymphony.xwork2.ActionContext;
|
||||||
@@ -273,14 +274,16 @@ public class MessageStoreInterceptor extends AbstractInterceptor {
|
|||||||
|
|
||||||
String reqOperationMode = getRequestOperationMode(invocation);
|
String reqOperationMode = getRequestOperationMode(invocation);
|
||||||
boolean isRedirect = invocation.getResult() instanceof ServletRedirectResult;
|
boolean isRedirect = invocation.getResult() instanceof ServletRedirectResult;
|
||||||
|
boolean isCommitted = ServletActionContext.getResponse().isCommitted();
|
||||||
|
|
||||||
if (STORE_MODE.equalsIgnoreCase(reqOperationMode) ||
|
if (STORE_MODE.equalsIgnoreCase(reqOperationMode) ||
|
||||||
STORE_MODE.equalsIgnoreCase(operationMode) ||
|
STORE_MODE.equalsIgnoreCase(operationMode) ||
|
||||||
(AUTOMATIC_MODE.equalsIgnoreCase(operationMode) && isRedirect)) {
|
(AUTOMATIC_MODE.equalsIgnoreCase(operationMode) && isRedirect)) {
|
||||||
|
|
||||||
Object action = invocation.getAction();
|
Object action = invocation.getAction();
|
||||||
if (action instanceof ValidationAware) {
|
if (action instanceof ValidationAware && !isCommitted) {
|
||||||
// store error / messages into session
|
// store error / messages into session
|
||||||
Map session = (Map) invocation.getInvocationContext().get(ActionContext.SESSION);
|
Map<String, Object> session = invocation.getInvocationContext().getSession();
|
||||||
|
|
||||||
if (session == null) {
|
if (session == null) {
|
||||||
if (LOG.isDebugEnabled()) {
|
if (LOG.isDebugEnabled()) {
|
||||||
@@ -297,14 +300,17 @@ public class MessageStoreInterceptor extends AbstractInterceptor {
|
|||||||
session.put(actionErrorsSessionKey, validationAwareAction.getActionErrors());
|
session.put(actionErrorsSessionKey, validationAwareAction.getActionErrors());
|
||||||
session.put(actionMessagesSessionKey, validationAwareAction.getActionMessages());
|
session.put(actionMessagesSessionKey, validationAwareAction.getActionMessages());
|
||||||
session.put(fieldErrorsSessionKey, validationAwareAction.getFieldErrors());
|
session.put(fieldErrorsSessionKey, validationAwareAction.getFieldErrors());
|
||||||
}
|
|
||||||
else if(LOG.isDebugEnabled()) {
|
} else if(LOG.isDebugEnabled()) {
|
||||||
LOG.debug("Action ["+action+"] is not ValidationAware, no message / error that are storeable");
|
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>
|
* 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
|
* 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.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.struts2.ServletActionContext;
|
||||||
import org.apache.struts2.StrutsInternalTestCase;
|
import org.apache.struts2.StrutsInternalTestCase;
|
||||||
import org.apache.struts2.dispatcher.ServletActionRedirectResult;
|
import org.apache.struts2.dispatcher.ServletActionRedirectResult;
|
||||||
import org.easymock.EasyMock;
|
import org.easymock.EasyMock;
|
||||||
@@ -36,6 +37,8 @@ import com.opensymphony.xwork2.ActionContext;
|
|||||||
import com.opensymphony.xwork2.ActionInvocation;
|
import com.opensymphony.xwork2.ActionInvocation;
|
||||||
import com.opensymphony.xwork2.ActionSupport;
|
import com.opensymphony.xwork2.ActionSupport;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test case for MessageStoreInterceptor.
|
* Test case for MessageStoreInterceptor.
|
||||||
@@ -44,6 +47,18 @@ import com.opensymphony.xwork2.ActionSupport;
|
|||||||
*/
|
*/
|
||||||
public class MessageStoreInterceptorTest extends StrutsInternalTestCase {
|
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 {
|
public void testStoreMessage() throws Exception {
|
||||||
MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
|
MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
|
||||||
interceptor.setAllowRequestParameterSwitch(true);
|
interceptor.setAllowRequestParameterSwitch(true);
|
||||||
|
|||||||
Reference in New Issue
Block a user