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 2017/01/09 10:40:22 UTC

struts git commit: WW-4728 Allows override request parameter names used to enable validation

Repository: struts
Updated Branches:
  refs/heads/master a7f4e255b -> 08e181a4f


WW-4728 Allows override request parameter names used to enable validation


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/08e181a4
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/08e181a4
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/08e181a4

Branch: refs/heads/master
Commit: 08e181a4febb4e999e5e3366a0890eb1d5a953e5
Parents: a7f4e25
Author: Lukasz Lenart <lu...@apache.org>
Authored: Mon Jan 9 11:40:13 2017 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Mon Jan 9 11:40:13 2017 +0100

----------------------------------------------------------------------
 .../struts2/json/JSONValidationInterceptor.java | 98 ++++++++++----------
 .../json/JSONValidationInterceptorTest.java     | 49 +++++++++-
 2 files changed, 96 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/08e181a4/plugins/json/src/main/java/org/apache/struts2/json/JSONValidationInterceptor.java
----------------------------------------------------------------------
diff --git a/plugins/json/src/main/java/org/apache/struts2/json/JSONValidationInterceptor.java b/plugins/json/src/main/java/org/apache/struts2/json/JSONValidationInterceptor.java
index 20ffb50..ab91f24 100644
--- a/plugins/json/src/main/java/org/apache/struts2/json/JSONValidationInterceptor.java
+++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONValidationInterceptor.java
@@ -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>
- * &lt;interceptor-stack name="jsonValidationWorkflowStack"&gt;
- *      &lt;interceptor-ref name="basicStack"/&gt;
- *      &lt;interceptor-ref name="validation"&gt;
- *            &lt;param name="excludeMethods"&gt;input,back,cancel&lt;/param&gt;
- *      &lt;/interceptor-ref&gt;
- *      &lt;interceptor-ref name="jsonValidation"/&gt;
- *      &lt;interceptor-ref name="workflow"/&gt;
- * &lt;/interceptor-stack&gt;
- * </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;
+    }
 }

http://git-wip-us.apache.org/repos/asf/struts/blob/08e181a4/plugins/json/src/test/java/org/apache/struts2/json/JSONValidationInterceptorTest.java
----------------------------------------------------------------------
diff --git a/plugins/json/src/test/java/org/apache/struts2/json/JSONValidationInterceptorTest.java b/plugins/json/src/test/java/org/apache/struts2/json/JSONValidationInterceptorTest.java
index 3f3103f..24ac417 100644
--- a/plugins/json/src/test/java/org/apache/struts2/json/JSONValidationInterceptorTest.java
+++ b/plugins/json/src/test/java/org/apache/struts2/json/JSONValidationInterceptorTest.java
@@ -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();