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 org.kievguide.controller; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; import java.security.SecureRandom; import javax.inject.Inject; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.kievguide.entity.Place; import org.kievguide.entity.User; import org.kievguide.service.CommentService; import org.kievguide.service.PlaceService; import org.kievguide.service.StationService; import org.kievguide.service.UserService; import org.kievguide.service.VoteService; import org.kievguide.util.Util; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; /** * * @author denis */ @Controller public class UserController { @Inject @Qualifier("userService") private UserService userService; @Inject @Qualifier("placeService") private PlaceService placeService; @Inject @Qualifier("voteService") private VoteService voteService; @Inject @Qualifier("commentService") private CommentService commentService; @Inject @Qualifier("stationService") private StationService stationService; @RequestMapping(value = "/signinredirect", method = RequestMethod.GET) public ModelAndView signIn() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("sign-in"); return modelAndView; } @RequestMapping(value = "/signinaction", method = RequestMethod.POST) public ModelAndView signInAction(@RequestParam("email") String email, @RequestParam("password") String password, HttpServletResponse response) { ModelAndView modelAndView = new ModelAndView(); User user = null; user = userService.searchUser(email); if (user.getPassword().equals(password)) { Cookie userCookie = new Cookie("userstatus", email); response.addCookie(userCookie); String userStatus = Util.userPanel(email); modelAndView.addObject("userstatus", userStatus); modelAndView.addObject("user", user); modelAndView.setViewName("redirect:" + "firstrequest"); return modelAndView; } else { modelAndView.setViewName("error"); return modelAndView; } } @RequestMapping(value = "/signupredirect", method = RequestMethod.GET) public ModelAndView signUp() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("sign-up"); return modelAndView; } @RequestMapping(value = "/signupaction", method = RequestMethod.POST) public ModelAndView signUpAction(@RequestParam("firstname") String firstname, @RequestParam("lastname") String lastname, @RequestParam("email") String email, @RequestParam("password") String password) { ModelAndView modelAndView = new ModelAndView(); User user = new User(); user.setFirstname(firstname); user.setLastname(lastname); user.setPassword(password); user.setEmail(email); user.setPhotosrc("img/userpicdefault.png"); userService.addUser(user); modelAndView.setViewName("sign-in"); return modelAndView; } @RequestMapping(value = "/settingsredirect", method = RequestMethod.GET) public ModelAndView settingsRedirect( @CookieValue(value = "userstatus", defaultValue = "guest") String useremail) { ModelAndView modelAndView = new ModelAndView(); User user = userService.searchUser(useremail); String userStatus = Util.userPanel(useremail); modelAndView.addObject("userstatus", userStatus); modelAndView.addObject("user", user); modelAndView.setViewName("settings"); return modelAndView; } @RequestMapping(value = "/settingssave", method = RequestMethod.POST) public ModelAndView settingsSave(@CookieValue(value = "userstatus", defaultValue = "guest") String useremail, @RequestParam("firstname") String firstname, @RequestParam("lastname") String lastname, @RequestParam("email") String email, @RequestParam("password") String password, @RequestParam("photosrc") MultipartFile file, HttpServletResponse response, HttpServletRequest request) throws FileNotFoundException, IOException { ModelAndView modelAndView = new ModelAndView(); SecureRandom random = new SecureRandom(); String photoname = new BigInteger(130, random).toString(32); Place place = new Place(); User user = userService.searchUser(useremail); user.setFirstname(firstname); user.setLastname(lastname); user.setPassword(password); user.setEmail(email); if (!file.isEmpty()) { String folder = request.getSession().getServletContext().getRealPath(""); folder = folder.substring(0, 30); BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream(new File(folder + "/src/main/webapp/img/" + photoname + ".jpg"))); FileCopyUtils.copy(file.getInputStream(), stream); stream.close(); user.setPhotosrc("img/" + photoname + ".jpg"); } userService.addUser(user); Cookie userCookie = new Cookie("userstatus", user.getEmail()); response.addCookie(userCookie); String userStatus = Util.userPanel(user.getEmail()); modelAndView.addObject("userstatus", userStatus); return new ModelAndView("redirect:" + "firstrequest"); } @RequestMapping(value = "/logout", method = RequestMethod.GET) public ModelAndView logOut(HttpServletResponse response) { ModelAndView modelAndView = new ModelAndView(); Cookie logOutCookie = new Cookie("userstatus", "guest"); String userStatus = Util.guestPanel(); modelAndView.addObject("userstatus", userStatus); response.addCookie(logOutCookie); modelAndView.setViewName("redirect:" + "signinredirect"); return modelAndView; } }