diff --git a/vraptor/src/main/java/com/baeldung/controllers/AuthController.java b/vraptor/src/main/java/com/baeldung/controllers/AuthController.java index 7f93e59606..746f3b0edc 100644 --- a/vraptor/src/main/java/com/baeldung/controllers/AuthController.java +++ b/vraptor/src/main/java/com/baeldung/controllers/AuthController.java @@ -1,8 +1,6 @@ package com.baeldung.controllers; -import br.com.caelum.vraptor.Controller; -import br.com.caelum.vraptor.Path; -import br.com.caelum.vraptor.Result; +import br.com.caelum.vraptor.*; import br.com.caelum.vraptor.freemarker.FreemarkerView; import br.com.caelum.vraptor.validator.Validator; import com.baeldung.config.UserInfo; @@ -36,60 +34,61 @@ public class AuthController { this.userInfo = userInfo; } - @Path("/register") + @Get("/register") + public void registrationForm() { + result.use(FreemarkerView.class).withTemplate("auth/register"); + } + + @Post("/register") public void register(User user, HttpServletRequest request) { - if(request.getMethod().equals("GET")) { - result.use(FreemarkerView.class).withTemplate("auth/register"); - } - else if(request.getMethod().equals("POST")) { - validator.validate(user); - validator.onErrorRedirectTo(this).register(user, request); - if(!user.getPassword().equals(request.getParameter("password_confirmation"))) { - result.include("error", "Passwords Do Not Match"); - result.redirectTo(this).register(null, request); - } - user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt())); - Object resp = userDao.add(user); - if(resp != null) { - result.include("status", "Registration Successful! Now Login"); - result.redirectTo(this).login(request); - } else { - result.include("error", "There was an error during registration"); - result.redirectTo(this).register(user, request); - } + validator.validate(user); + if(validator.hasErrors()) { + result.include("errors", validator.getErrors()); + } + + validator.onErrorRedirectTo(this).registrationForm(); + + if(!user.getPassword().equals(request.getParameter("password_confirmation"))) { + result.include("error", "Passwords Do Not Match"); + result.redirectTo(this).registrationForm(); + } + + user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt())); + + Object resp = userDao.add(user); + + if(resp != null) { + result.include("status", "Registration Successful! Now Login"); + result.redirectTo(this).loginForm(); } else { - result.notFound(); + result.include("error", "There was an error during registration"); + result.redirectTo(this).registrationForm(); } } - @Path("/login") + @Get("/login") + public void loginForm() { + result.use(FreemarkerView.class).withTemplate("auth/login"); + } + + @Post("/login") public void login(HttpServletRequest request) { - if(request.getMethod().equals("GET")) { - result.use(FreemarkerView.class).withTemplate("auth/login"); + if (request.getParameter("user.email").isEmpty() || request.getParameter("user.password").isEmpty()) { + result.include("error", "Email/Password is Required!"); + result.redirectTo(AuthController.class).loginForm(); } - else if(request.getMethod().equals("POST")) { - if (request.getParameter("user.email").isEmpty() || request.getParameter("user.password").isEmpty()) { - result.include("error", "Email/Password is Required!"); - result.redirectTo(AuthController.class).login(request); - } - - User user = userDao.findByEmail(request.getParameter("user.email")); - if (Objects.nonNull(user) && BCrypt.checkpw(request.getParameter("user.password"), user.getPassword())) { - userInfo.setUser(user); - result.include("status", "Login Successful!"); - result.redirectTo(IndexController.class).index(); - } else { - result.include("error", "Email/Password Does Not Match!"); - result.redirectTo(AuthController.class).login(request); - } - } - else { - result.include("error", "Invalid Request"); - result.notFound(); + User user = userDao.findByEmail(request.getParameter("user.email")); + if (Objects.nonNull(user) && BCrypt.checkpw(request.getParameter("user.password"), user.getPassword())) { + userInfo.setUser(user); + result.include("status", "Login Successful!"); + result.redirectTo(IndexController.class).index(); + } else { + result.include("error", "Email/Password Does Not Match!"); + result.redirectTo(AuthController.class).loginForm(); } } } diff --git a/vraptor/src/main/java/com/baeldung/controllers/PostController.java b/vraptor/src/main/java/com/baeldung/controllers/PostController.java index dfb7589120..655b248350 100644 --- a/vraptor/src/main/java/com/baeldung/controllers/PostController.java +++ b/vraptor/src/main/java/com/baeldung/controllers/PostController.java @@ -34,42 +34,35 @@ public class PostController { this.validator = validator; } - - @Path(value = "/post/add") - public void add(Post post, HttpServletRequest request) { - - if(request.getMethod().equals("GET")) { + @Get("/post/add") + public void addForm() { if(Objects.isNull(userInfo.getUser())) { - result.include("error", "Please Login to Proceed"); - result.redirectTo(AuthController.class).login(request); - return; + result.include("error", "Please Login to Proceed"); + result.redirectTo(AuthController.class).loginForm(); + return; } result.use(FreemarkerView.class).withTemplate("posts/add"); - } - else if(request.getMethod().equals("POST")) { + } - post.setAuthor(userInfo.getUser()); + @br.com.caelum.vraptor.Post("/post/add") + public void add(Post post) { - validator.validate(post); - if(validator.hasErrors()) - result.include("error", "Post's Title (min of 5 chars) and Body (min of 10 chars) is Required"); - validator.onErrorRedirectTo(this).add(post, request); + post.setAuthor(userInfo.getUser()); + validator.validate(post); + if(validator.hasErrors()) + result.include("errors", validator.getErrors()); + validator.onErrorRedirectTo(this).addForm(); - Object id = postDao.add(post); + Object id = postDao.add(post); - if(Objects.nonNull(id)) { - result.include("status", "Post Added Successfully"); - result.redirectTo(IndexController.class).index(); - } else { - result.include("error", "There was an error creating the post. Try Again"); - result.redirectTo(this).add(post, request); - } - } - else { - result.include("error", "Invalid Request Type"); + if(Objects.nonNull(id)) { + result.include("status", "Post Added Successfully"); result.redirectTo(IndexController.class).index(); + } else { + result.include("error", "There was an error creating the post. Try Again"); + result.redirectTo(this).addForm(); } } diff --git a/vraptor/src/main/java/com/baeldung/models/User.java b/vraptor/src/main/java/com/baeldung/models/User.java index 69af299ca6..b0f3bd9500 100644 --- a/vraptor/src/main/java/com/baeldung/models/User.java +++ b/vraptor/src/main/java/com/baeldung/models/User.java @@ -1,5 +1,7 @@ package com.baeldung.models; +import org.hibernate.validator.constraints.Email; + import javax.persistence.*; import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; @@ -20,17 +22,7 @@ public class User { private String name; @Column(unique = true) - @NotNull @Pattern(regexp = "" + - "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+" + - "(?:\\\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"" + - "(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\" + - "[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@" + - "(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9]" + - "(?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}" + - "(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]" + - ":(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\" + - "[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])", - message = "Invalid Email Address") + @NotNull @Email private String email; @NotNull diff --git a/vraptor/src/main/resources/templates/auth/register.ftl b/vraptor/src/main/resources/templates/auth/register.ftl index 9ac8d42621..c65264c834 100644 --- a/vraptor/src/main/resources/templates/auth/register.ftl +++ b/vraptor/src/main/resources/templates/auth/register.ftl @@ -87,6 +87,13 @@

Register

${error!""} ${status!""}
+ <#if errors??> + <#list errors as error> +
${error.category?upper_case}: ${error.message}
+ <#else> + + <#else> +
diff --git a/vraptor/src/main/resources/templates/posts/add.ftl b/vraptor/src/main/resources/templates/posts/add.ftl index 3ef744a49e..de036cea20 100644 --- a/vraptor/src/main/resources/templates/posts/add.ftl +++ b/vraptor/src/main/resources/templates/posts/add.ftl @@ -73,7 +73,14 @@

Compose the Next Viral Message!

${error!""} ${status!""}
- + <#if errors??> + <#list errors as error> +
${error.category?upper_case}: ${error.message}
+ <#else> + + <#else> + +
@@ -95,7 +102,7 @@
- +