Return-Path: X-Original-To: apmail-struts-commits-archive@minotaur.apache.org Delivered-To: apmail-struts-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 87EB810B22 for ; Sun, 9 Mar 2014 20:04:55 +0000 (UTC) Received: (qmail 87973 invoked by uid 500); 9 Mar 2014 20:04:53 -0000 Delivered-To: apmail-struts-commits-archive@struts.apache.org Received: (qmail 87816 invoked by uid 500); 9 Mar 2014 20:04:51 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 87730 invoked by uid 99); 9 Mar 2014 20:04:49 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 09 Mar 2014 20:04:49 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 9918193B8E5; Sun, 9 Mar 2014 20:04:49 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: lukaszlenart@apache.org To: commits@struts.apache.org Date: Sun, 09 Mar 2014 20:04:49 -0000 Message-Id: <8fffe6c738ea490c98363c1b9433a282@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/4] git commit: Extends validator to allow set predefined regex used to validate URLs Repository: struts Updated Branches: refs/heads/feature/WW-4198-update-form-close [created] e66a30629 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/feature/WW-4198-update-form-close Commit: 31be88afa28fb9b1e9854d0d7673ab9b979cf9be Parents: bcd61a0 Author: Lukasz Lenart Authored: Sun Mar 9 21:01:15 2014 +0100 Committer: Lukasz Lenart 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; /** * @@ -31,6 +32,8 @@ import com.opensymphony.xwork2.util.URLUtil; * *
    *
  • fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required
  • + *
  • urlRegexExpression - The regex defined as expression used to validate url. If not defined 'urlRegex' will be used instead
  • + *
  • urlRegex - The regex used to validate url. If not defined default regex will be used
  • *
* * @@ -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"; + } + } }