From d88f67ce00a8e85d2ed8f90fa678979d8549cbcb Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Wed, 15 May 2019 03:08:21 +0400 Subject: [PATCH] 87 transactional inviting --- .../user/controller/UserControllerV2.java | 8 ++- .../user/error/UserSendingMailException.java | 7 +++ .../ru/ulstu/user/service/MailService.java | 55 ++++++++++++------- .../ru/ulstu/user/service/UserService.java | 19 +++---- 4 files changed, 54 insertions(+), 35 deletions(-) create mode 100644 src/main/java/ru/ulstu/user/error/UserSendingMailException.java diff --git a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java b/src/main/java/ru/ulstu/user/controller/UserControllerV2.java index 3447a49..dfd14d8 100644 --- a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java +++ b/src/main/java/ru/ulstu/user/controller/UserControllerV2.java @@ -10,10 +10,12 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.configuration.Constants; import ru.ulstu.odin.controller.OdinController; +import ru.ulstu.user.error.UserSendingMailException; import ru.ulstu.user.model.*; import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserSessionService; +import javax.mail.MessagingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; @@ -36,7 +38,7 @@ public class UserControllerV2 extends OdinController { private final UserSessionService userSessionService; public UserControllerV2(UserService userService, - UserSessionService userSessionService) { + UserSessionService userSessionService) { super(UserListDto.class, UserDto.class); this.userService = userService; this.userSessionService = userSessionService; @@ -57,8 +59,8 @@ public class UserControllerV2 extends OdinController { modelMap.addAttribute("userDto", userService.updateUserInformation(user, userDto)); } - @PostMapping("/invite" ) - public String inviteUser(@RequestParam(value = "email") String email){ + @PostMapping("/invite") + public String inviteUser(@RequestParam(value = "email") String email) throws UserSendingMailException { userService.inviteUser(email); return "redirect:/"; } diff --git a/src/main/java/ru/ulstu/user/error/UserSendingMailException.java b/src/main/java/ru/ulstu/user/error/UserSendingMailException.java new file mode 100644 index 0000000..576e834 --- /dev/null +++ b/src/main/java/ru/ulstu/user/error/UserSendingMailException.java @@ -0,0 +1,7 @@ +package ru.ulstu.user.error; + +public class UserSendingMailException extends RuntimeException { + public UserSendingMailException(String message) { + super(message); + } +} diff --git a/src/main/java/ru/ulstu/user/service/MailService.java b/src/main/java/ru/ulstu/user/service/MailService.java index da8e098..59d7e51 100644 --- a/src/main/java/ru/ulstu/user/service/MailService.java +++ b/src/main/java/ru/ulstu/user/service/MailService.java @@ -3,6 +3,7 @@ package ru.ulstu.user.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.mail.MailProperties; +import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.scheduling.annotation.Async; @@ -13,6 +14,7 @@ import ru.ulstu.configuration.ApplicationProperties; import ru.ulstu.configuration.Constants; import ru.ulstu.user.model.User; +import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.nio.charset.StandardCharsets; import java.util.Map; @@ -38,24 +40,16 @@ public class MailService { } @Async - public void sendEmail(String to, String subject, String content) { + public void sendEmail(String to, String subject, String content) throws MessagingException { log.debug("Send email to '{}' with subject '{}'", to, subject); MimeMessage mimeMessage = javaMailSender.createMimeMessage(); - try { - MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, StandardCharsets.UTF_8.name()); - message.setTo(to); - message.setFrom(mailProperties.getUsername()); - message.setSubject(subject); - message.setText(content, true); - javaMailSender.send(mimeMessage); - log.debug("Sent email to User '{}'", to); - } catch (Exception e) { - if (log.isDebugEnabled()) { - log.warn("Email could not be sent to user '{}'", to, e); - } else { - log.warn("Email could not be sent to user '{}': {}", to, e.getMessage()); - } - } + MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, StandardCharsets.UTF_8.name()); + message.setTo(to); + message.setFrom(mailProperties.getUsername()); + message.setSubject(subject); + message.setText(content, true); + javaMailSender.send(mimeMessage); + log.debug("Sent email to User '{}'", to); } @Async @@ -64,7 +58,17 @@ public class MailService { context.setVariable(USER, user); context.setVariable(BASE_URL, applicationProperties.getBaseUrl()); String content = templateEngine.process(templateName, context); - sendEmail(user.getEmail(), subject, content); + try { + sendEmail(user.getEmail(), subject, content); + } catch ( + Exception e) { + if (log.isDebugEnabled()) { + log.warn("Email could not be sent to user '{}'", user.getEmail(), e); + } else { + log.warn("Email could not be sent to user '{}': {}", user.getEmail(), e.getMessage()); + } + } + } //Todo: выделить сервис нотификаций @@ -75,11 +79,21 @@ public class MailService { context.setVariable(USER, user); context.setVariable(BASE_URL, applicationProperties.getBaseUrl()); String content = templateEngine.process(templateName, context); - sendEmail(user.getEmail(), subject, content); + try { + sendEmail(user.getEmail(), subject, content); + } catch ( + Exception e) { + if (log.isDebugEnabled()) { + log.warn("Email could not be sent to user '{}'", user.getEmail(), e); + } else { + log.warn("Email could not be sent to user '{}': {}", user.getEmail(), e.getMessage()); + } + } } @Async - public void sendEmailFromTemplate(Map variables, String templateName, String subject, String email) { + public void sendEmailFromTemplate(Map variables, String templateName, String subject, String email) + throws MessagingException { Context context = new Context(); variables.entrySet().forEach(entry -> context.setVariable(entry.getKey(), entry.getValue())); context.setVariable(BASE_URL, applicationProperties.getBaseUrl()); @@ -97,8 +111,7 @@ public class MailService { sendEmailFromTemplate(user, "passwordResetEmail", Constants.MAIL_RESET); } - @Async - public void sendInviteMail(Map variables, String email) { + public void sendInviteMail(Map variables, String email) throws MessagingException { sendEmailFromTemplate(variables, "userInviteEmail", Constants.MAIL_INVITE, email); } } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index ef11796..3a06684 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -18,15 +18,7 @@ import ru.ulstu.core.error.EntityIdIsNullException; import ru.ulstu.core.jpa.OffsetablePageRequest; import ru.ulstu.core.model.BaseEntity; import ru.ulstu.core.model.response.PageableItems; -import ru.ulstu.user.error.UserActivationError; -import ru.ulstu.user.error.UserEmailExistsException; -import ru.ulstu.user.error.UserIdExistsException; -import ru.ulstu.user.error.UserIsUndeadException; -import ru.ulstu.user.error.UserLoginExistsException; -import ru.ulstu.user.error.UserNotActivatedException; -import ru.ulstu.user.error.UserNotFoundException; -import ru.ulstu.user.error.UserPasswordsNotValidOrNotMatchException; -import ru.ulstu.user.error.UserResetKeyError; +import ru.ulstu.user.error.*; import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserListDto; @@ -38,6 +30,7 @@ import ru.ulstu.user.repository.UserRepository; import ru.ulstu.user.repository.UserRoleRepository; import ru.ulstu.user.util.UserUtils; +import javax.mail.MessagingException; import java.util.*; import java.util.stream.Collectors; @@ -314,7 +307,7 @@ public class UserService implements UserDetailsService { return userRepository.filterByAgeAndDegree(hasDegree, hasAge); } - public void inviteUser(String email) { + public void inviteUser(String email) throws UserSendingMailException { if (userRepository.findOneByEmailIgnoreCase(email) != null) { throw new UserEmailExistsException(email); } @@ -331,6 +324,10 @@ public class UserService implements UserDetailsService { userRepository.save(user); Map variables = ImmutableMap.of("password", password, "email", email); - mailService.sendInviteMail(variables, email); + try { + mailService.sendInviteMail(variables, email); + } catch (MessagingException e) { + throw new UserSendingMailException(email); + } } }