Repository: struts
Updated Branches:
  refs/heads/develop bcd61a0de -> aa744b811

Extends validator to allow set predefined regex used to validate URLs


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

Branch: refs/heads/develop
Commit: 31be88afa28fb9b1e9854d0d7673ab9b979cf9be
Parents: bcd61a0
Author: Lukasz Lenart <lukaszlen...@apache.org>
Authored: Sun Mar 9 21:01:15 2014 +0100
Committer: Lukasz Lenart <lukaszlen...@apache.org>
Committed: Sun Mar 9 21:01:15 2014 +0100

----------------------------------------------------------------------
 .../validator/validators/URLValidator.java      | 46 ++++++++++++++++++
 .../xwork2/validator/URLValidatorTest.java      | 50 ++++++++++++++++++++
 2 files changed, 96 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/31be88af/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
----------------------------------------------------------------------
diff --git 
a/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
 
b/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
index b4a1287..4f63961 100644
--- 
a/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
+++ 
b/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/URLValidator.java
@@ -17,6 +17,7 @@ package com.opensymphony.xwork2.validator.validators;
 
 import com.opensymphony.xwork2.validator.ValidationException;
 import com.opensymphony.xwork2.util.URLUtil;
+import org.apache.commons.lang3.StringUtils;
 
 /**
  * <!-- START SNIPPET: javadoc -->
@@ -31,6 +32,8 @@ import com.opensymphony.xwork2.util.URLUtil;
  * 
  * <ul>
  *             <li>fieldName - The field name this validator is validating. 
Required if using Plain-Validator Syntax otherwise not required</li>
+ *             <li>urlRegexExpression - The regex defined as expression used 
to validate url. If not defined 'urlRegex' will be used instead</li>
+ *             <li>urlRegex - The regex used to validate url. If not defined 
default regex will be used</li>
  * </ul>
  * 
  * <!-- END SNIPPET: parameters -->
@@ -62,6 +65,9 @@ import com.opensymphony.xwork2.util.URLUtil;
  */
 public class URLValidator extends FieldValidatorSupport {
 
+    private String urlRegex;
+    private String urlRegexExpression;
+
     public void validate(Object object) throws ValidationException {
         String fieldName = getFieldName();
         Object value = this.getFieldValue(fieldName, object);
@@ -72,8 +78,48 @@ public class URLValidator extends FieldValidatorSupport {
             return;
         }
 
+        // FIXME deprecated! the same regex below should be used instead
+        // replace logic with next major release
         if (!(value.getClass().equals(String.class)) || 
!URLUtil.verifyUrl((String) value)) {
             addFieldError(fieldName, object);
         }
     }
+
+    /**
+     * This is used to support client-side validation, it's based on
+     * 
http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url
+     *
+     * @return regex to validate URLs
+     */
+    public String getUrlRegex() {
+        if (StringUtils.isNotEmpty(urlRegexExpression)) {
+            return (String) parse(urlRegexExpression, String.class);
+        } else if (StringUtils.isNotEmpty(urlRegex)) {
+            return urlRegex;
+        } else {
+            return "^(https?|ftp):\\/\\/" +
+                    "(([a-z0-9$_\\.\\+!\\*\\'\\(\\),;\\?&=-]|%[0-9a-f]{2})+" +
+                    
"(:([a-z0-9$_\\.\\+!\\*\\'\\(\\),;\\?&=-]|%[0-9a-f]{2})+)?" +
+                    "@)?(#?" +
+                    ")((([a-z0-9]\\.|[a-z0-9][a-z0-9-]*[a-z0-9]\\.)*" +
+                    "[a-z][a-z0-9-]*[a-z0-9]" +
+                    "|((\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])\\.){3}" +
+                    "(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])" +
+                    ")(:\\d+)?" +
+                    
")(((\\/+([a-z0-9$_\\.\\+!\\*\\'\\(\\),;:@&=-]|%[0-9a-f]{2})*)*" +
+                    
"(\\?([a-z0-9$_\\.\\+!\\*\\'\\(\\),;:@&=-]|%[0-9a-f]{2})*)" +
+                    "?)?)?" +
+                    "(#([a-z0-9$_\\.\\+!\\*\\'\\(\\),;:@&=-]|%[0-9a-f]{2})*)?" 
+
+                    "$";
+        }
+    }
+
+    public void setUrlRegex(String urlRegex) {
+        this.urlRegex = urlRegex;
+    }
+
+    public void setUrlRegexExpression(String urlRegexExpression) {
+        this.urlRegexExpression = urlRegexExpression;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/struts/blob/31be88af/xwork-core/src/test/java/com/opensymphony/xwork2/validator/URLValidatorTest.java
----------------------------------------------------------------------
diff --git 
a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/URLValidatorTest.java
 
b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/URLValidatorTest.java
index 9724895..f495557 100644
--- 
a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/URLValidatorTest.java
+++ 
b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/URLValidatorTest.java
@@ -17,9 +17,12 @@ package com.opensymphony.xwork2.validator;
 
 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.XWorkTestCase;
+import com.opensymphony.xwork2.util.URLUtil;
 import com.opensymphony.xwork2.util.ValueStack;
 import com.opensymphony.xwork2.validator.validators.URLValidator;
 
+import java.util.regex.Pattern;
+
 /**
  * Test case for URLValidator
  * 
@@ -103,6 +106,46 @@ public class URLValidatorTest extends XWorkTestCase {
                assertFalse(validator.getValidatorContext().hasFieldErrors());
        }
        
+       public void testValidUrlWithRegex() throws Exception {
+               URLValidator validator = new URLValidator();
+
+        validator.setUrlRegex("^myapp:\\/\\/[a-z]*\\.com$");
+
+        Pattern pattern = Pattern.compile(validator.getUrlRegex());
+
+        assertTrue(pattern.matcher("myapp://test.com").matches());
+        assertFalse(pattern.matcher("myap://test.com").matches());
+       }
+
+       public void testValidUrlWithRegexExpression() throws Exception {
+               URLValidator validator = new URLValidator();
+        ActionContext.getContext().getValueStack().push(new MyAction());
+        validator.setValueStack(ActionContext.getContext().getValueStack());
+        validator.setUrlRegexExpression("${urlRegex}");
+
+        Pattern pattern = Pattern.compile(validator.getUrlRegex());
+
+        assertTrue(pattern.matcher("myapp://test.com").matches());
+        assertFalse(pattern.matcher("myap://test.com").matches());
+       }
+
+       public void testValidUrlWithDefaultRegex() throws Exception {
+               URLValidator validator = new URLValidator();
+
+        Pattern pattern = Pattern.compile(validator.getUrlRegex());
+
+        assertFalse(pattern.matcher("myapp://test.com").matches());
+        assertFalse(pattern.matcher("myap://test.com").matches());
+        assertFalse(pattern.matcher("").matches());
+        assertFalse(pattern.matcher("   ").matches());
+        assertFalse(pattern.matcher("no url").matches());
+
+        assertTrue(pattern.matcher("http://www.opensymphony.com";).matches());
+        assertTrue(pattern.matcher("https://www.opensymphony.com";).matches());
+        
assertTrue(pattern.matcher("https://www.opensymphony.com:443/login";).matches());
+        assertTrue(pattern.matcher("http://localhost:8080/myapp";).matches());
+    }
+
        @Override
     protected void setUp() throws Exception {
            super.setUp();
@@ -140,4 +183,11 @@ public class URLValidatorTest extends XWorkTestCase {
                        return "http://yahoo.com/articles?id=123";;
                }
        }
+
+    class MyAction {
+
+        public String getUrlRegex() {
+            return "myapp:\\/\\/[a-z]*\\.com";
+        }
+    }
 }

Reply via email to