-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
.../application-server/src/main/java/de/tum/in/www1/hephaestus/notification/MailBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package de.tum.in.www1.hephaestus.notification; | ||
|
||
import de.tum.in.www1.hephaestus.gitprovider.user.User; | ||
import de.tum.in.www1.hephaestus.gitprovider.user.UserInfoDTO; | ||
import jakarta.activation.DataHandler; | ||
import jakarta.activation.FileDataSource; | ||
import jakarta.mail.*; | ||
import jakarta.mail.internet.InternetAddress; | ||
import jakarta.mail.internet.MimeBodyPart; | ||
import jakarta.mail.internet.MimeMessage; | ||
import jakarta.mail.internet.MimeMultipart; | ||
import jakarta.mail.util.ByteArrayDataSource; | ||
import lombok.Getter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.thymeleaf.context.Context; | ||
|
||
import java.util.*; | ||
|
||
public class MailBuilder { | ||
private static final Logger log = LoggerFactory.getLogger(MailBuilder.class); | ||
private final MailConfig config; | ||
|
||
private final List<User> primarySenders; | ||
private final List<User> primaryRecipients; | ||
|
||
@Getter | ||
private final String subject; | ||
|
||
@Getter | ||
private final String template; | ||
|
||
@Getter | ||
private final Map<String, Object> variables; | ||
|
||
@Getter | ||
private final Set<String> notificationNames; | ||
|
||
public MailBuilder(MailConfig config, String subject, String template) { | ||
this.config = config; | ||
|
||
this.primarySenders = new ArrayList<>(); | ||
this.primaryRecipients = new ArrayList<>(); | ||
|
||
this.subject = subject; | ||
this.template = template; | ||
|
||
this.variables = new HashMap<>(); | ||
this.variables.put("config", config.getConfigDto()); | ||
|
||
this.notificationNames = new HashSet<>(); | ||
} | ||
|
||
public MailBuilder addNotificationName(String name) { | ||
notificationNames.add(name); | ||
|
||
return this; | ||
} | ||
|
||
public MailBuilder addPrimarySender(User user) { | ||
this.primarySenders.add(user); | ||
|
||
return this; | ||
} | ||
|
||
public MailBuilder addPrimaryRecipient(User user) { | ||
if (primaryRecipients.contains(user)) { | ||
return this; | ||
} | ||
|
||
primaryRecipients.add(user); | ||
|
||
return this; | ||
} | ||
|
||
|
||
public void send(JavaMailSender mailSender) { | ||
List<User> toRecipients = new ArrayList<>(); | ||
|
||
for (User recipient : primaryRecipients) { | ||
if (!recipient.isNotificationsEnabled()) { | ||
continue; | ||
} | ||
toRecipients.add(recipient); | ||
} | ||
|
||
for (User recipient : toRecipients) { | ||
try { | ||
MimeMessage message = mailSender.createMimeMessage(); | ||
|
||
message.setFrom("ThesisManagement <" + config.getSender().getAddress() + ">"); | ||
message.setSender(config.getSender()); | ||
|
||
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient.getEmail())); | ||
|
||
Context templateContext = new Context(); | ||
templateContext.setVariables(this.variables); | ||
templateContext.setVariable("recipient", UserInfoDTO.fromUser(recipient)); | ||
|
||
message.setSubject(subject); | ||
|
||
Multipart messageContent = new MimeMultipart(); | ||
|
||
BodyPart messageBody = new MimeBodyPart(); | ||
messageBody.setContent(config.getTemplateEngine().process(template, templateContext), "text/html; charset=utf-8"); | ||
messageContent.addBodyPart(messageBody); | ||
|
||
message.setContent(messageContent); | ||
|
||
if (config.isEnabled()) { | ||
mailSender.send(message); | ||
} else { | ||
log.info("Sending Mail (postfix disabled)\n{}", messageBody.getContent()); | ||
} | ||
} catch (Exception exception) { | ||
log.warn("Failed to send email", exception); | ||
} | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...r/application-server/src/main/java/de/tum/in/www1/hephaestus/notification/MailConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package de.tum.in.www1.hephaestus.notification; | ||
|
||
import jakarta.mail.internet.AddressException; | ||
import jakarta.mail.internet.InternetAddress; | ||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.thymeleaf.TemplateEngine; | ||
|
||
import java.util.*; | ||
|
||
@Component | ||
public class MailConfig { | ||
|
||
private final Boolean enabled; | ||
|
||
@Getter | ||
private final String clientHost; | ||
|
||
@Getter | ||
private final InternetAddress sender; | ||
|
||
@Getter | ||
private final String signature; | ||
|
||
@Getter | ||
private final TemplateEngine templateEngine; | ||
|
||
@Autowired | ||
public MailConfig( | ||
@Value("${hephaestus.mail.sender}") InternetAddress sender, | ||
@Value("${hephaestus.mail.enabled}") boolean enabled, | ||
@Value("${hephaestus.mail.signature}") String mailSignature, | ||
@Value("${hephaestus.client.host}") String clientHost, | ||
TemplateEngine templateEngine | ||
) { | ||
this.enabled = enabled; | ||
this.sender = sender; | ||
this.signature = mailSignature; | ||
this.clientHost = clientHost; | ||
this.templateEngine = templateEngine; | ||
} | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public record MailConfigDto( | ||
String signature, | ||
String clientHost | ||
) {} | ||
|
||
public MailConfigDto getConfigDto() { | ||
return new MailConfigDto( | ||
Objects.requireNonNullElse(signature, ""), | ||
Objects.requireNonNullElse(getClientHost(), "") | ||
); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
.../application-server/src/main/java/de/tum/in/www1/hephaestus/notification/MailService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package de.tum.in.www1.hephaestus.notification; | ||
|
||
import de.tum.in.www1.hephaestus.gitprovider.user.User; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class MailService { | ||
|
||
private final JavaMailSender javaMailSender; | ||
|
||
private final MailConfig mailConfig; | ||
|
||
@Autowired | ||
public MailService(JavaMailSender javaMailSender, MailConfig mailConfig) { | ||
this.javaMailSender = javaMailSender; | ||
this.mailConfig = mailConfig; | ||
} | ||
|
||
public void sendBadPracticesDetectedEmail(User user, List<String> badPractices) { | ||
MailBuilder mailBuilder = new MailBuilder(mailConfig, "Bad Practices Detected", "bad-practices-detected"); | ||
mailBuilder | ||
.addPrimaryRecipient(user) | ||
.send(javaMailSender); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,23 @@ spring: | |
jwt: | ||
issuer-uri: http://localhost:8081/realms/hephaestus | ||
|
||
thymeleaf: | ||
prefix: ${MAIL_TEMPLATE_FOLDER:classpath:/mail-templates/} | ||
suffix: .html | ||
|
||
mail: | ||
host: ${POSTFIX_HOST:localhost} | ||
port: ${POSTFIX_PORT:25} | ||
username: ${POSTFIX_USERNAME:} | ||
password: ${POSTFIX_PASSWORD:} | ||
properties: | ||
mail: | ||
transport: | ||
protocol: smtp | ||
smtp: | ||
starttls: | ||
enable: true | ||
|
||
springdoc: | ||
default-produces-media-type: application/json | ||
|
||
|
@@ -51,6 +68,10 @@ hephaestus: | |
intelligence-service: | ||
url: http://localhost:8000 | ||
|
||
mail: | ||
enabled: ${MAIL_ENABLED:true} | ||
sender: ${MAIL_SENDER:[email protected]} | ||
signature: ${MAIL_SIGNATURE:} | ||
|
||
keycloak: | ||
url: http://localhost:8081 | ||
|
3 changes: 3 additions & 0 deletions
3
server/application-server/src/main/resources/mail-templates/bad-practices-detected.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<p th:inline="text"> | ||
Bad practice detected. | ||
</p> |