From 90ed86400b7db3a345aa49922bdbb4c3c62e70dd Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Wed, 27 May 2020 16:47:13 +0530 Subject: [PATCH] Updated registration controller --- .../controller/RegistrationController.java | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/chatto/src/main/java/org/ros/chatto/controller/RegistrationController.java b/chatto/src/main/java/org/ros/chatto/controller/RegistrationController.java index a971337..d5a342e 100644 --- a/chatto/src/main/java/org/ros/chatto/controller/RegistrationController.java +++ b/chatto/src/main/java/org/ros/chatto/controller/RegistrationController.java @@ -28,16 +28,19 @@ import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + @Controller +@RequiredArgsConstructor +@Slf4j public class RegistrationController { @Autowired - private UserService userService; + private final UserService userService; @Autowired - private CaptchaService captchaService; - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final CaptchaService captchaService; private final Map captchaMap = new ConcurrentHashMap<>(); @@ -46,7 +49,7 @@ public class RegistrationController { UserRegistrationDTO userRegistrationDTO = new UserRegistrationDTO(); String captchaText = captchaService.getRandomText(); userRegistrationDTO.setCaptchaText(captchaText); - logger.debug("captcha text = {}", captchaText); + log.debug("captcha text = {}", captchaText); Long captchaID = ThreadLocalRandom.current().nextLong(); userRegistrationDTO.setCaptchaID(captchaID); captchaMap.put(captchaID, captchaText); @@ -59,28 +62,33 @@ public class RegistrationController { @ModelAttribute("userRegistrationDTO") @Valid UserRegistrationDTO userRegistrationDTO, BindingResult bindingResult) { if (bindingResult.hasErrors()) { - logger.warn("Registration input has errors!"); + log.warn("Registration input has errors!"); return "registration"; } - logger.debug("Captcha text from user input = {}", userRegistrationDTO.getCaptchaInput()); - logger.debug("Captcha text from captcha map = {}", captchaMap.get(userRegistrationDTO.getCaptchaID())); - if (userRegistrationDTO.getCaptchaInput().equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) { - logger.info("Registration captcha equal success"); + log.debug("Captcha text from user input = {}", + userRegistrationDTO.getCaptchaInput()); + log.debug("Captcha text from captcha map = {}", + captchaMap.get(userRegistrationDTO.getCaptchaID())); + if (userRegistrationDTO.getCaptchaInput() + .equals(captchaMap.get(userRegistrationDTO.getCaptchaID()))) { + log.info("Registration captcha equal success"); userService.createUser(userRegistrationDTO); return "redirect:registration?success"; } else { - logger.warn("Registration captcha equal fail"); + log.warn("Registration captcha equal fail"); return "redirect:registration?error"; } } @GetMapping(value = "/img/captcha/{image_id}", produces = MediaType.IMAGE_PNG_VALUE) - public ResponseEntity getImage(@PathVariable("image_id") Long imageId) throws IOException { + public ResponseEntity getImage( + @PathVariable("image_id") Long imageId) throws IOException { final String captchaText = captchaMap.get(imageId); final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); - BufferedImage captchaBufferedImage = captchaService.createCaptchaImage(captchaText); + BufferedImage captchaBufferedImage = captchaService + .createCaptchaImage(captchaText); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(captchaBufferedImage, "png", baos);