87 transactional inviting

This commit is contained in:
Artem.Arefev 2019-05-15 03:08:21 +04:00
parent 11ef644e5e
commit d88f67ce00
4 changed files with 54 additions and 35 deletions

View File

@ -10,10 +10,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.configuration.Constants; import ru.ulstu.configuration.Constants;
import ru.ulstu.odin.controller.OdinController; import ru.ulstu.odin.controller.OdinController;
import ru.ulstu.user.error.UserSendingMailException;
import ru.ulstu.user.model.*; import ru.ulstu.user.model.*;
import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserService;
import ru.ulstu.user.service.UserSessionService; import ru.ulstu.user.service.UserSessionService;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
@ -36,7 +38,7 @@ public class UserControllerV2 extends OdinController<UserListDto, UserDto> {
private final UserSessionService userSessionService; private final UserSessionService userSessionService;
public UserControllerV2(UserService userService, public UserControllerV2(UserService userService,
UserSessionService userSessionService) { UserSessionService userSessionService) {
super(UserListDto.class, UserDto.class); super(UserListDto.class, UserDto.class);
this.userService = userService; this.userService = userService;
this.userSessionService = userSessionService; this.userSessionService = userSessionService;
@ -57,8 +59,8 @@ public class UserControllerV2 extends OdinController<UserListDto, UserDto> {
modelMap.addAttribute("userDto", userService.updateUserInformation(user, userDto)); modelMap.addAttribute("userDto", userService.updateUserInformation(user, userDto));
} }
@PostMapping("/invite" ) @PostMapping("/invite")
public String inviteUser(@RequestParam(value = "email") String email){ public String inviteUser(@RequestParam(value = "email") String email) throws UserSendingMailException {
userService.inviteUser(email); userService.inviteUser(email);
return "redirect:/"; return "redirect:/";
} }

View File

@ -0,0 +1,7 @@
package ru.ulstu.user.error;
public class UserSendingMailException extends RuntimeException {
public UserSendingMailException(String message) {
super(message);
}
}

View File

@ -3,6 +3,7 @@ package ru.ulstu.user.service;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.mail.MailProperties; import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
@ -13,6 +14,7 @@ import ru.ulstu.configuration.ApplicationProperties;
import ru.ulstu.configuration.Constants; import ru.ulstu.configuration.Constants;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
@ -38,24 +40,16 @@ public class MailService {
} }
@Async @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); log.debug("Send email to '{}' with subject '{}'", to, subject);
MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try { MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, StandardCharsets.UTF_8.name());
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, StandardCharsets.UTF_8.name()); message.setTo(to);
message.setTo(to); message.setFrom(mailProperties.getUsername());
message.setFrom(mailProperties.getUsername()); message.setSubject(subject);
message.setSubject(subject); message.setText(content, true);
message.setText(content, true); javaMailSender.send(mimeMessage);
javaMailSender.send(mimeMessage); log.debug("Sent email to User '{}'", to);
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());
}
}
} }
@Async @Async
@ -64,7 +58,17 @@ public class MailService {
context.setVariable(USER, user); context.setVariable(USER, user);
context.setVariable(BASE_URL, applicationProperties.getBaseUrl()); context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
String content = templateEngine.process(templateName, context); 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: выделить сервис нотификаций //Todo: выделить сервис нотификаций
@ -75,11 +79,21 @@ public class MailService {
context.setVariable(USER, user); context.setVariable(USER, user);
context.setVariable(BASE_URL, applicationProperties.getBaseUrl()); context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
String content = templateEngine.process(templateName, context); 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 @Async
public void sendEmailFromTemplate(Map<String, Object> variables, String templateName, String subject, String email) { public void sendEmailFromTemplate(Map<String, Object> variables, String templateName, String subject, String email)
throws MessagingException {
Context context = new Context(); Context context = new Context();
variables.entrySet().forEach(entry -> context.setVariable(entry.getKey(), entry.getValue())); variables.entrySet().forEach(entry -> context.setVariable(entry.getKey(), entry.getValue()));
context.setVariable(BASE_URL, applicationProperties.getBaseUrl()); context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
@ -97,8 +111,7 @@ public class MailService {
sendEmailFromTemplate(user, "passwordResetEmail", Constants.MAIL_RESET); sendEmailFromTemplate(user, "passwordResetEmail", Constants.MAIL_RESET);
} }
@Async public void sendInviteMail(Map<String, Object> variables, String email) throws MessagingException {
public void sendInviteMail(Map<String, Object> variables, String email) {
sendEmailFromTemplate(variables, "userInviteEmail", Constants.MAIL_INVITE, email); sendEmailFromTemplate(variables, "userInviteEmail", Constants.MAIL_INVITE, email);
} }
} }

View File

@ -18,15 +18,7 @@ import ru.ulstu.core.error.EntityIdIsNullException;
import ru.ulstu.core.jpa.OffsetablePageRequest; import ru.ulstu.core.jpa.OffsetablePageRequest;
import ru.ulstu.core.model.BaseEntity; import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.core.model.response.PageableItems; import ru.ulstu.core.model.response.PageableItems;
import ru.ulstu.user.error.UserActivationError; import ru.ulstu.user.error.*;
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.model.User; import ru.ulstu.user.model.User;
import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserDto;
import ru.ulstu.user.model.UserListDto; 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.repository.UserRoleRepository;
import ru.ulstu.user.util.UserUtils; import ru.ulstu.user.util.UserUtils;
import javax.mail.MessagingException;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -314,7 +307,7 @@ public class UserService implements UserDetailsService {
return userRepository.filterByAgeAndDegree(hasDegree, hasAge); return userRepository.filterByAgeAndDegree(hasDegree, hasAge);
} }
public void inviteUser(String email) { public void inviteUser(String email) throws UserSendingMailException {
if (userRepository.findOneByEmailIgnoreCase(email) != null) { if (userRepository.findOneByEmailIgnoreCase(email) != null) {
throw new UserEmailExistsException(email); throw new UserEmailExistsException(email);
} }
@ -331,6 +324,10 @@ public class UserService implements UserDetailsService {
userRepository.save(user); userRepository.save(user);
Map<String, Object> variables = ImmutableMap.of("password", password, "email", email); Map<String, Object> variables = ImmutableMap.of("password", password, "email", email);
mailService.sendInviteMail(variables, email); try {
mailService.sendInviteMail(variables, email);
} catch (MessagingException e) {
throw new UserSendingMailException(email);
}
} }
} }