[parancoe] push by lucio.be...@gmail.com - Added validator for password length. on 2013-12-05 15:07 GMT

1 view
Skip to first unread message

para...@googlecode.com

unread,
Dec 5, 2013, 10:08:08 AM12/5/13
to parancoe...@googlegroups.com
Revision: 9d4cc45f59a6
Branch: default
Author: lucio.benfante <lucio.b...@gmail.com>
Date: Thu Dec 5 15:07:09 2013 UTC
Log: Added validator for password length.
http://code.google.com/p/parancoe/source/detail?r=9d4cc45f59a6

Added:

/parancoe-validator/src/main/java/org/parancoe/validator/constraints/NewPasswordLength.java

/parancoe-validator/src/main/java/org/parancoe/validator/constraints/impl/NewPasswordLengthValidator.java
Modified:

/parancoe-validator/src/main/java/org/parancoe/validator/constraints/NewPassword.java

/parancoe-validator/src/main/java/org/parancoe/validator/constraints/impl/NewPasswordValidator.java

/parancoe-validator/src/main/resources/org/parancoe/validator/ValidationMessages.properties

/parancoe-validator/src/main/resources/org/parancoe/validator/ValidationMessages_en.properties

/parancoe-validator/src/main/resources/org/parancoe/validator/ValidationMessages_it.properties

/parancoe-validator/src/test/java/org/parancoe/validator/constraints/NewPasswordBean.java

/parancoe-validator/src/test/java/org/parancoe/validator/constraints/impl/NewPasswordValidatorTest.java

