mirror of
https://github.com/apache/struts.git
synced 2026-08-06 23:27:07 +00:00
WW-4728 Allows override request parameter names used to enable validation
This commit is contained in:
@@ -39,36 +39,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>Serializes validation and action errors into JSON. This interceptor does not
|
||||
* Serializes validation and action errors into JSON. This interceptor does not
|
||||
* perform any validation, so it must follow the 'validation' interceptor on the stack.
|
||||
* </p>
|
||||
*
|
||||
* <p>This stack (defined in struts-default.xml) shows how to use this interceptor with the
|
||||
* 'validation' interceptor</p>
|
||||
* <pre>
|
||||
* <interceptor-stack name="jsonValidationWorkflowStack">
|
||||
* <interceptor-ref name="basicStack"/>
|
||||
* <interceptor-ref name="validation">
|
||||
* <param name="excludeMethods">input,back,cancel</param>
|
||||
* </interceptor-ref>
|
||||
* <interceptor-ref name="jsonValidation"/>
|
||||
* <interceptor-ref name="workflow"/>
|
||||
* </interceptor-stack>
|
||||
* </pre>
|
||||
* <p>If 'validationFailedStatus' is set it will be used as the Response status
|
||||
* when validation fails.</p>
|
||||
*
|
||||
* <p>If the request has a parameter 'struts.validateOnly' execution will return after
|
||||
* validation (action won't be executed).</p>
|
||||
*
|
||||
* <p>If 'struts.validateOnly' is set to false you may want to use {@link JSONActionRedirectResult}.</p>
|
||||
*
|
||||
* <p>A request parameter named 'struts.enableJSONValidation' must be set to 'true' to
|
||||
* use this interceptor</p>
|
||||
*
|
||||
* <p>If the request has a parameter 'struts.JSONValidation.set.encoding' set to true
|
||||
* the character encoding will NOT be set on the response - is needed in portlet environment
|
||||
* - for more details see issue WW-3237</p>
|
||||
*/
|
||||
public class JSONValidationInterceptor extends MethodFilterInterceptor {
|
||||
|
||||
@@ -80,16 +52,10 @@ public class JSONValidationInterceptor extends MethodFilterInterceptor {
|
||||
|
||||
public static final String DEFAULT_ENCODING = "UTF-8";
|
||||
|
||||
private int validationFailedStatus = -1;
|
||||
|
||||
/**
|
||||
* HTTP status that will be set in the response if validation fails
|
||||
*
|
||||
* @param validationFailedStatus validation failed status
|
||||
*/
|
||||
public void setValidationFailedStatus(int validationFailedStatus) {
|
||||
this.validationFailedStatus = validationFailedStatus;
|
||||
}
|
||||
private int validationFailedStatus = HttpServletResponse.SC_BAD_REQUEST;
|
||||
private String validateOnlyParam = VALIDATE_ONLY_PARAM;
|
||||
private String validateJsonParam = VALIDATE_JSON_PARAM;
|
||||
private String noEncodingSetParam = NO_ENCODING_SET_PARAM;
|
||||
|
||||
@Override
|
||||
protected String doIntercept(ActionInvocation invocation) throws Exception {
|
||||
@@ -121,13 +87,9 @@ public class JSONValidationInterceptor extends MethodFilterInterceptor {
|
||||
|
||||
private void setupEncoding(HttpServletResponse response, HttpServletRequest request) {
|
||||
if (isSetEncoding(request)) {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Default encoding not set!");
|
||||
}
|
||||
} else {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Setting up encoding to: [" + DEFAULT_ENCODING + "]!");
|
||||
}
|
||||
LOG.debug("Setting up encoding to: [{}]!", DEFAULT_ENCODING);
|
||||
response.setCharacterEncoding(DEFAULT_ENCODING);
|
||||
}
|
||||
}
|
||||
@@ -143,16 +105,16 @@ public class JSONValidationInterceptor extends MethodFilterInterceptor {
|
||||
return Action.NONE;
|
||||
}
|
||||
|
||||
private boolean isJsonEnabled(HttpServletRequest request) {
|
||||
return "true".equals(request.getParameter(VALIDATE_JSON_PARAM));
|
||||
public boolean isJsonEnabled(HttpServletRequest request) {
|
||||
return Boolean.parseBoolean(request.getParameter(validateJsonParam));
|
||||
}
|
||||
|
||||
private boolean isValidateOnly(HttpServletRequest request) {
|
||||
return "true".equals(request.getParameter(VALIDATE_ONLY_PARAM));
|
||||
public boolean isValidateOnly(HttpServletRequest request) {
|
||||
return Boolean.parseBoolean(request.getParameter(validateOnlyParam));
|
||||
}
|
||||
|
||||
private boolean isSetEncoding(HttpServletRequest request) {
|
||||
return "true".equals(request.getParameter(NO_ENCODING_SET_PARAM));
|
||||
public boolean isSetEncoding(HttpServletRequest request) {
|
||||
return Boolean.parseBoolean(request.getParameter(noEncodingSetParam));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,4 +184,40 @@ public class JSONValidationInterceptor extends MethodFilterInterceptor {
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP status that will be set in the response if validation fails
|
||||
*
|
||||
* @param validationFailedStatus validation failed status
|
||||
*/
|
||||
public void setValidationFailedStatus(int validationFailedStatus) {
|
||||
this.validationFailedStatus = validationFailedStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides 'struts.validateOnly' param name
|
||||
*
|
||||
* @param validateOnlyParam new param name
|
||||
*/
|
||||
public void setValidateOnlyParam(String validateOnlyParam) {
|
||||
this.validateOnlyParam = validateOnlyParam;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides 'struts.enableJSONValidation' param name
|
||||
*
|
||||
* @param validateJsonParam new param name
|
||||
*/
|
||||
public void setValidateJsonParam(String validateJsonParam) {
|
||||
this.validateJsonParam = validateJsonParam;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides 'struts.JSONValidation.no.encoding' param name
|
||||
*
|
||||
* @param noEncodingSetParam new param name
|
||||
*/
|
||||
public void setNoEncodingSetParam(String noEncodingSetParam) {
|
||||
this.noEncodingSetParam = noEncodingSetParam;
|
||||
}
|
||||
}
|
||||
|
||||
+48
-1
@@ -63,7 +63,6 @@ public class JSONValidationInterceptorTest extends StrutsTestCase {
|
||||
request.setParameterMap(parameters);
|
||||
|
||||
validationInterceptor.intercept(invocation);
|
||||
interceptor.setValidationFailedStatus(HttpServletResponse.SC_BAD_REQUEST);
|
||||
interceptor.intercept(invocation);
|
||||
|
||||
String json = stringWriter.toString();
|
||||
@@ -131,6 +130,54 @@ public class JSONValidationInterceptorTest extends StrutsTestCase {
|
||||
assertEquals("UTF-8", response.getCharacterEncoding());
|
||||
}
|
||||
|
||||
public void testValidationSucceedsWithDifferentParamName() throws Exception {
|
||||
JSONValidationInterceptor interceptor = new JSONValidationInterceptor();
|
||||
interceptor.setValidateJsonParam("enableJSONValidation");
|
||||
|
||||
action.setText("abcd@ggg.com");
|
||||
action.setPassword("apassword");
|
||||
action.setValue(10);
|
||||
|
||||
Map<String, String> parameters = new HashMap<>();
|
||||
parameters.put("enableJSONValidation", "true");
|
||||
request.setParameterMap(parameters);
|
||||
|
||||
validationInterceptor.intercept(invocation);
|
||||
interceptor.intercept(invocation);
|
||||
|
||||
String json = stringWriter.toString();
|
||||
|
||||
String normalizedActual = TestUtils.normalize(json, true);
|
||||
assertEquals("", normalizedActual);
|
||||
}
|
||||
|
||||
public void testValidationSucceedsValidateOnlyWithDifferentParamName() throws Exception {
|
||||
JSONValidationInterceptor interceptor = new JSONValidationInterceptor();
|
||||
interceptor.setValidateOnlyParam("validateOnly");
|
||||
interceptor.setValidateJsonParam("enableJSONValidation");
|
||||
|
||||
action.setText("abcd@ggg.com");
|
||||
action.setPassword("apassword");
|
||||
action.setValue(10);
|
||||
|
||||
//just validate
|
||||
Map<String, String> parameters = new HashMap<>();
|
||||
parameters.put("validateOnly", "true");
|
||||
parameters.put("enableJSONValidation", "true");
|
||||
request.setParameterMap(parameters);
|
||||
|
||||
validationInterceptor.intercept(invocation);
|
||||
interceptor.intercept(invocation);
|
||||
|
||||
String json = stringWriter.toString();
|
||||
|
||||
String normalizedActual = TestUtils.normalize(json, true);
|
||||
assertEquals("{}", normalizedActual);
|
||||
assertFalse(action.isExecuted());
|
||||
assertEquals("application/json", response.getContentType());
|
||||
assertEquals("UTF-8", response.getCharacterEncoding());
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
ActionConfig config = new ActionConfig.Builder("", "name", "").build();
|
||||
|
||||
Reference in New Issue
Block a user