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 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<UserListDto, UserDto> {
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<UserListDto, UserDto> {
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:/";
}

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.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<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();
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<String, Object> variables, String email) {
public void sendInviteMail(Map<String, Object> variables, String email) throws MessagingException {
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.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<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);
}
}
}