=======================================
--- /dev/null
+++
/parancoe-validator/src/main/java/org/parancoe/validator/constraints/NewPasswordLength.java
Thu Dec 5 15:07:09 2013 UTC
@@ -0,0 +1,110 @@
+/**
+ * Copyright (C) 2006-2010 The Parancoe Team <in...@parancoe.org>
+ *
+ * This file is part of Parancoe Validator.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.parancoe.validator.constraints;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import org.parancoe.validator.constraints.impl.NewPasswordLengthValidator;
+
+/**
+ * Validation annotation for the setting of a new password.
+ * The validation fails if the new password property is not blank and
+ * hasn't a length between min and max.
+ *
+ * Example:
+ * <pre>
+ * &#064;{@code NewPassword(newPasswordProperty="newPassword",
confirmPasswordProperty="confirmPassword")
+ * &#064;{@code NewPasswordLength(newPasswordProperty="newPassword",
confirmPasswordProperty="confirmPassword", min = 6)
+ * public class NewPasswordBean implements Serializable {
+ *
+ * protected String newPassword;
+ * protected String confirmPassword;
+ *
+ * public String getNewPassword() {
+ * return newPassword;
+ * }
+ *
+ * public void setNewPassword(String newPassword) {
+ * this.newPassword = newPassword;
+ * }
+ *
+ * public String getConfirmPassword() {
+ * return confirmPassword;
+ * }
+ *
+ * public void setConfirmPassword(String confirmPassword) {
+ * this.confirmPassword = confirmPassword;
+ * }
+ * }
+ * }</pre>
+ *
+ * @author Lucio Benfante
+ */
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Constraint(validatedBy = NewPasswordLengthValidator.class)
+@Documented
+public @interface NewPasswordLength {
+
+ String message()
default "{org.parancoe.validator.constraints.NewPassword.message}";
+
+ Class<?>[] groups() default {};
+
+ Class<? extends Payload>[] payload() default {};
+
+ /**
+ * The name of the property that contains the new password.
+ *
+ * @return The name of the property.
+ */
+ String newPasswordProperty() default "newPassword";
+
+ /**
+ * The name of the property that contains the confirm password.
+ *
+ * @return The name of the property.
+ */
+ String confirmPasswordProperty() default "confirmPassword";
+
+ /**
+ * The min length of the new password.
+ *
+ * @return The min length of the new password.
+ */
+ int min() default 0;
+
+ /**
+ * The min length of the new password.
+ *
+ * @return The min length of the new password.
+ */
+ int max() default Integer.MAX_VALUE;
+
+ /**
+ * Pass the validation if the new password is blank. Usually for not
changing the current password.
+ *
+ * @return True if the validation must pass if the new password is
blank.
+ */
+ boolean passIfBlank() default true;
+
+}
=======================================
--- /dev/null
+++
/parancoe-validator/src/main/java/org/parancoe/validator/constraints/impl/NewPasswordLengthValidator.java
Thu Dec 5 15:07:09 2013 UTC
@@ -0,0 +1,97 @@
+/**
+ * Copyright (C) 2006-2010 The Parancoe Team <in...@parancoe.org>
+ *
+ * This file is part of Parancoe Validator.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.parancoe.validator.constraints.impl;
+
+import java.beans.BeanInfo;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import org.apache.commons.lang.StringUtils;
+import org.parancoe.validator.constraints.NewPassword;
+import org.parancoe.validator.constraints.NewPasswordLength;
+
+/**
+ * A validator for the setting of a new password.
+ *
+ * @author Lucio Benfante
+ */
+public class NewPasswordLengthValidator implements
+ ConstraintValidator<NewPasswordLength, Object> {
+
+ private NewPasswordLength constraintAnnotation;
+
+ @Override
+ public void initialize(NewPasswordLength constraintAnnotation) {
+ this.constraintAnnotation = constraintAnnotation;
+ }
+
+ @Override
+ public boolean isValid(Object value, ConstraintValidatorContext
context) {
+ String newPasswordValue = null;
+ String confirmPasswordValue = null;
+ boolean newPasswordFound = false;
+ boolean confirmPasswordFound = false;
+ try {
+ BeanInfo beanInfo = Introspector.getBeanInfo(value.getClass());
+ PropertyDescriptor[] propertyDescriptors =
beanInfo.getPropertyDescriptors();
+ for (PropertyDescriptor propertyDescriptor :
propertyDescriptors) {
+ if
(constraintAnnotation.newPasswordProperty().equals(propertyDescriptor.getName()))
{
+ newPasswordValue = (String)
propertyDescriptor.getReadMethod().invoke(value);
+ newPasswordFound = true;
+ }
+ if (constraintAnnotation.confirmPasswordProperty().equals(
+ propertyDescriptor.getName())) {
+ confirmPasswordValue = (String)
propertyDescriptor.getReadMethod().invoke(value);
+ confirmPasswordFound = true;
+ }
+ if (newPasswordFound && confirmPasswordFound) {
+ break;
+ }
+ }
+ } catch (Exception ex) {
+ throw new RuntimeException("Can't validate this bean.", ex);
+ }
+ if (!newPasswordFound) {
+ throw new RuntimeException("Can't validate this bean:
property "
+ + constraintAnnotation.newPasswordProperty() + " not
found.");
+ }
+ if (!confirmPasswordFound) {
+ throw new RuntimeException("Can't validate this bean:
property "
+ + constraintAnnotation.confirmPasswordProperty() + "
not found.");
+ }
+ if (constraintAnnotation.passIfBlank()) {
+ if (value == null || (StringUtils.isBlank(newPasswordValue)
+ && StringUtils.isBlank(confirmPasswordValue))) {
+ return true;
+ }
+ }
+ boolean result = true;
+ if (StringUtils.isNotBlank(newPasswordValue)
+ && (newPasswordValue.length() < constraintAnnotation.min()
+ || newPasswordValue.length() >
constraintAnnotation.max())) {
+ context.disableDefaultConstraintViolation();
+ context.buildConstraintViolationWithTemplate(
+ "{org.parancoe.validator.constraints.NewPasswordLength.message}").
+ addNode("newPassword").
+
addConstraintViolation().disableDefaultConstraintViolation();
+ result = false;
+ }
+ return result;
+ }
+}
=======================================
---
/parancoe-validator/src/main/java/org/parancoe/validator/constraints/NewPassword.java
Wed Apr 6 15:50:21 2011 UTC
+++
/parancoe-validator/src/main/java/org/parancoe/validator/constraints/NewPassword.java
Thu Dec 5 15:07:09 2013 UTC
@@ -84,4 +84,12 @@
* @return The name of the property.
*/
String confirmPasswordProperty() default "confirmPassword";
+
+ /**
+ * Pass the validation if the new password is blank. Usually for not
changing the current password.
+ *
+ * @return True if the validation must pass if the new password is
blank.
+ */
+ boolean passIfBlank() default true;
+
}
=======================================
---
/parancoe-validator/src/main/java/org/parancoe/validator/constraints/impl/NewPasswordValidator.java
Wed Apr 6 15:50:21 2011 UTC
+++
/parancoe-validator/src/main/java/org/parancoe/validator/constraints/impl/NewPasswordValidator.java
Thu Dec 5 15:07:09 2013 UTC
@@ -74,22 +74,21 @@
throw new RuntimeException("Can't validate this bean:
property "
+ constraintAnnotation.confirmPasswordProperty() + "
not found.");
}
- boolean result = false;
- if (value == null || (StringUtils.isBlank(newPasswordValue)
- && StringUtils.isBlank(confirmPasswordValue))) {
- result = true;
- } else {
- if (StringUtils.isNotBlank(newPasswordValue)
- && newPasswordValue.equals(confirmPasswordValue)) {
- result = true;
+ if (constraintAnnotation.passIfBlank()) {
+ if (value == null || (StringUtils.isBlank(newPasswordValue)
+ && StringUtils.isBlank(confirmPasswordValue))) {
+ return true;
}
}
- if (!result) {
+ boolean result = true;
+ if (StringUtils.isNotBlank(newPasswordValue)
+ && !newPasswordValue.equals(confirmPasswordValue)) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(
"{org.parancoe.validator.constraints.NewPassword.message}").
addNode("newPassword").
- addConstraintViolation();
+
addConstraintViolation().disableDefaultConstraintViolation();
+ result = false;
}
return result;
}
=======================================
---
/parancoe-validator/src/main/resources/org/parancoe/validator/ValidationMessages.properties
Wed Apr 6 15:50:21 2011 UTC
+++
/parancoe-validator/src/main/resources/org/parancoe/validator/ValidationMessages.properties
Thu Dec 5 15:07:09 2013 UTC
@@ -1,1 +1,2 @@
org.parancoe.validator.constraints.NewPassword.message=The new and confirm
passwords are not equal.
+org.parancoe.validator.constraints.NewPasswordLength.message=The new
password must be at least {3} characters long.
=======================================
---
/parancoe-validator/src/main/resources/org/parancoe/validator/ValidationMessages_en.properties
Wed Apr 6 15:50:21 2011 UTC
+++
/parancoe-validator/src/main/resources/org/parancoe/validator/ValidationMessages_en.properties
Thu Dec 5 15:07:09 2013 UTC
@@ -1,1 +1,2 @@
org.parancoe.validator.constraints.NewPassword.message=The new and confirm
passwords are not equal.
+org.parancoe.validator.constraints.NewPasswordLength.message=The new
password must be at least {3} characters long.
=======================================
---
/parancoe-validator/src/main/resources/org/parancoe/validator/ValidationMessages_it.properties
Wed Apr 6 15:50:21 2011 UTC
+++
/parancoe-validator/src/main/resources/org/parancoe/validator/ValidationMessages_it.properties
Thu Dec 5 15:07:09 2013 UTC
@@ -1,1 +1,2 @@
org.parancoe.validator.constraints.NewPassword.message=La nuova password e
la conferma non sono uguali.
+org.parancoe.validator.constraints.NewPasswordLength.message=La nuova
password deve essere lunga almeno {3} caratteri.
=======================================
---
/parancoe-validator/src/test/java/org/parancoe/validator/constraints/NewPasswordBean.java
Wed Apr 6 15:50:21 2011 UTC
+++
/parancoe-validator/src/test/java/org/parancoe/validator/constraints/NewPasswordBean.java
Thu Dec 5 15:07:09 2013 UTC
@@ -26,6 +26,7 @@
* @author Lucio Benfante <lucio.b...@gmail.com>
*/
@NewPassword(newPasswordProperty="newPassword",
confirmPasswordProperty="confirmPassword")
+@NewPasswordLength(newPasswordProperty="newPassword",
confirmPasswordProperty="confirmPassword", min = 2, max = 10)
public class NewPasswordBean implements Serializable {

protected String newPassword;
=======================================
---
/parancoe-validator/src/test/java/org/parancoe/validator/constraints/impl/NewPasswordValidatorTest.java
Wed Apr 6 15:50:21 2011 UTC
+++
/parancoe-validator/src/test/java/org/parancoe/validator/constraints/impl/NewPasswordValidatorTest.java
Thu Dec 5 15:07:09 2013 UTC
@@ -77,4 +77,41 @@

assertEquals("{org.parancoe.validator.constraints.NewPassword.message}",
constraintViolation.getMessage());

assertEquals("{org.parancoe.validator.constraints.NewPassword.message}",
constraintViolation.getMessageTemplate());
}
+
+ public void testValidationFailWithShortPasswd() {
+ NewPasswordBean newPasswordBean =
+ new NewPasswordBean("s", "s");
+ Set<ConstraintViolation<NewPasswordBean>> constraintViolations =
+ this.hibernateValidator.validate(newPasswordBean);
+ assertEquals(1, constraintViolations.size());
+ ConstraintViolation<NewPasswordBean> constraintViolation =
constraintViolations.iterator().
+ next();
+ assertSame(newPasswordBean, constraintViolation.getRootBean());
+ Path propertyPath = constraintViolation.getPropertyPath();
+ Iterator<Node> itNodes = propertyPath.iterator();
+ assertTrue(itNodes.hasNext());
+ Node node = itNodes.next();
+ assertEquals("newPassword", node.getName());
+
assertEquals("{org.parancoe.validator.constraints.NewPasswordLength.message}",
constraintViolation.getMessage());
+
assertEquals("{org.parancoe.validator.constraints.NewPasswordLength.message}",
constraintViolation.getMessageTemplate());
+ }
+
+ public void testValidationFailWithLongPasswd() {
+ NewPasswordBean newPasswordBean =
+ new
NewPasswordBean("accidentichepasswordlunga", "accidentichepasswordlunga");
+ Set<ConstraintViolation<NewPasswordBean>> constraintViolations =
+ this.hibernateValidator.validate(newPasswordBean);
+ assertEquals(1, constraintViolations.size());
+ ConstraintViolation<NewPasswordBean> constraintViolation =
constraintViolations.iterator().
+ next();
+ assertSame(newPasswordBean, constraintViolation.getRootBean());
+ Path propertyPath = constraintViolation.getPropertyPath();
+ Iterator<Node> itNodes = propertyPath.iterator();
+ assertTrue(itNodes.hasNext());
+ Node node = itNodes.next();
+ assertEquals("newPassword", node.getName());
+
assertEquals("{org.parancoe.validator.constraints.NewPasswordLength.message}",
constraintViolation.getMessage());
+
assertEquals("{org.parancoe.validator.constraints.NewPasswordLength.message}",
constraintViolation.getMessageTemplate());
+ }
+
}
Reply all
Reply to author
Forward
0 new messages