WW-3952 Extends existing logic to handle credit card validation
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/0813eaa0 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/0813eaa0 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/0813eaa0 Branch: refs/heads/master Commit: 0813eaa0c8f446404243fba87cde51a619643cb9 Parents: 743d27f Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Wed Apr 19 18:07:16 2017 +0200 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Wed Apr 19 18:07:16 2017 +0200 ---------------------------------------------------------------------- ...nnotationValidationConfigurationBuilder.java | 39 ++++++++++++++++++++ .../validator/annotations/Validations.java | 2 + .../validators/RegexFieldValidator.java | 11 ++++-- .../validator/AnnotationValidationAction.java | 3 ++ core/src/test/resources/validators.xml | 1 + 5 files changed, 53 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/0813eaa0/core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java b/core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java index b587bf3..b2764ca 100644 --- a/core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java +++ b/core/src/main/java/com/opensymphony/xwork2/validator/AnnotationValidationConfigurationBuilder.java @@ -122,6 +122,14 @@ public class AnnotationValidationConfigurationBuilder { result.add(temp); } } + // Process CrediCardValidator + else if (a instanceof CreditCardValidator) { + CreditCardValidator v = (CreditCardValidator) a; + ValidatorConfig temp = processCreditCardValidatorAnnotation(v, fieldName, methodName); + if (temp != null) { + result.add(temp); + } + } // Process FieldExpressionValidator else if (a instanceof FieldExpressionValidator) { FieldExpressionValidator v = (FieldExpressionValidator) a; @@ -263,6 +271,15 @@ public class AnnotationValidationConfigurationBuilder { } } } + CreditCardValidator[] ccv = validations.creditCards(); + if (ccv != null) { + for (CreditCardValidator v : ccv) { + ValidatorConfig temp = processCreditCardValidatorAnnotation(v, fieldName, methodName); + if (temp != null) { + result.add(temp); + } + } + } FieldExpressionValidator[] fev = validations.fieldExpressions(); if (fev != null) { for (FieldExpressionValidator v : fev) { @@ -786,6 +803,28 @@ public class AnnotationValidationConfigurationBuilder { .build(); } + private ValidatorConfig processCreditCardValidatorAnnotation(CreditCardValidator v, String fieldName, String methodName) { + String validatorType = "creditcard"; + + Map<String, Object> params = new HashMap<>(); + + if (fieldName != null) { + params.put("fieldName", fieldName); + } else if (StringUtils.isNotEmpty(v.fieldName())) { + params.put("fieldName", v.fieldName()); + } + + validatorFactory.lookupRegisteredValidatorType(validatorType); + return new ValidatorConfig.Builder(validatorType) + .addParams(params) + .addParam("methodName", methodName) + .shortCircuit(v.shortCircuit()) + .defaultMessage(v.message()) + .messageKey(v.key()) + .messageParams(v.messageParams()) + .build(); + } + private ValidatorConfig processDateRangeFieldValidatorAnnotation(DateRangeFieldValidator v, String fieldName, String methodName) { String validatorType = "date"; http://git-wip-us.apache.org/repos/asf/struts/blob/0813eaa0/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validations.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validations.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validations.java index b4b640b..bdf5a75 100644 --- a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validations.java +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validations.java @@ -167,6 +167,8 @@ public @interface Validations { EmailValidator[] emails() default {}; + CreditCardValidator[] creditCards() default {}; + FieldExpressionValidator[] fieldExpressions() default {}; IntRangeFieldValidator[] intRangeFields() default {}; http://git-wip-us.apache.org/repos/asf/struts/blob/0813eaa0/core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java index eff835c..f6e9748 100644 --- a/core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java +++ b/core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java @@ -148,9 +148,14 @@ public class RegexFieldValidator extends FieldValidatorSupport { compare = compare.trim(); } - Matcher matcher = pattern.matcher(compare); - if (!matcher.matches()) { - addFieldError(fieldName, object); + try { + setCurrentValue(compare); + Matcher matcher = pattern.matcher(compare); + if (!matcher.matches()) { + addFieldError(fieldName, object); + } + } finally { + setCurrentValue(null); } } http://git-wip-us.apache.org/repos/asf/struts/blob/0813eaa0/core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java b/core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java index 6dbb429..9e95dde 100644 --- a/core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java +++ b/core/src/test/java/com/opensymphony/xwork2/validator/AnnotationValidationAction.java @@ -3,6 +3,7 @@ package com.opensymphony.xwork2.validator; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.validator.annotations.ConditionalVisitorFieldValidator; import com.opensymphony.xwork2.validator.annotations.ConversionErrorFieldValidator; +import com.opensymphony.xwork2.validator.annotations.CreditCardValidator; import com.opensymphony.xwork2.validator.annotations.CustomValidator; import com.opensymphony.xwork2.validator.annotations.DateRangeFieldValidator; import com.opensymphony.xwork2.validator.annotations.DoubleRangeFieldValidator; @@ -45,6 +46,8 @@ public class AnnotationValidationAction extends ActionSupport { messageParams = {"one", "two", "three"}) @EmailValidator(message = "Foo isn't a valid e-mail!", fieldName = "foo", key = "email.key", messageParams = {"one", "two", "three"}, shortCircuit = true) + @CreditCardValidator(message = "Foo isn't a valid credit card!", fieldName = "foo", key = "creditCard.key", + messageParams = {"one", "two", "three"}, shortCircuit = true) @ExpressionValidator(expression = "true", message = "Is not true!", key = "expression.key", messageParams = {"one", "two", "three"}, shortCircuit = true) @FieldExpressionValidator(expression = "true", fieldName = "foo", key = "fieldexpression.key", message = "It is not true!", http://git-wip-us.apache.org/repos/asf/struts/blob/0813eaa0/core/src/test/resources/validators.xml ---------------------------------------------------------------------- diff --git a/core/src/test/resources/validators.xml b/core/src/test/resources/validators.xml index 48369fa..5eff925 100644 --- a/core/src/test/resources/validators.xml +++ b/core/src/test/resources/validators.xml @@ -11,6 +11,7 @@ <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/> <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/> <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/> + <validator name="creditcard" class="com.opensymphony.xwork2.validator.validators.CreditCardValidator"/> <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/> <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/> <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>