fix SimpleJavaMail configuration username and password handling

This commit is contained in:
Vadim Karantayer 2021-08-10 10:45:30 -04:00
parent 3edb59475c
commit f68040aae9
3 changed files with 40 additions and 15 deletions

View File

@ -1,61 +1,64 @@
package ca.uhn.fhir.rest.server.mail;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
public class MailConfig {
private String smtpHostname;
private Integer smtpPort;
private String smtpUsername;
private String smtpPassword;
private boolean smtpUseStartTLS;
private String mySmtpHostname;
private Integer mySmtpPort;
private String mySmtpUsername;
private String mySmtpPassword;
private boolean mySmtpUseStartTLS;
public MailConfig() {
}
public String getSmtpHostname() {
return smtpHostname;
return mySmtpHostname;
}
public MailConfig setSmtpHostname(String theSmtpHostname) {
smtpHostname = theSmtpHostname;
mySmtpHostname = theSmtpHostname;
return this;
}
public Integer getSmtpPort() {
return smtpPort;
return mySmtpPort;
}
public MailConfig setSmtpPort(Integer theSmtpPort) {
smtpPort = theSmtpPort;
mySmtpPort = theSmtpPort;
return this;
}
public String getSmtpUsername() {
return smtpUsername;
return mySmtpUsername;
}
public MailConfig setSmtpUsername(String theSmtpUsername) {
smtpUsername = theSmtpUsername;
// SimpleJavaMail treats empty smtp username as valid username and requires auth
mySmtpUsername = StringUtils.isBlank(theSmtpUsername) ? null : theSmtpUsername;
return this;
}
public String getSmtpPassword() {
return smtpPassword;
return mySmtpPassword;
}
public MailConfig setSmtpPassword(String theSmtpPassword) {
smtpPassword = theSmtpPassword;
// SimpleJavaMail treats empty smtp password as valid password and requires auth
mySmtpPassword = StringUtils.isBlank(theSmtpPassword) ? null : theSmtpPassword;
return this;
}
public boolean isSmtpUseStartTLS() {
return smtpUseStartTLS;
return mySmtpUseStartTLS;
}
public MailConfig setSmtpUseStartTLS(boolean theSmtpUseStartTLS) {
smtpUseStartTLS = theSmtpUseStartTLS;
mySmtpUseStartTLS = theSmtpUseStartTLS;
return this;
}

View File

@ -62,6 +62,9 @@ public class MailSvc {
@Nonnull
private Mailer makeMailer(@Nonnull MailConfig theMailConfig) {
ourLog.info("SMTP Mailer config Hostname:[{}] | Port:[{}] | Username:[{}] | TLS:[{}]",
theMailConfig.getSmtpHostname(), theMailConfig.getSmtpPort(),
theMailConfig.getSmtpUsername(), theMailConfig.isSmtpUseStartTLS());
return MailerBuilder
.withSMTPServer(
theMailConfig.getSmtpHostname(),

View File

@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -86,4 +87,22 @@ public class MailConfigTest {
assertNotEquals(fixture, null);
}
@Test
public void testSetSmtpUsername() {
// execute & validate
assertEquals("xyz", fixture.setSmtpUsername("xyz").getSmtpUsername());
assertNull(fixture.setSmtpUsername(null).getSmtpUsername());
assertNull(fixture.setSmtpUsername("").getSmtpUsername());
assertNull(fixture.setSmtpUsername(" ").getSmtpUsername());
}
@Test
public void testSetSmtpPassword() {
// execute & validate
assertEquals("xyz", fixture.setSmtpPassword("xyz").getSmtpPassword());
assertNull(fixture.setSmtpPassword(null).getSmtpPassword());
assertNull(fixture.setSmtpPassword("").getSmtpPassword());
assertNull(fixture.setSmtpPassword(" ").getSmtpPassword());
}
}