diff --git a/src/main/java/org/dstadler/commons/email/EmailConfig.java b/src/main/java/org/dstadler/commons/email/EmailConfig.java new file mode 100644 index 00000000..1e84279e --- /dev/null +++ b/src/main/java/org/dstadler/commons/email/EmailConfig.java @@ -0,0 +1,136 @@ +package org.dstadler.commons.email; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.dstadler.commons.logging.jdk.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + + +/** +* Configuration object for all properties necessary to send an email. +* +* Note: properties for the mail server configuration are provided in a +* separate object, see @link MailserverConfig. +* +* @author dominik.stadler +* +*/ +public class EmailConfig { + private static final Logger logger = LoggerFactory.make(); + + private String subject = ""; + + private String from = null; + + private List to = new ArrayList<>(), + cc = new ArrayList<>(), + bcc = new ArrayList<>(); + + public EmailConfig() { + super(); + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public List getTo() { + return to; + } + + public String getToAsEmail() { + return listToEmail(to); + } + + public void addTo(String toIn) { + this.to.add(toIn); + } + + public void setTo(final List toIn) { + if(toIn == null) { + this.to = new ArrayList<>(); + } else { + this.to = new ArrayList<>(toIn); + } + } + + public List getCc() { + return cc; + } + + public String getCcAsEmail() { + return listToEmail(cc); + } + + public void addCc(String ccIn) { + this.cc.add(ccIn); + } + + public void setCc(final List ccIn) { + if(ccIn == null) { + this.cc = new ArrayList<>(); + } else { + this.cc = new ArrayList<>(ccIn); + } + } + + public List getBcc() { + return bcc; + } + + public String getBccAsEmail() { + return listToEmail(bcc); + } + + public void addBcc(String bccIn) { + this.bcc.add(bccIn); + } + + public void setBcc(final List bccIn) { + if(bccIn == null) { + this.bcc = new ArrayList<>(); + } else { + this.bcc = new ArrayList<>(bccIn); + } + } + + public static String listToEmail(List list) { + StringBuilder email = new StringBuilder(); + for(String address : list) { + if(address == null || address.length() == 0) { + logger.warning("Trying to use email recipient without email address: " + address + " cannot send email to this recipient."); + } else { + email.append(address).append(','); + } + } + + // trim any trailing commas + while(email.length() > 0 && ',' == email.charAt(email.length()-1)) { + email.setLength(email.length()-1); + } + + return email.toString(); + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } +} diff --git a/src/main/java/org/dstadler/commons/email/MailserverConfig.java b/src/main/java/org/dstadler/commons/email/MailserverConfig.java new file mode 100644 index 00000000..bde22217 --- /dev/null +++ b/src/main/java/org/dstadler/commons/email/MailserverConfig.java @@ -0,0 +1,123 @@ +package org.dstadler.commons.email; + +import org.apache.commons.lang3.builder.ToStringBuilder; + + +/** + * Configuration object for the properties that are necessary to configure + * an SMTP email server. + * + * Note: properties for sending an email are stored in a separate object, + * see @link EmailConfig + * + * The value "verificationEmail()" is just a way to remember the email that + * was entered in the dialog across restarts, it is only + * used for sending emails to verify that the configuration is okay. + * + * @author dominik.stadler + * + */ +public class MailserverConfig { + //private static final Log logger = LogFactory.getLog(MailserverConfig.class); + + public static final int SERVER_PORT_DEFAULT = 25; + + private String serverAddress = ""; + private int serverPort = SERVER_PORT_DEFAULT; + + private String userId = ""; + private String password = ""; + private String bounce = ""; + private String subjectPrefix = ""; + private boolean sslEnabled = false; + + private boolean debug = false; + + public MailserverConfig() { + super(); + } + + public String getServerAddress() { + return serverAddress; + } + + public void setServerAddress(String serverAddress) { + this.serverAddress = serverAddress; + } + + public int getServerPort() { + return serverPort; + } + + public void setServerPort(int serverPort) { + this.serverPort = serverPort; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + if (this.password == null) { + this.password = ""; + } + } + + + public String getUserId() { + return userId; + } + + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getBounce() { + return bounce; + } + + public void setBounce(String bounce) { + this.bounce = bounce; + } + + public boolean isDebug() { + return debug; + } + + public void setDebug(boolean debug) { + this.debug = debug; + } + + public String getSubjectPrefix() { + return subjectPrefix; + } + + public void setSubjectPrefix(String subjectPrefix) { + this.subjectPrefix = subjectPrefix; + } + + public boolean isSSLEnabled() { + return sslEnabled; + } + + public void setSSLEnabled(boolean sslEnabled) { + this.sslEnabled = sslEnabled; + } + + @Override + public String toString() { + //return ToStringBuilder.reflectionToString(this); + return new ToStringBuilder(this). + append("serverAddress", serverAddress). + append("serverPort", serverPort). + append("userId", userId). + // excluded! append("password", password). + append("bounce", bounce). + append("subjectPrefix", subjectPrefix). + append("sslEnabled", sslEnabled). + append("debug", debug). + toString(); + } +} diff --git a/src/test/java/org/dstadler/commons/email/EmailConfigTest.java b/src/test/java/org/dstadler/commons/email/EmailConfigTest.java new file mode 100644 index 00000000..9194ea70 --- /dev/null +++ b/src/test/java/org/dstadler/commons/email/EmailConfigTest.java @@ -0,0 +1,69 @@ +package org.dstadler.commons.email; + +import org.dstadler.commons.testing.TestHelpers; +import org.junit.Test; + +import java.util.Collections; + +import static org.junit.Assert.*; + +public class EmailConfigTest { + @Test + public void test() { + EmailConfig config = new EmailConfig(); + + assertEquals("", config.getSubject()); + config.setSubject("testsubject"); + assertEquals("testsubject", config.getSubject()); + + assertNull(config.getFrom()); + config.setFrom("testfrom"); + assertEquals("testfrom", config.getFrom()); + + assertTrue(config.getTo().isEmpty()); + config.addTo("test@to"); + assertEquals("[test@to]", config.getTo().toString()); + assertEquals("test@to", config.getToAsEmail()); + config.addTo("2nd@to"); + assertEquals("[test@to, 2nd@to]", config.getTo().toString()); + assertEquals("test@to,2nd@to", config.getToAsEmail()); + + config.setTo(null); + assertTrue(config.getTo().isEmpty()); + config.setTo(Collections.singletonList("3rd@to")); + assertEquals("[3rd@to]", config.getTo().toString()); + + assertTrue(config.getCc().isEmpty()); + config.addCc("test@Cc"); + assertEquals("[test@Cc]", config.getCc().toString()); + assertEquals("test@Cc", config.getCcAsEmail()); + config.addCc("2nd@Cc"); + assertEquals("[test@Cc, 2nd@Cc]", config.getCc().toString()); + assertEquals("test@Cc,2nd@Cc", config.getCcAsEmail()); + + config.setCc(null); + assertTrue(config.getCc().isEmpty()); + config.setCc(Collections.singletonList("3rd@Cc")); + assertEquals("[3rd@Cc]", config.getCc().toString()); + + assertTrue(config.getBcc().isEmpty()); + config.addBcc("test@Bcc"); + assertEquals("[test@Bcc]", config.getBcc().toString()); + assertEquals("test@Bcc", config.getBccAsEmail()); + config.addBcc("2nd@Bcc"); + assertEquals("[test@Bcc, 2nd@Bcc]", config.getBcc().toString()); + assertEquals("test@Bcc,2nd@Bcc", config.getBccAsEmail()); + + config.setBcc(null); + assertTrue(config.getBcc().isEmpty()); + config.setBcc(Collections.singletonList("3rd@Bcc")); + assertEquals("[3rd@Bcc]", config.getBcc().toString()); + + assertEquals("test@list", EmailConfig.listToEmail(Collections.singletonList("test@list"))); + assertEquals("", EmailConfig.listToEmail(Collections.singletonList((String)null))); + assertEquals("", EmailConfig.listToEmail(Collections.singletonList(""))); + + TestHelpers.ToStringTest(config); + TestHelpers.ToStringTest(new EmailConfig()); + } +} diff --git a/src/test/java/org/dstadler/commons/email/MailserverConfigTest.java b/src/test/java/org/dstadler/commons/email/MailserverConfigTest.java new file mode 100644 index 00000000..0c2efd85 --- /dev/null +++ b/src/test/java/org/dstadler/commons/email/MailserverConfigTest.java @@ -0,0 +1,50 @@ +package org.dstadler.commons.email; + +import org.dstadler.commons.testing.TestHelpers; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class MailserverConfigTest { + @Test + public void test() { + MailserverConfig config = new MailserverConfig(); + + assertEquals("", config.getServerAddress()); + config.setServerAddress("somehost"); + assertEquals("somehost", config.getServerAddress()); + + assertEquals(25, config.getServerPort()); + config.setServerPort(834); + assertEquals(834, config.getServerPort()); + + assertEquals("", config.getPassword()); + config.setPassword("somepwd"); + assertEquals("somepwd", config.getPassword()); + config.setPassword(null); + assertEquals("", config.getPassword()); + + assertEquals("", config.getUserId()); + config.setUserId("someuser"); + assertEquals("someuser", config.getUserId()); + + assertEquals("", config.getBounce()); + config.setBounce("someuser1"); + assertEquals("someuser1", config.getBounce()); + + assertFalse(config.isDebug()); + config.setDebug(true); + assertTrue(config.isDebug()); + + assertEquals("", config.getSubjectPrefix()); + config.setSubjectPrefix("pref"); + assertEquals("pref", config.getSubjectPrefix()); + + assertFalse(config.isSSLEnabled()); + config.setSSLEnabled(true); + assertTrue(config.isSSLEnabled()); + + TestHelpers.ToStringTest(config); + TestHelpers.ToStringTest(new MailserverConfig()); + } +} \ No newline at end of file