Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.local.ask.controller.spring; import com.local.ask.exception.UniqueCorruptionException; import com.local.ask.exception.UserAlreadyExistException; import com.local.ask.exception.UserDoesNotExistException; import com.local.ask.form.LoginUser; import com.local.ask.form.ReconfirmUser; import com.local.ask.form.SignUpUser; import com.local.ask.model.UserTemp; import java.sql.Timestamp; import java.util.Date; import javax.validation.Valid; import org.apache.shiro.authz.annotation.RequiresGuest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * * @author Olayinka */ @Controller public class SignUpController extends BaseController { @RequiresGuest @RequestMapping(value = "/signup", method = RequestMethod.GET) public String loadSignUpForm(Model m) { return "signup"; } @RequiresGuest @RequestMapping(value = "/signup", method = RequestMethod.POST) public String submitSignUpForm(@Valid SignUpUser signUpUser, BindingResult result, Model m) { try { UserTemp userTemp = new UserTemp(signUpUser); if (!result.hasErrors() && dbHelper.addUserTemp(userTemp) > 0) { mailManager.confirmRegistration(userTemp); m.addAttribute("message", "Successfully signed up"); return "registered"; } } catch (UserAlreadyExistException ex) { m.addAttribute("message", "It seems this email is already registered. Try log in."); } catch (UniqueCorruptionException ex) { m.addAttribute("message", "There is an error validating your request, please try again."); } return "signup"; } @RequiresGuest @RequestMapping(value = "/registered/confirm/{id}", method = RequestMethod.GET) public String confirmRegistration(@PathVariable String id, Model m) throws Exception { try { UserTemp userTemp = dbHelper.getUserTemp(id); Long day = 24 * 60 * 60 * 1000L; if (userTemp != null && userTemp.getTimeJoined().after(new Timestamp(new Date().getTime() - day))) { dbHelper.addUser(userTemp); LoginUser loginUser = new LoginUser(); loginUser.setEmail(userTemp.getEmail()); m.addAttribute(loginUser); m.addAttribute(new SignUpUser()); m.addAttribute("confirm", "message.confirmed"); return "confirm"; } else if (userTemp != null) { m.addAttribute("reconfirmUser", new ReconfirmUser()); return REDIRECT + "/reconfirm"; } } catch (UniqueCorruptionException ex) { //TODO database trigger. ex.printStackTrace(); } catch (UserAlreadyExistException ex) { ex.printStackTrace(); m.addAttribute("message", "It seems you're already registered."); throw new Exception(); } catch (UserDoesNotExistException ex) { ex.printStackTrace(); m.addAttribute("message", "This link is invalid."); throw new Exception(); } return REDIRECT + "/error"; } @RequiresGuest @RequestMapping(value = "/reconfirm", method = RequestMethod.POST) public String submitReconfirmForm(@Valid ReconfirmUser reconfirmUser, BindingResult result, Model m) { try { UserTemp userTemp = new UserTemp(reconfirmUser); if (result.hasErrors()) { return "reconfirm"; } userTemp = dbHelper.updateUserTemp(userTemp); mailManager.confirmRegistration(userTemp); m.addAttribute("message", "Successfully confirmed"); return "reconfirm_success"; } catch (UserDoesNotExistException ex) { ex.printStackTrace(); m.addAttribute("message", "It seems your email is not registered."); } catch (UniqueCorruptionException ex) { //TODO database trigger. ex.printStackTrace(); } return BaseController.REDIRECT + "/error"; } @RequiresGuest @RequestMapping(value = "/reconfirm", method = RequestMethod.GET) public String submitReconfirmForm(Model m) { return "reconfirm"; } }