Chatto/src/main/java/org/ros/chatto/controller/RegistrationController.java

104 lines
3.7 KiB
Java
Raw Normal View History

2019-09-27 06:37:55 +00:00
package org.ros.chatto.controller;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import javax.imageio.ImageIO;
import javax.validation.Valid;
2019-09-27 06:37:55 +00:00
2019-10-02 16:25:46 +00:00
import org.ros.chatto.dto.UserRegistrationDTO;
import org.ros.chatto.service.CaptchaService;
2019-09-27 06:37:55 +00:00
import org.ros.chatto.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
2019-09-27 06:37:55 +00:00
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
2019-09-27 06:37:55 +00:00
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
2019-09-27 06:37:55 +00:00
import org.springframework.web.bind.annotation.PostMapping;
2020-05-27 11:17:13 +00:00
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
2019-09-27 06:37:55 +00:00
@Controller
2020-05-27 11:17:13 +00:00
@RequiredArgsConstructor
@Slf4j
public class RegistrationController {
2019-09-27 06:37:55 +00:00
@Autowired
2020-05-27 11:17:13 +00:00
private final UserService userService;
@Autowired
2020-05-27 11:17:13 +00:00
private final CaptchaService captchaService;
// FIXME must change this to a timeout base cache otherwise memory leak!
private final Map<Long, String> captchaMap = new ConcurrentHashMap<>();
2019-09-27 06:37:55 +00:00
@GetMapping("/registration")
public String registrationForm(Model model) {
UserRegistrationDTO userRegistrationDTO = new UserRegistrationDTO();
String captchaText = captchaService.getRandomText();
userRegistrationDTO.setCaptchaText(captchaText);
2020-05-27 11:17:13 +00:00
log.debug("captcha text = {}", captchaText);
Long captchaID = ThreadLocalRandom.current().nextLong();
userRegistrationDTO.setCaptchaID(captchaID);
captchaMap.put(captchaID, captchaText);
model.addAttribute("userRegistrationDTO", userRegistrationDTO);
return "registration";
2019-09-27 06:37:55 +00:00
}
2019-09-27 06:37:55 +00:00
@PostMapping("/perform_registration")
public String performRegistration(
@ModelAttribute("userRegistrationDTO") @Valid UserRegistrationDTO userRegistrationDTO,
BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
2020-05-27 11:17:13 +00:00
log.warn("Registration input has errors!");
return "redirect:registration?error";
}
if (userService.getUser(userRegistrationDTO.getUserName())
.isPresent()) {
return "redirect:registration?error&duplicate=true";
}
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()))) {
2020-05-27 11:17:13 +00:00
log.info("Registration captcha equal success");
userService.createUser(userRegistrationDTO);
2020-01-09 07:24:15 +00:00
return "redirect:registration?success";
} else {
2020-05-27 11:17:13 +00:00
log.warn("Registration captcha equal fail");
return "redirect:registration?error&captchaError=true";
}
2019-09-27 06:37:55 +00:00
}
2019-11-29 19:13:50 +00:00
@GetMapping(value = "/img/captcha/{image_id}", produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<byte[]> 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);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(captchaBufferedImage, "png", baos);
byte[] imageBytes = baos.toByteArray();
return new ResponseEntity<byte[]>(imageBytes, headers, HttpStatus.OK);
}
2019-09-27 06:37:55 +00:00
}