From d8198b53c3c92af2e91fe6e1df65af791b356b77 Mon Sep 17 00:00:00 2001 From: Oliver Bertuch Date: Tue, 26 Mar 2024 14:40:11 +0100 Subject: [PATCH] style(mail): enable more debug output from session producer In case people want to debug Jakarta Mail, they activate dataverse.mail.debug. Let's hook into that and add more verbose output from the session producer, too. That way people can make sure everything is set up as they wish. --- .../dataverse/util/MailSessionProducer.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/util/MailSessionProducer.java b/src/main/java/edu/harvard/iq/dataverse/util/MailSessionProducer.java index 13fedb94014..149f92761d2 100644 --- a/src/main/java/edu/harvard/iq/dataverse/util/MailSessionProducer.java +++ b/src/main/java/edu/harvard/iq/dataverse/util/MailSessionProducer.java @@ -16,6 +16,7 @@ import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; @ApplicationScoped public class MailSessionProducer { @@ -42,6 +43,12 @@ public class MailSessionProducer { private static final String PREFIX = "mail.smtp."; private static final Logger logger = Logger.getLogger(MailSessionProducer.class.getCanonicalName()); + static { + if (Boolean.TRUE.equals(JvmSettings.MAIL_DEBUG.lookup(Boolean.class))) { + logger.setLevel(Level.FINE); + } + } + Session systemMailSession; /** @@ -60,7 +67,7 @@ public MailSessionProducer() { } catch (NamingException e) { // This exception simply means the appserver did not provide the legacy mail session. // Debug level output is just fine. - logger.log(Level.FINE, "Error during mail resource lookup", e); + logger.log(Level.FINER, "Error during legacy appserver-level mail resource lookup", e); } } @@ -75,14 +82,21 @@ public Session getSession() { } if (systemMailSession == null) { + logger.fine("Setting up new mail session"); + // Initialize with null (= no authenticator) is a valid argument for the session factory method. Authenticator authenticator = null; // In case we want auth, create an authenticator (default = false from microprofile-config.properties) - if (JvmSettings.MAIL_MTA_AUTH.lookup(Boolean.class)) { + if (Boolean.TRUE.equals(JvmSettings.MAIL_MTA_AUTH.lookup(Boolean.class))) { + logger.fine("Mail Authentication is enabled, building authenticator"); authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { + logger.fine(() -> + String.format("Returning PasswordAuthenticator with username='%s', password='%s'", + JvmSettings.MAIL_MTA_USER.lookup(), + "*".repeat(JvmSettings.MAIL_MTA_PASSWORD.lookup().length()))); return new PasswordAuthentication(JvmSettings.MAIL_MTA_USER.lookup(), JvmSettings.MAIL_MTA_PASSWORD.lookup()); } }; @@ -116,6 +130,10 @@ Properties getMailProperties() { prop -> JvmSettings.MAIL_MTA_SETTING.lookupOptional(Integer.class, prop).ifPresent( number -> configuration.put(PREFIX + prop, number.toString()))); + logger.fine(() -> "Compiled properties:" + configuration.entrySet().stream() + .map(entry -> "\"" + entry.getKey() + "\": \"" + entry.getValue() + "\"") + .collect(Collectors.joining(",\n"))); + return configuration; }