rave-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ja...@apache.org
Subject svn commit: r1162831 - in /incubator/rave/trunk/rave-portal/src: main/java/org/apache/rave/portal/web/controller/ main/java/org/apache/rave/portal/web/validator/ test/java/org/apache/rave/portal/web/validator/
Date Mon, 29 Aug 2011 14:29:42 GMT
Author: jasha
Date: Mon Aug 29 14:29:42 2011
New Revision: 1162831

URL: http://svn.apache.org/viewvc?rev=1162831&view=rev
Log:
RAVE-154 add email field to validator, get confirmPassword field. 
TODO: should we display the password in the form or only store it if the user wants to change
it

Modified:
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/UserProfileController.java
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java
    incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/UserProfileValidator.java
    incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/UserProfileValidatorTest.java

Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/UserProfileController.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/UserProfileController.java?rev=1162831&r1=1162830&r2=1162831&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/UserProfileController.java
(original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/controller/UserProfileController.java
Mon Aug 29 14:29:42 2011
@@ -52,6 +52,7 @@ public class UserProfileController {
 		  this.userProfileValidator=userProfileValidator;
     }
 
+    // TODO why .jsp?
     @RequestMapping(value ="/userProfile.jsp")
 	 public void setUpForm(ModelMap model) {
 		  logger.debug("Initializing form");
@@ -66,28 +67,19 @@ public class UserProfileController {
 		  
 		  userProfileValidator.validate(user,results);
 		  if(results.hasErrors()){
-			  logger.error("newaccount.jsp: shows validation errors");
+			  logger.info("userProfile.jsp: shows validation errors");
 			  return ViewNames.USER_PROFILE;
 		  }
 
-		  //Now attempt to create the account.
+		  //Now attempt to update the account.
 		  try {
 			    logger.debug("userprofile: passed form validation");
-			    
 			    userService.updateUserProfile(user);
 				return ViewNames.REDIRECT;
 		  }
-		  
-		  catch (org.springframework.dao.IncorrectResultSizeDataAccessException ex) {
-				//This exception is thrown if the account already exists.
-				logger.error("Account creation failed: "+ex.getMessage());
-				results.reject("Account already exists","Unable to create account");
-				return ViewNames.USER_PROFILE;
-				
-		  }
 		  //TODO need to handle more specific exceptions
 		  catch (Exception ex) {
-				logger.error("Account creation failed: "+ex.getMessage());
+				logger.info("Account creation failed: {}", ex.getMessage());
 				results.reject("Unable to create account:"+ex.getMessage(),"Unable to create account");
 				return ViewNames.USER_PROFILE;
 		  }

Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java?rev=1162831&r1=1162830&r2=1162831&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java
(original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/NewAccountValidator.java
Mon Aug 29 14:29:42 2011
@@ -58,7 +58,7 @@ public class NewAccountValidator impleme
         validateUsername(errors, newUser);
         validatePassword(errors, newUser);
         validateConfirmPassword(errors, newUser);
-        validateEmail(errors, newUser);
+        validateEmail(errors, newUser.getEmail());
 
         writeResultToLog(errors);
     }
@@ -105,12 +105,12 @@ public class NewAccountValidator impleme
         return !(newUser.getConfirmPassword().equals(newUser.getPassword()));
     }
 
-    private void validateEmail(Errors errors, NewUser newUser) {
-        if (StringUtils.isBlank(newUser.getEmail())) {
+    private void validateEmail(Errors errors, String email) {
+        if (StringUtils.isBlank(email)) {
             errors.rejectValue(FIELD_EMAIL, "email.required");
-        } else if (isInvalidEmailAddress(newUser.getEmail())) {
+        } else if (isInvalidEmailAddress(email)) {
             errors.rejectValue(FIELD_EMAIL, "email.invalid");
-        } else if (isExistingEmailAddress(newUser.getEmail())) {
+        } else if (isExistingEmailAddress(email)) {
             errors.rejectValue(FIELD_EMAIL, "email.exists");
         }
     }

Modified: incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/UserProfileValidator.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/UserProfileValidator.java?rev=1162831&r1=1162830&r2=1162831&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/UserProfileValidator.java
(original)
+++ incubator/rave/trunk/rave-portal/src/main/java/org/apache/rave/portal/web/validator/UserProfileValidator.java
Mon Aug 29 14:29:42 2011
@@ -20,6 +20,7 @@
 package org.apache.rave.portal.web.validator;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.validator.EmailValidator;
 import org.apache.rave.portal.model.User;
 import org.apache.rave.portal.service.UserService;
 import org.slf4j.Logger;
@@ -34,6 +35,7 @@ public class UserProfileValidator implem
     private final Logger logger = LoggerFactory.getLogger(getClass());
 
     private UserService userService;
+    private static final String FIELD_EMAIL = "email";
 
     @Autowired
     public UserProfileValidator(UserService userService) {
@@ -70,9 +72,38 @@ public class UserProfileValidator implem
             logger.info("Password mismatch");
         }
 
+        validateEmail(errors, user);
+
+        writeResultToLog(errors);
+    }
+
+    // TODO: copy of NewAccountValidator, should make this generic
+    private void validateEmail(Errors errors, User user) {
+        final String email = user.getEmail();
+        if (StringUtils.isBlank(email)) {
+            errors.rejectValue(FIELD_EMAIL, "email.required");
+        } else if (isInvalidEmailAddress(email)) {
+            errors.rejectValue(FIELD_EMAIL, "email.invalid");
+        } else if (isExistingEmailAddress(user, email)) {
+            errors.rejectValue(FIELD_EMAIL, "email.exists");
+        }
+    }
+
+    private boolean isInvalidEmailAddress(String emailAddress) {
+        return !EmailValidator.getInstance().isValid(emailAddress);
+    }
+
+    private boolean isExistingEmailAddress(User user, String email) {
+        final User userByEmail = userService.getUserByEmail(email);
+        return userByEmail != null && !userByEmail.equals(user);
+    }
+
+        private void writeResultToLog(Errors errors) {
         if (errors.hasErrors()) {
-            for (ObjectError error : errors.getAllErrors()) {
-                logger.info("Validation error: {}", error.toString());
+            if (logger.isInfoEnabled()) {
+                for (ObjectError error : errors.getAllErrors()) {
+                    logger.info("Validation error: {}", error.toString());
+                }
             }
         } else {
             logger.debug("Validation successful");

Modified: incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/UserProfileValidatorTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/UserProfileValidatorTest.java?rev=1162831&r1=1162830&r2=1162831&view=diff
==============================================================================
--- incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/UserProfileValidatorTest.java
(original)
+++ incubator/rave/trunk/rave-portal/src/test/java/org/apache/rave/portal/web/validator/UserProfileValidatorTest.java
Mon Aug 29 14:29:42 2011
@@ -37,6 +37,7 @@ import static org.easymock.EasyMock.crea
 public class UserProfileValidatorTest {
     private static final String VALID_NAME = "valid.name";
     private static final String VALID_PASSWORD = "valid.password";
+    private static final String VALID_EMAIL = "valid@example.com";
     private static final String FIELD_PASSWORD = "password";
     private static final String FIELD_CONFIRM_PASSWORD = "confirmPassword";
     private static final String USER = "user";
@@ -54,6 +55,7 @@ public class UserProfileValidatorTest {
         user.setUsername(VALID_NAME);
         user.setPassword(VALID_PASSWORD);
         user.setConfirmPassword(VALID_PASSWORD);
+        user.setEmail(VALID_EMAIL);
 
         Errors errors = new BindException(user, USER);
         validator.validate(user, errors);
@@ -65,6 +67,7 @@ public class UserProfileValidatorTest {
     public void testValidateFailsOnEmptyPassword() throws Exception {
         User user = new User();
         user.setUsername(VALID_NAME);
+        user.setEmail(VALID_EMAIL);
 
         Errors errors = new BindException(user, USER);
         validator.validate(user, errors);



Mime
View raw message