Skip to content

Commit

Permalink
Merge pull request #220 from AreaFiftyLAN/fix-mail-cleaup
Browse files Browse the repository at this point in the history
Mail functionality cleanup
  • Loading branch information
Martijn Janssen committed May 12, 2016
2 parents d50f610 + 794bb6f commit 047b416
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
import ch.wisv.areafiftylan.service.TeamService;
import ch.wisv.areafiftylan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import static ch.wisv.areafiftylan.util.ResponseEntityBuilder.createResponseEntity;

@RestController
@PreAuthorize("hasRole('ADMIN')")
@RequestMapping("/mail")
public class MailRestController {

Expand All @@ -26,43 +27,31 @@ public MailRestController(MailService mailService, UserService userService, Team
this.teamService = teamService;
}

MailService mailService;
UserService userService;
TeamService teamService;
private MailService mailService;
private UserService userService;
private TeamService teamService;

@RequestMapping(value = "/user/{userId}", method = RequestMethod.POST)
ResponseEntity<?> sendMailToUser(@PathVariable Long userId, @Validated @RequestBody MailDTO mailDTO) {
User user = userService.getUserById(userId);

try {
mailService.sendTemplateMailToUser(user, mailDTO);
} catch (MessagingException e) {
e.printStackTrace();
}
mailService.sendTemplateMailToUser(user, mailDTO);

return new ResponseEntity<>(null, new HttpHeaders(), HttpStatus.OK);
return createResponseEntity(HttpStatus.OK, "Mail successfully sent");

}

@RequestMapping(value = "/team/{teamId}", method = RequestMethod.POST)
ResponseEntity<?> sendMailToTeam(@PathVariable Long teamId, @Validated @RequestBody MailDTO mailDTO) {
Team team = teamService.getTeamById(teamId);
try {
mailService.sendTemplateMailToTeam(team, mailDTO);
} catch (MessagingException e) {
e.printStackTrace();
}
mailService.sendTemplateMailToTeam(team, mailDTO);

return new ResponseEntity<>(null, new HttpHeaders(), HttpStatus.OK);
return createResponseEntity(HttpStatus.OK, "Mail successfully sent");
}

@RequestMapping(value = "/users/all/YESREALLY", method = RequestMethod.POST)
ResponseEntity<?> sendMailToAll(@Validated @RequestBody MailDTO mailDTO) {
try {
mailService.sendTemplateMailToAll(userService.getAllUsers(), mailDTO);
} catch (MessagingException e) {
e.printStackTrace();
}
return new ResponseEntity<>(null, new HttpHeaders(), HttpStatus.OK);
mailService.sendTemplateMailToAll(userService.getAllUsers(), mailDTO);
return createResponseEntity(HttpStatus.OK, "Mail successfully sent");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ protected void configure(HttpSecurity http) throws Exception {
.failureHandler(authenticationFailureHandler)
.and()
.logout()
.logoutUrl("/logout")
.permitAll()
.and().authorizeRequests()
.antMatchers("/mail").hasAuthority("ADMIN")
.anyRequest().permitAll();
.logoutUrl("/logout");
//@formatter:on

// This is used for the Mollie webhook, so it shouldn't be protected by CSRF
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/ch/wisv/areafiftylan/service/MailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@
import ch.wisv.areafiftylan.model.Team;
import ch.wisv.areafiftylan.model.User;

import javax.mail.MessagingException;
import java.util.Collection;

public interface MailService {

void sendMail(String recipientEmail, String recipientName, String senderEmail, String subject, String message)
throws MessagingException;
void sendMail(String recipientEmail, String recipientName, String subject, String message);

void sendTemplateMailToTeam(Team team, MailDTO mailDTO) throws MessagingException;
void sendTemplateMailToTeam(Team team, MailDTO mailDTO);

void sendTemplateMailToAll(Collection<User> users, MailDTO mailDTO) throws MessagingException;
void sendTemplateMailToAll(Collection<User> users, MailDTO mailDTO);

void sendTemplateMailToUser(User user, MailDTO mailDTO) throws MessagingException;
void sendTemplateMailToUser(User user, MailDTO mailDTO);

void sendVerificationmail(User user, String url) throws MessagingException;
void sendVerificationmail(User user, String url);

void sendPasswordResetMail(User user, String url) throws MessagingException;
void sendPasswordResetMail(User user, String url);

void sendTicketTransferMail(User sender, User receiver, String url) throws MessagingException;
void sendTicketTransferMail(User sender, User receiver, String url);

void sendTeamInviteMail(User user, String teamName, User teamCaptain) throws MessagingException;
void sendTeamInviteMail(User user, String teamName, User teamCaptain);
}
73 changes: 43 additions & 30 deletions src/main/java/ch/wisv/areafiftylan/service/MailServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import ch.wisv.areafiftylan.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.MailPreparationException;
import org.springframework.mail.MailSendException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -33,23 +36,30 @@ public MailServiceImpl(JavaMailSender mailSender, SpringTemplateEngine templateE
}

@Override
public void sendMail(String recipientEmail, String recipientName, String senderEmail, String subject,
String messageString) throws MessagingException {
public void sendMail(String recipientEmail, String recipientName, String subject, String messageString) {

// Prepare message using a Spring helper
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart

message.setSubject("[Area FiftyLAN] " + subject);
message.setFrom(sender);
message.setTo(recipientEmail);

// Create the HTML body using Thymeleaf
String htmlContent = prepareHtmlContent(recipientName, messageString);
message.setText(htmlContent, true); // true = isHtml
final MimeMessageHelper message;

try {
message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setSubject("[Area FiftyLAN] " + subject);
message.setFrom(sender);
message.setTo(recipientEmail);

// Create the HTML body using Thymeleaf
String htmlContent = prepareHtmlContent(recipientName, messageString);
message.setText(htmlContent, true); // true = isHtml

// Send mail
this.mailSender.send(mimeMessage);
} catch (MessagingException e) {
throw new MailPreparationException("Unable to prepare email", e.getCause());
} catch (MailException m) {
throw new MailSendException("Unable to send email", m.getCause());
}

// Send mail
this.mailSender.send(mimeMessage);
}

private String prepareHtmlContent(String name, String message) {
Expand All @@ -62,39 +72,40 @@ private String prepareHtmlContent(String name, String message) {
}

@Override
public void sendTemplateMailToTeam(Team team, MailDTO mailDTO) throws MessagingException {
public void sendTemplateMailToTeam(Team team, MailDTO mailDTO) {
for (User user : team.getMembers()) {
sendTemplateMailToUser(user, mailDTO);
}
}

@Override
public void sendTemplateMailToAll(Collection<User> users, MailDTO mailDTO) throws MessagingException {
public void sendTemplateMailToAll(Collection<User> users, MailDTO mailDTO) {
for (User user : users) {
sendTemplateMailToUser(user, mailDTO);
}
}

@Override
public void sendTemplateMailToUser(User user, MailDTO mailDTO) throws MessagingException {
sendMail(user.getEmail(), user.getProfile().getFirstName(), "[email protected]", mailDTO.getSubject(),
mailDTO.getMessage());
public void sendTemplateMailToUser(User user, MailDTO mailDTO) {
sendMail(user.getEmail(), user.getProfile().getFirstName(), mailDTO.getSubject(), mailDTO.getMessage());
}

@Override
public void sendVerificationmail(User user, String url) throws MessagingException {
String message = "Please click on the following link to complete your registration: " + url;
sendMail(user.getEmail(), user.getUsername(), null, "Confirm your registration", message);
public void sendVerificationmail(User user, String url) {
String message = "Please click on the following link to complete your registration: <a href=\"" +
url + "\">" + url + "</a>";
sendMail(user.getEmail(), user.getUsername(), "Confirm your registration", message);
}

@Override
public void sendPasswordResetMail(User user, String url) throws MessagingException {
String message = "Please click on the following link to reset your password: " + url;
sendMail(user.getEmail(), user.getUsername(), null, "Password reset requested", message);
public void sendPasswordResetMail(User user, String url) {
String message = "Please click on the following link to reset your password: <a href=\"" +
url + "\">" + url + "</a>";
sendMail(user.getEmail(), user.getUsername(), "Password reset requested", message);
}

@Override
public void sendTeamInviteMail(User user, String teamName, User teamCaptain) throws MessagingException {
public void sendTeamInviteMail(User user, String teamName, User teamCaptain) {

String message = "You've been invited to join \"Team " + teamName +
"\" by " +
Expand All @@ -103,13 +114,15 @@ public void sendTeamInviteMail(User user, String teamName, User teamCaptain) thr
teamCaptain.getProfile().getLastName() +
"! Please log in to My Area to accept the invitation.";

sendMail(user.getEmail(), user.getUsername(), null, "You've been invited to \"Team " + teamName + "\"",
message);
sendMail(user.getEmail(), user.getUsername(), "You've been invited to \"Team " + teamName + "\"", message);
}

@Override
public void sendTicketTransferMail(User sender, User receiver, String url) throws MessagingException {
String message = sender.getProfile().firstName + " has sent you a ticket for AreaFiftyLAN! To accept this ticket please click on the following link: " + url;
sendMail(receiver.getEmail(), receiver.getProfile().getFirstName() + " " + receiver.getProfile().getLastName(), null, "A ticket for AreaFiftyLAN has been sent to you!", message);
public void sendTicketTransferMail(User sender, User receiver, String url) {
String message = sender.getProfile().firstName +
" has sent you a ticket for AreaFiftyLAN! To accept this ticket please click on the following link: " +
"<a href=\"" + url + "\">" + url + "</a>";
sendMail(receiver.getEmail(), receiver.getProfile().getFirstName() + " " + receiver.getProfile().getLastName(),
"A ticket for AreaFiftyLAN has been sent to you!", message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -118,12 +117,7 @@ public TeamInviteToken inviteMember(Long teamId, String username) {
TeamInviteToken inviteToken = new TeamInviteToken(user, team);
teamInviteTokenRepository.save(inviteToken);

try {
mailService.sendTeamInviteMail(user, team.getTeamName(), team.getCaptain());
} catch (MessagingException e) {
// TODO: Fix mailservice exception handling
e.printStackTrace();
}
mailService.sendTeamInviteMail(user, team.getTeamName(), team.getCaptain());
return inviteToken;
} else {
throw new IllegalArgumentException("User already invited");
Expand Down
Loading

0 comments on commit 047b416

Please sign in to comment.