diff --git a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/pom.xml b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/pom.xml index 085d3e4904..aa87944e6c 100644 --- a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/pom.xml +++ b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/pom.xml @@ -16,6 +16,10 @@ de.muenchen.oss.digiwf digiwf-s3-integration-client-starter + + de.muenchen.oss.digiwf + digiwf-email-starter + org.apache.commons commons-lang3 diff --git a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/adapter/out/MailAdapter.java b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/adapter/out/MailAdapter.java new file mode 100644 index 0000000000..a5b452530b --- /dev/null +++ b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/adapter/out/MailAdapter.java @@ -0,0 +1,20 @@ +package de.muenchen.oss.digiwf.email.integration.adapter.out; + +import de.muenchen.oss.digiwf.email.api.DigiwfEmailApi; +import de.muenchen.oss.digiwf.email.integration.application.port.out.MailPort; +import de.muenchen.oss.digiwf.email.model.FileAttachment; +import jakarta.mail.MessagingException; +import lombok.RequiredArgsConstructor; + +import java.util.List; + +@RequiredArgsConstructor +public class MailAdapter implements MailPort { + + private final DigiwfEmailApi digiwfEmailApi; + + @Override + public void sendMail(String receivers, String subject, String body, String replyTo, String receiversCc, String receiversBcc, List attachments) throws MessagingException { + this.digiwfEmailApi.sendMailWithAttachments(receivers, subject, body, replyTo, receiversCc, receiversBcc, attachments); + } +} diff --git a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/adapter/out/S3Adapter.java b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/adapter/out/S3Adapter.java index 0ac91a4578..528b28ec6a 100644 --- a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/adapter/out/S3Adapter.java +++ b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/adapter/out/S3Adapter.java @@ -1,20 +1,19 @@ package de.muenchen.oss.digiwf.email.integration.adapter.out; import de.muenchen.oss.digiwf.email.integration.application.port.out.LoadMailAttachmentPort; -import de.muenchen.oss.digiwf.email.integration.model.FileAttachment; import de.muenchen.oss.digiwf.email.integration.model.PresignedUrl; +import de.muenchen.oss.digiwf.email.model.FileAttachment; import de.muenchen.oss.digiwf.message.process.api.error.BpmnError; import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageClientErrorException; import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageException; import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageServerErrorException; import de.muenchen.oss.digiwf.s3.integration.client.repository.transfer.S3FileTransferRepository; +import jakarta.mail.util.ByteArrayDataSource; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.tika.Tika; -import jakarta.mail.util.ByteArrayDataSource; - @Slf4j @RequiredArgsConstructor public class S3Adapter implements LoadMailAttachmentPort { diff --git a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/application/port/out/LoadMailAttachmentPort.java b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/application/port/out/LoadMailAttachmentPort.java index e97a349d58..af2bbf6694 100644 --- a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/application/port/out/LoadMailAttachmentPort.java +++ b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/application/port/out/LoadMailAttachmentPort.java @@ -1,7 +1,7 @@ package de.muenchen.oss.digiwf.email.integration.application.port.out; -import de.muenchen.oss.digiwf.email.integration.model.FileAttachment; import de.muenchen.oss.digiwf.email.integration.model.PresignedUrl; +import de.muenchen.oss.digiwf.email.model.FileAttachment; public interface LoadMailAttachmentPort { diff --git a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/application/port/out/MailPort.java b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/application/port/out/MailPort.java new file mode 100644 index 0000000000..98e5a0e7f4 --- /dev/null +++ b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/application/port/out/MailPort.java @@ -0,0 +1,12 @@ +package de.muenchen.oss.digiwf.email.integration.application.port.out; + +import de.muenchen.oss.digiwf.email.model.FileAttachment; +import jakarta.mail.MessagingException; + +import java.util.List; + +public interface MailPort { + + void sendMail(String receivers, String subject, String body, String replyTo, String receiversCc, String receiversBcc, List attachments) throws MessagingException; + +} diff --git a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/application/usecase/SendMailUseCase.java b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/application/usecase/SendMailUseCase.java index c6f8397133..0864b989da 100644 --- a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/application/usecase/SendMailUseCase.java +++ b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/application/usecase/SendMailUseCase.java @@ -1,27 +1,23 @@ package de.muenchen.oss.digiwf.email.integration.application.usecase; -import de.muenchen.oss.digiwf.email.integration.model.FileAttachment; import de.muenchen.oss.digiwf.email.integration.application.port.in.SendMail; import de.muenchen.oss.digiwf.email.integration.application.port.out.CorrelateMessagePort; import de.muenchen.oss.digiwf.email.integration.application.port.out.LoadMailAttachmentPort; +import de.muenchen.oss.digiwf.email.integration.application.port.out.MailPort; import de.muenchen.oss.digiwf.email.integration.model.Mail; +import de.muenchen.oss.digiwf.email.model.FileAttachment; import de.muenchen.oss.digiwf.message.process.api.error.BpmnError; +import jakarta.mail.MessagingException; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import lombok.val; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.springframework.mail.MailException; -import org.springframework.mail.javamail.JavaMailSender; -import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.validation.annotation.Validated; -import jakarta.mail.Message; -import jakarta.mail.MessagingException; -import jakarta.mail.internet.InternetAddress; -import jakarta.mail.internet.MimeMessage; -import jakarta.validation.Valid; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; @Slf4j @@ -29,10 +25,9 @@ @Validated public class SendMailUseCase implements SendMail { - private final JavaMailSender mailSender; private final LoadMailAttachmentPort loadAttachmentPort; private final CorrelateMessagePort correlateMessagePort; - private final String fromAddress; + private final MailPort mailPort; /** * Send a mail. @@ -41,50 +36,23 @@ public class SendMailUseCase implements SendMail { */ @Override public void sendMail(final String processInstanceIde, final String messageName, @Valid final Mail mail) throws BpmnError { - final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); - try { - mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail.getReceivers())); - - if (StringUtils.isNotEmpty(mail.getReceiversCc())) { - mimeMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(mail.getReceiversCc())); - } - if (StringUtils.isNotEmpty(mail.getReceiversBcc())) { - mimeMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(mail.getReceiversBcc())); - } - - final var helper = new MimeMessageHelper(mimeMessage, true); - - helper.setSubject(mail.getSubject()); - helper.setText(mail.getBody()); - helper.setFrom(this.fromAddress); - - if (StringUtils.isNotBlank(mail.getReplyTo())) { - helper.setReplyTo(mail.getReplyTo()); - } - - // mail attachments + // load Attachments + final List attachments = new ArrayList<>(); if (CollectionUtils.isNotEmpty(mail.getAttachments())) { for (val attachment : mail.getAttachments()) { - final FileAttachment mailAttachment = this.loadAttachmentPort.loadAttachment(attachment); - helper.addAttachment(mailAttachment.getFileName(), mailAttachment.getFile()); + attachments.add(this.loadAttachmentPort.loadAttachment(attachment)); } } + // send mail + this.mailPort.sendMail(mail.getReceivers(), mail.getSubject(), mail.getBody(), mail.getReplyTo(), mail.getReceiversCc(), mail.getReceiversBcc(), attachments); + // correlate message + final Map correlatePayload = new HashMap<>(); + correlatePayload.put("mailSentStatus", true); + this.correlateMessagePort.correlateMessage(processInstanceIde, messageName, correlatePayload); } catch (final MessagingException ex) { - log.error(ex.getMessage()); - throw new BpmnError("MESSAGING_EXCEPTION", ex.getMessage()); - } - - try { - this.mailSender.send(mimeMessage); - log.info("Mail sent to {}.", mail.getReceivers()); - } catch (final MailException ex) { log.error("Sending mail failed with exception: {}", ex.getMessage()); throw new BpmnError("MAIL_SENDING_FAILED", ex.getMessage()); } - - final Map correlatePayload = new HashMap<>(); - correlatePayload.put("mailSentStatus", true); - this.correlateMessagePort.correlateMessage(processInstanceIde, messageName, correlatePayload); } } diff --git a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/model/FileAttachment.java b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/model/FileAttachment.java deleted file mode 100644 index 5c75b5fdc7..0000000000 --- a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/main/java/de/muenchen/oss/digiwf/email/integration/model/FileAttachment.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.muenchen.oss.digiwf.email.integration.model; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.RequiredArgsConstructor; - -import jakarta.mail.util.ByteArrayDataSource; - -@Data -@RequiredArgsConstructor -@AllArgsConstructor -public class FileAttachment { - - private String fileName; - - private ByteArrayDataSource file; - -} diff --git a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/test/java/de/muenchen/oss/digiwf/email/integration/adapter/out/MailAdapterTest.java b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/test/java/de/muenchen/oss/digiwf/email/integration/adapter/out/MailAdapterTest.java new file mode 100644 index 0000000000..f930cdf0a4 --- /dev/null +++ b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/test/java/de/muenchen/oss/digiwf/email/integration/adapter/out/MailAdapterTest.java @@ -0,0 +1,21 @@ +package de.muenchen.oss.digiwf.email.integration.adapter.out; + +import de.muenchen.oss.digiwf.email.api.DigiwfEmailApi; +import jakarta.mail.MessagingException; +import org.junit.jupiter.api.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +class MailAdapterTest { + + private final DigiwfEmailApi digiwfEmailApi = mock(DigiwfEmailApi.class); + + @Test + void sendMail() throws MessagingException { + final MailAdapter mailAdapter = new MailAdapter(digiwfEmailApi); + mailAdapter.sendMail("receivers", "subject", "body", "replyTo", "receiversCc", "receiversBcc", null); + verify(digiwfEmailApi).sendMailWithAttachments("receivers", "subject", "body", "replyTo", "receiversCc", "receiversBcc", null); + } + +} diff --git a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/test/java/de/muenchen/oss/digiwf/email/integration/adapter/out/S3AdapterTest.java b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/test/java/de/muenchen/oss/digiwf/email/integration/adapter/out/S3AdapterTest.java index 47896bf363..e701d09528 100644 --- a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/test/java/de/muenchen/oss/digiwf/email/integration/adapter/out/S3AdapterTest.java +++ b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/test/java/de/muenchen/oss/digiwf/email/integration/adapter/out/S3AdapterTest.java @@ -1,7 +1,7 @@ package de.muenchen.oss.digiwf.email.integration.adapter.out; -import de.muenchen.oss.digiwf.email.integration.model.FileAttachment; import de.muenchen.oss.digiwf.email.integration.model.PresignedUrl; +import de.muenchen.oss.digiwf.email.model.FileAttachment; import de.muenchen.oss.digiwf.message.process.api.error.BpmnError; import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageClientErrorException; import de.muenchen.oss.digiwf.s3.integration.client.exception.DocumentStorageException; diff --git a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/test/java/de/muenchen/oss/digiwf/email/integration/application/usecase/SendMailUseCaseTest.java b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/test/java/de/muenchen/oss/digiwf/email/integration/application/usecase/SendMailUseCaseTest.java index b3f9e0196b..ece98e4ab2 100644 --- a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/test/java/de/muenchen/oss/digiwf/email/integration/application/usecase/SendMailUseCaseTest.java +++ b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-core/src/test/java/de/muenchen/oss/digiwf/email/integration/application/usecase/SendMailUseCaseTest.java @@ -1,47 +1,31 @@ package de.muenchen.oss.digiwf.email.integration.application.usecase; -import de.muenchen.oss.digiwf.email.integration.model.FileAttachment; -import de.muenchen.oss.digiwf.email.integration.adapter.out.ProcessAdapter; +import de.muenchen.oss.digiwf.email.integration.application.port.in.SendMail; import de.muenchen.oss.digiwf.email.integration.application.port.out.CorrelateMessagePort; import de.muenchen.oss.digiwf.email.integration.application.port.out.LoadMailAttachmentPort; +import de.muenchen.oss.digiwf.email.integration.application.port.out.MailPort; import de.muenchen.oss.digiwf.email.integration.model.Mail; import de.muenchen.oss.digiwf.email.integration.model.PresignedUrl; -import de.muenchen.oss.digiwf.message.core.api.MessageApi; -import de.muenchen.oss.digiwf.message.process.api.ProcessApi; +import de.muenchen.oss.digiwf.email.model.FileAttachment; import de.muenchen.oss.digiwf.message.process.api.error.BpmnError; -import de.muenchen.oss.digiwf.message.process.impl.ProcessApiImpl; -import org.junit.jupiter.api.Assertions; +import jakarta.mail.MessagingException; +import jakarta.mail.util.ByteArrayDataSource; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; -import org.springframework.mail.MailException; -import org.springframework.mail.javamail.JavaMailSender; -import jakarta.mail.MessagingException; -import jakarta.mail.Session; -import jakarta.mail.internet.MimeMessage; -import jakarta.mail.util.ByteArrayDataSource; import java.util.List; +import java.util.Map; -import static org.mockito.ArgumentMatchers.any; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.*; class SendMailUseCaseTest { - private final MessageApi messageApi = Mockito.spy(Mockito.mock(MessageApi.class)); - - private final JavaMailSender javaMailSender = mock(JavaMailSender.class); private final LoadMailAttachmentPort loadMailAttachmentPort = mock(LoadMailAttachmentPort.class); + private final CorrelateMessagePort correlateMessagePort = mock(CorrelateMessagePort.class); + private final MailPort mailPort = mock(MailPort.class); - private final ProcessApi processApi = new ProcessApiImpl( - this.messageApi, - "correlateMessageDestination", - "startProcessDestination" - ); - - private final CorrelateMessagePort correlateMessagePort = new ProcessAdapter(processApi); - private final String fromAddress = "digiwf@muenchen.de"; + private SendMail sendMail; private final Mail mail = new Mail( "mailReceiver1@muenchen.de,mailReceiver2@muenchen.de", @@ -52,57 +36,51 @@ class SendMailUseCaseTest { "digiwf@muenchen.de", null ); + private final String processInstanceId = "processInstanceId"; + private final String messageName = "messageName"; @BeforeEach void setUp() { - String anhangInhalt = "Anhang Inhalt"; - byte[] anhangByt = anhangInhalt.getBytes(); - when(this.javaMailSender.createMimeMessage()).thenReturn(new MimeMessage((Session) null)); - when(this.loadMailAttachmentPort.loadAttachment(any())).thenReturn(new FileAttachment("Testanhang", new ByteArrayDataSource(anhangByt, "AttName"))); + this.sendMail = new SendMailUseCase(loadMailAttachmentPort, correlateMessagePort, mailPort); } @Test void sendMail() throws MessagingException { - - final SendMailUseCase sendMailUseCase = new SendMailUseCase(this.javaMailSender, this.loadMailAttachmentPort, this.correlateMessagePort, fromAddress); - - sendMailUseCase.sendMail("processInstanceIde", "messageName", this.mail); - - final ArgumentCaptor messageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class); - - verify(this.javaMailSender).send(messageArgumentCaptor.capture()); - Assertions.assertEquals(4, messageArgumentCaptor.getValue().getAllRecipients().length); - Assertions.assertEquals(1, messageArgumentCaptor.getValue().getReplyTo().length); - Assertions.assertEquals("Test Mail", messageArgumentCaptor.getValue().getSubject()); + sendMail.sendMail(processInstanceId, messageName, mail); + verify(mailPort).sendMail( + mail.getReceivers(), + mail.getSubject(), + mail.getBody(), + mail.getReplyTo(), + mail.getReceiversCc(), + mail.getReceiversBcc(), + List.of()); + verify(correlateMessagePort).correlateMessage(processInstanceId, messageName, Map.of("mailSentStatus", true)); } @Test void sendMailWithAttachments() throws MessagingException { - final Mail mailWithAttachments = this.mail; - mailWithAttachments.setAttachments(List.of(new PresignedUrl("http://localhost:9000/some-url", "test.txt", "GET"))); - final SendMailUseCase sendMailUseCase = new SendMailUseCase(this.javaMailSender, this.loadMailAttachmentPort, this.correlateMessagePort, this.fromAddress); - sendMailUseCase.sendMail("processInstanceIde", "messageName", mailWithAttachments); - - final ArgumentCaptor messageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class); - verify(this.javaMailSender).send(messageArgumentCaptor.capture()); - Assertions.assertEquals(4, messageArgumentCaptor.getValue().getAllRecipients().length); - Assertions.assertEquals(1, messageArgumentCaptor.getValue().getReplyTo().length); - Assertions.assertEquals("Test Mail", messageArgumentCaptor.getValue().getSubject()); - // attachment - verify(this.loadMailAttachmentPort, times(1)).loadAttachment(any()); - final FileAttachment result = this.loadMailAttachmentPort.loadAttachment(any()); - Assertions.assertEquals("Testanhang", result.getFileName()); + final PresignedUrl presignedUrl = new PresignedUrl("http://localhost:9000/some-url", "test.txt", "GET"); + mail.setAttachments(List.of(presignedUrl)); + + final FileAttachment fileAttachment = new FileAttachment("test.txt", new ByteArrayDataSource("Anhang Inhalt".getBytes(), "text/plain")); + when(loadMailAttachmentPort.loadAttachment(presignedUrl)).thenReturn(fileAttachment); + + sendMail.sendMail(processInstanceId, messageName, mail); + verify(mailPort).sendMail( + mail.getReceivers(), + mail.getSubject(), + mail.getBody(), + mail.getReplyTo(), + mail.getReceiversCc(), + mail.getReceiversBcc(), + List.of(fileAttachment)); + verify(correlateMessagePort).correlateMessage(processInstanceId, messageName, Map.of("mailSentStatus", true)); } @Test - void testThatABpmnErrorIsThrowIfSendMailFailsWithAMailException() { - doThrow(mock(MailException.class)).when(this.javaMailSender).send(any(MimeMessage.class)); - final SendMailUseCase sendMailUseCase = new SendMailUseCase(this.javaMailSender, this.loadMailAttachmentPort, this.correlateMessagePort, this.fromAddress); - - Assertions.assertThrows(BpmnError.class, () -> { - sendMailUseCase.sendMail("processInstanceIde", "messageName", this.mail); - }); + void sendMailThrowsBpmnError() throws MessagingException { + doThrow(new MessagingException("Test Exception")).when(mailPort).sendMail(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), any()); + assertThrows(BpmnError.class, () -> sendMail.sendMail(processInstanceId, messageName, mail)); } - - } diff --git a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-starter/src/main/java/de/muenchen/oss/digiwf/email/integration/configuration/CustomMailProperties.java b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-starter/src/main/java/de/muenchen/oss/digiwf/email/integration/configuration/CustomMailProperties.java deleted file mode 100644 index cbc9cab3cd..0000000000 --- a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-starter/src/main/java/de/muenchen/oss/digiwf/email/integration/configuration/CustomMailProperties.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.muenchen.oss.digiwf.email.integration.configuration; - -import lombok.Data; -import org.springframework.boot.context.properties.ConfigurationProperties; - -import jakarta.validation.constraints.NotBlank; - -@Data -@ConfigurationProperties(prefix = "io.muenchendigital.digiwf.mail") -public class CustomMailProperties { - - /** - * Sender mail address. - */ - @NotBlank - private String fromAddress; - -} diff --git a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-starter/src/main/java/de/muenchen/oss/digiwf/email/integration/configuration/MailAutoConfiguration.java b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-starter/src/main/java/de/muenchen/oss/digiwf/email/integration/configuration/MailAutoConfiguration.java index cce690e693..de5f261319 100644 --- a/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-starter/src/main/java/de/muenchen/oss/digiwf/email/integration/configuration/MailAutoConfiguration.java +++ b/digiwf-integrations/digiwf-email-integration/digiwf-email-integration-starter/src/main/java/de/muenchen/oss/digiwf/email/integration/configuration/MailAutoConfiguration.java @@ -1,75 +1,50 @@ package de.muenchen.oss.digiwf.email.integration.configuration; +import de.muenchen.oss.digiwf.email.api.DigiwfEmailApi; import de.muenchen.oss.digiwf.email.integration.adapter.in.MessageProcessor; -import de.muenchen.oss.digiwf.email.integration.application.usecase.SendMailUseCase; -import de.muenchen.oss.digiwf.email.integration.infrastructure.MonitoringService; -import io.micrometer.core.instrument.MeterRegistry; +import de.muenchen.oss.digiwf.email.integration.adapter.out.MailAdapter; import de.muenchen.oss.digiwf.email.integration.adapter.out.ProcessAdapter; import de.muenchen.oss.digiwf.email.integration.adapter.out.S3Adapter; import de.muenchen.oss.digiwf.email.integration.application.port.in.SendMail; import de.muenchen.oss.digiwf.email.integration.application.port.out.CorrelateMessagePort; import de.muenchen.oss.digiwf.email.integration.application.port.out.LoadMailAttachmentPort; +import de.muenchen.oss.digiwf.email.integration.application.port.out.MailPort; +import de.muenchen.oss.digiwf.email.integration.application.usecase.SendMailUseCase; +import de.muenchen.oss.digiwf.email.integration.infrastructure.MonitoringService; import de.muenchen.oss.digiwf.email.integration.model.Mail; import de.muenchen.oss.digiwf.message.process.api.ErrorApi; import de.muenchen.oss.digiwf.message.process.api.ProcessApi; import de.muenchen.oss.digiwf.s3.integration.client.repository.transfer.S3FileTransferRepository; +import io.micrometer.core.instrument.MeterRegistry; import lombok.RequiredArgsConstructor; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.mail.MailProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.mail.javamail.JavaMailSender; -import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.messaging.Message; -import jakarta.mail.MessagingException; -import java.util.Properties; import java.util.function.Consumer; @Configuration @RequiredArgsConstructor -@EnableConfigurationProperties({MailProperties.class, CustomMailProperties.class, MetricsProperties.class}) +@EnableConfigurationProperties({MailProperties.class, MetricsProperties.class}) public class MailAutoConfiguration { - private final MailProperties mailProperties; - private final CustomMailProperties customMailProperties; private final MetricsProperties metricsProperties; - /** - * Configures the {@link JavaMailSender} - * - * @return configured JavaMailSender - */ - @Bean - @ConditionalOnMissingBean - public JavaMailSender getJavaMailSender() throws MessagingException { - final JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); - mailSender.setHost(this.mailProperties.getHost()); - mailSender.setPort(this.mailProperties.getPort()); - mailSender.setProtocol(this.mailProperties.getProtocol()); - mailSender.setUsername(this.mailProperties.getUsername()); - mailSender.setPassword(this.mailProperties.getPassword()); - - final Properties props = mailSender.getJavaMailProperties(); - props.putAll(this.mailProperties.getProperties()); - mailSender.setJavaMailProperties(props); - mailSender.testConnection(); - return mailSender; - } - /** * Configures the {@link SendMail} use case. * - * @param javaMailSender JavaMailSender * @param loadAttachmentPort LoadMailAttachmentPort * @param correlateMessagePort CorrelateMessagePort + * @param mailPort MailPort * @return configured SendMail use case */ @Bean @ConditionalOnMissingBean - public SendMail getSendMailUseCase(final JavaMailSender javaMailSender, final LoadMailAttachmentPort loadAttachmentPort, final CorrelateMessagePort correlateMessagePort) { - return new SendMailUseCase(javaMailSender, loadAttachmentPort, correlateMessagePort, this.customMailProperties.getFromAddress()); + public SendMail getSendMailUseCase(final LoadMailAttachmentPort loadAttachmentPort, final CorrelateMessagePort correlateMessagePort, final MailPort mailPort) { + return new SendMailUseCase(loadAttachmentPort, correlateMessagePort, mailPort); } @Bean @@ -90,6 +65,12 @@ public LoadMailAttachmentPort getLoadMailAttachmentPort(final S3FileTransferRepo return new S3Adapter(s3FileTransferRepository); } + @Bean + @ConditionalOnMissingBean + public MailPort getMailPort(final DigiwfEmailApi digiwfEmailApi) { + return new MailAdapter(digiwfEmailApi); + } + // Function call had to be renamed for message routing @ConditionalOnMissingBean @Bean diff --git a/digiwf-integrations/digiwf-email-integration/pom.xml b/digiwf-integrations/digiwf-email-integration/pom.xml index 0cff8b38ea..b77311739a 100644 --- a/digiwf-integrations/digiwf-email-integration/pom.xml +++ b/digiwf-integrations/digiwf-email-integration/pom.xml @@ -43,6 +43,11 @@ digiwf-s3-integration-client-starter ${project.version} + + de.muenchen.oss.digiwf + digiwf-email-starter + ${project.version} + diff --git a/digiwf-libs/digiwf-email/digiwf-email-starter/src/main/java/de/muenchen/oss/digiwf/email/configuration/DigiwfEmailAutoConfiguration.java b/digiwf-libs/digiwf-email/digiwf-email-starter/src/main/java/de/muenchen/oss/digiwf/email/configuration/DigiwfEmailAutoConfiguration.java index 9e5e443ec0..350bd36e89 100644 --- a/digiwf-libs/digiwf-email/digiwf-email-starter/src/main/java/de/muenchen/oss/digiwf/email/configuration/DigiwfEmailAutoConfiguration.java +++ b/digiwf-libs/digiwf-email/digiwf-email-starter/src/main/java/de/muenchen/oss/digiwf/email/configuration/DigiwfEmailAutoConfiguration.java @@ -3,18 +3,46 @@ import de.muenchen.oss.digiwf.email.api.DigiwfEmailApi; import de.muenchen.oss.digiwf.email.impl.DigiwfEmailApiImpl; import de.muenchen.oss.digiwf.email.properties.CustomMailProperties; +import jakarta.mail.MessagingException; import lombok.RequiredArgsConstructor; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.mail.MailProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.JavaMailSenderImpl; + +import java.util.Properties; @RequiredArgsConstructor -@EnableConfigurationProperties(value = CustomMailProperties.class) +@EnableConfigurationProperties({MailProperties.class, CustomMailProperties.class}) public class DigiwfEmailAutoConfiguration { + private final MailProperties mailProperties; private final CustomMailProperties customMailProperties; + /** + * Configures the {@link JavaMailSender} + * + * @return configured JavaMailSender + */ + @Bean + @ConditionalOnMissingBean + public JavaMailSender getJavaMailSender() throws MessagingException { + final JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); + mailSender.setHost(this.mailProperties.getHost()); + mailSender.setPort(this.mailProperties.getPort()); + mailSender.setProtocol(this.mailProperties.getProtocol()); + mailSender.setUsername(this.mailProperties.getUsername()); + mailSender.setPassword(this.mailProperties.getPassword()); + + final Properties props = mailSender.getJavaMailProperties(); + props.putAll(this.mailProperties.getProperties()); + mailSender.setJavaMailProperties(props); + mailSender.testConnection(); + return mailSender; + } + @ConditionalOnMissingBean @Bean public DigiwfEmailApi digiwfEmailApi(final JavaMailSender javaMailSender) {