From 3f6d1eb29beb44232e56d5974e78d29ec1c83506 Mon Sep 17 00:00:00 2001 From: Thomas Papke Date: Thu, 26 Sep 2024 04:07:27 +0200 Subject: [PATCH] #5768 Upgrade to latest simple-java-mail (#6261) --- hapi-fhir-jpaserver-subscription/pom.xml | 5 +++ .../email/EmailSubscriptionDstu2Test.java | 4 +-- .../email/EmailSenderImplTest.java | 4 +-- .../email/EmailSubscriptionDstu3Test.java | 4 +-- hapi-fhir-server-cds-hooks/pom.xml | 14 -------- .../uhn/fhir/rest/server/mail/IMailSvc.java | 6 ++-- .../ca/uhn/fhir/rest/server/mail/MailSvc.java | 35 ++++++++++--------- .../uhn/fhir/rest/server/mail/MailSvcIT.java | 7 ++-- pom.xml | 33 +++++++++++------ 9 files changed, 57 insertions(+), 55 deletions(-) diff --git a/hapi-fhir-jpaserver-subscription/pom.xml b/hapi-fhir-jpaserver-subscription/pom.xml index ba09eed0ab5..45504f94b2f 100644 --- a/hapi-fhir-jpaserver-subscription/pom.xml +++ b/hapi-fhir-jpaserver-subscription/pom.xml @@ -70,6 +70,11 @@ jakarta.servlet-api provided + + jakarta.mail + jakarta.mail-api + true + diff --git a/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu2Test.java b/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu2Test.java index 90726afb0c8..ae1e985ed22 100644 --- a/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu2Test.java +++ b/hapi-fhir-jpaserver-test-dstu2/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu2Test.java @@ -28,8 +28,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeMessage; +import jakarta.mail.internet.InternetAddress; +import jakarta.mail.internet.MimeMessage; import java.util.ArrayList; import java.util.Arrays; import java.util.List; diff --git a/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSenderImplTest.java b/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSenderImplTest.java index fa871fa2595..9c01f3aeb90 100644 --- a/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSenderImplTest.java +++ b/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSenderImplTest.java @@ -17,8 +17,8 @@ import org.junit.jupiter.api.extension.RegisterExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeMessage; +import jakarta.mail.internet.InternetAddress; +import jakarta.mail.internet.MimeMessage; import java.util.Arrays; import static org.assertj.core.api.Assertions.assertThat; diff --git a/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu3Test.java b/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu3Test.java index 28d8ae61b35..825f6ebb9ae 100644 --- a/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu3Test.java +++ b/hapi-fhir-jpaserver-test-dstu3/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu3Test.java @@ -26,8 +26,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.springframework.beans.factory.annotation.Autowired; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeMessage; +import jakarta.mail.internet.InternetAddress; +import jakarta.mail.internet.MimeMessage; import java.util.ArrayList; import java.util.Arrays; import java.util.List; diff --git a/hapi-fhir-server-cds-hooks/pom.xml b/hapi-fhir-server-cds-hooks/pom.xml index 1f396dc4fdc..281604dba5a 100644 --- a/hapi-fhir-server-cds-hooks/pom.xml +++ b/hapi-fhir-server-cds-hooks/pom.xml @@ -79,25 +79,11 @@ org.simplejavamail simple-java-mail - - - - com.sun.activation - jakarta.activation - - com.icegreen greenmail compile - - - - com.sun.activation - jakarta.activation - - diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/IMailSvc.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/IMailSvc.java index 2a4d3ea6d56..62e67076204 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/IMailSvc.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/IMailSvc.java @@ -21,9 +21,9 @@ package ca.uhn.fhir.rest.server.mail; import jakarta.annotation.Nonnull; import org.simplejavamail.api.email.Email; -import org.simplejavamail.api.mailer.AsyncResponse; import java.util.List; +import java.util.function.Consumer; public interface IMailSvc { void sendMail(@Nonnull List theEmails); @@ -31,7 +31,5 @@ public interface IMailSvc { void sendMail(@Nonnull Email theEmail); void sendMail( - @Nonnull Email theEmail, - @Nonnull Runnable theOnSuccess, - @Nonnull AsyncResponse.ExceptionConsumer theErrorHandler); + @Nonnull Email theEmail, @Nonnull Runnable theOnSuccess, @Nonnull Consumer theErrorHandler); } diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/MailSvc.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/MailSvc.java index 45299edda15..adc7111d4b9 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/MailSvc.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/MailSvc.java @@ -20,12 +20,9 @@ package ca.uhn.fhir.rest.server.mail; import jakarta.annotation.Nonnull; -import org.apache.commons.lang3.Validate; import org.simplejavamail.MailException; import org.simplejavamail.api.email.Email; import org.simplejavamail.api.email.Recipient; -import org.simplejavamail.api.mailer.AsyncResponse; -import org.simplejavamail.api.mailer.AsyncResponse.ExceptionConsumer; import org.simplejavamail.api.mailer.Mailer; import org.simplejavamail.api.mailer.config.TransportStrategy; import org.simplejavamail.mailer.MailerBuilder; @@ -33,6 +30,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.List; +import java.util.Objects; +import java.util.function.Consumer; import java.util.stream.Collectors; public class MailSvc implements IMailSvc { @@ -42,14 +41,14 @@ public class MailSvc implements IMailSvc { private final Mailer myMailer; public MailSvc(@Nonnull MailConfig theMailConfig) { - Validate.notNull(theMailConfig); + Objects.requireNonNull(theMailConfig); myMailConfig = theMailConfig; myMailer = makeMailer(myMailConfig); } @Override public void sendMail(@Nonnull List theEmails) { - Validate.notNull(theEmails); + Objects.requireNonNull(theEmails); theEmails.forEach(theEmail -> send(theEmail, new OnSuccess(theEmail), new ErrorHandler(theEmail))); } @@ -60,21 +59,23 @@ public class MailSvc implements IMailSvc { @Override public void sendMail( - @Nonnull Email theEmail, @Nonnull Runnable theOnSuccess, @Nonnull ExceptionConsumer theErrorHandler) { + @Nonnull Email theEmail, @Nonnull Runnable theOnSuccess, @Nonnull Consumer theErrorHandler) { send(theEmail, theOnSuccess, theErrorHandler); } private void send( - @Nonnull Email theEmail, @Nonnull Runnable theOnSuccess, @Nonnull ExceptionConsumer theErrorHandler) { - Validate.notNull(theEmail); - Validate.notNull(theOnSuccess); - Validate.notNull(theErrorHandler); + @Nonnull Email theEmail, @Nonnull Runnable theOnSuccess, @Nonnull Consumer theErrorHandler) { + Objects.requireNonNull(theEmail); + Objects.requireNonNull(theOnSuccess); + Objects.requireNonNull(theErrorHandler); try { - final AsyncResponse asyncResponse = myMailer.sendMail(theEmail, true); - if (asyncResponse != null) { - asyncResponse.onSuccess(theOnSuccess); - asyncResponse.onException(theErrorHandler); - } + myMailer.sendMail(theEmail, true).whenComplete((result, ex) -> { + if (ex != null) { + theErrorHandler.accept(ex); + } else { + theOnSuccess.run(); + } + }); } catch (MailException e) { theErrorHandler.accept(e); } @@ -117,7 +118,7 @@ public class MailSvc implements IMailSvc { } } - private class ErrorHandler implements ExceptionConsumer { + private class ErrorHandler implements Consumer { private final Email myEmail; private ErrorHandler(@Nonnull Email theEmail) { @@ -125,7 +126,7 @@ public class MailSvc implements IMailSvc { } @Override - public void accept(Exception t) { + public void accept(Throwable t) { ourLog.error("Email not sent" + makeMessage(myEmail), t); } } diff --git a/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/mail/MailSvcIT.java b/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/mail/MailSvcIT.java index 2c2fd6c2d52..a78b4f8c26c 100644 --- a/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/mail/MailSvcIT.java +++ b/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/mail/MailSvcIT.java @@ -4,6 +4,7 @@ import com.icegreen.greenmail.junit5.GreenMailExtension; import com.icegreen.greenmail.util.GreenMailUtil; import com.icegreen.greenmail.util.ServerSetupTest; import jakarta.annotation.Nonnull; +import jakarta.mail.internet.MimeMessage; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -11,7 +12,6 @@ import org.simplejavamail.MailException; import org.simplejavamail.api.email.Email; import org.simplejavamail.email.EmailBuilder; -import javax.mail.internet.MimeMessage; import java.util.Arrays; import java.util.List; @@ -86,13 +86,14 @@ public class MailSvcIT { @Test public void testSendMailWithInvalidToAddressExpectErrorHandler() { // setup - final Email email = withEmail("xyz"); + String invalidEmailAdress = "xyz"; + final Email email = withEmail(invalidEmailAdress); // execute fixture.sendMail(email, () -> fail("Should not execute on Success"), (e) -> { assertTrue(e instanceof MailException); - assertEquals("Invalid TO address: " + email, e.getMessage()); + assertEquals("Invalid TO address: " + invalidEmailAdress, e.getMessage()); }); // validate assertTrue(ourGreenMail.waitForIncomingEmail(1000, 0)); diff --git a/pom.xml b/pom.xml index 481c3a77047..0f562d546bd 100644 --- a/pom.xml +++ b/pom.xml @@ -1161,27 +1161,38 @@ org.simplejavamail simple-java-mail - 6.6.1 + 8.11.2 - com.sun.activation - jakarta.activation-api + com.github.bbottema + jetbrains-runtime-annotations - com.sun.activation - jakarta.activation + jakarta.mail + jakarta.mail-api + + + + + jakarta.mail + jakarta.mail-api + 2.1.3 + + + com.icegreen + greenmail + 2.1.0-rc-1 + + + jakarta.mail + jakarta.mail-api - - com.icegreen - greenmail - 1.6.4 - com.icegreen greenmail-junit5 - 1.6.4 + 2.1.0-rc-1 compile