Skip to content

Commit

Permalink
Cache mail session, but update if properties changed. IQSS#7424
Browse files Browse the repository at this point in the history
  • Loading branch information
poikilotherm committed Dec 3, 2020
1 parent a8cd516 commit 24496b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,33 @@ public class MailSessionProducer {

public static final String MAIL_CONFIG_PREFIX = "dataverse.mail.system";

Session systemMailSession;

@Produces
@Resource(name = "java:app/notify/mail/system")
public Session getSystemMailSession() {
return Session.getInstance(getMailProperties());
// return the saved session if properties have not changed
if (systemMailSession != null && systemMailSession.getProperties().equals(getMailProperties()))
return systemMailSession;

// otherwise update and return
systemMailSession = Session.getInstance(getMailProperties());
return systemMailSession;
}

Properties getMailProperties() {
Config config = ConfigProvider.getConfig();

// Map properties 1:1 to mail.smtp properties for the mail session.
// See https://javaee.github.io/javamail/docs/api/com/sun/mail/smtp/package-summary.html for an extensive list
// of options.
Map<String,String> propMap = StreamSupport.stream(config.getPropertyNames().spliterator(), false)
.filter(prop -> prop.startsWith(MAIL_CONFIG_PREFIX+"."))
.collect(Collectors.toConcurrentMap(s -> s.replace(MAIL_CONFIG_PREFIX, "mail.smtp"), s -> config.getValue(s, String.class)));
.collect(
Collectors.toConcurrentMap(
s -> s.replace(MAIL_CONFIG_PREFIX, "mail.smtp"),
s -> config.getValue(s, String.class)
)
);
Properties mailProps = new Properties();
mailProps.putAll(propMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.io.PrintWriter;
import java.util.Date;

import static com.jayway.restassured.RestAssured.given;
Expand Down

0 comments on commit 24496b9

Please sign in to comment.