From 1ea84ef84dab5abfdf5288b9694a1143256076d8 Mon Sep 17 00:00:00 2001 From: jomu Date: Thu, 24 Nov 2016 20:46:30 +0000 Subject: [PATCH] added protocol support --- .../network/mail/DefaultMailReader.java | 2 + .../shared/network/mail/MailProtocol.java | 41 +++++++++++++++++++ .../network/mail/MailReaderConfiguration.java | 37 +++++++++++++---- .../network/mail/imap/ImapMailReader.java | 15 +++++-- 4 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 network/src/main/java/de/muehlencord/shared/network/mail/MailProtocol.java diff --git a/network/src/main/java/de/muehlencord/shared/network/mail/DefaultMailReader.java b/network/src/main/java/de/muehlencord/shared/network/mail/DefaultMailReader.java index 23f8f57..a20a2a1 100644 --- a/network/src/main/java/de/muehlencord/shared/network/mail/DefaultMailReader.java +++ b/network/src/main/java/de/muehlencord/shared/network/mail/DefaultMailReader.java @@ -255,8 +255,10 @@ public abstract class DefaultMailReader implements MailReader { int messageCount; try { + LOGGER.debug ("getting message count from folder {}", sourceFolder); folder.open(Folder.READ_ONLY); messageCount = folder.getMessageCount(); + LOGGER.debug("{} messages found infolder", messageCount); } catch (Exception ex) { throw new MailReaderException("Error while getting messageCount. Reason:" + ex.getMessage(), ex); } finally { diff --git a/network/src/main/java/de/muehlencord/shared/network/mail/MailProtocol.java b/network/src/main/java/de/muehlencord/shared/network/mail/MailProtocol.java new file mode 100644 index 0000000..2c5149e --- /dev/null +++ b/network/src/main/java/de/muehlencord/shared/network/mail/MailProtocol.java @@ -0,0 +1,41 @@ +package de.muehlencord.shared.network.mail; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * + * @author Joern Muehlencord + */ +public enum MailProtocol { + + IMAP("imap"), + IMAPS("imaps"); + + private final static Map protocolNameMap = new ConcurrentHashMap<>(); + + static { + for (MailProtocol currentProtocol : MailProtocol.values()) { + protocolNameMap.put(currentProtocol.getName(), currentProtocol); + } + } + + public static MailProtocol getByProtocolName(String protocolName) { + if (protocolNameMap.containsKey(protocolName)) { + return protocolNameMap.get(protocolName); + } else { + return null; + } + } + + private MailProtocol(String protocolName) { + this.name = protocolName; + } + + private final String name; + + public String getName() { + return name; + } + +} diff --git a/network/src/main/java/de/muehlencord/shared/network/mail/MailReaderConfiguration.java b/network/src/main/java/de/muehlencord/shared/network/mail/MailReaderConfiguration.java index 19da782..6c01d64 100644 --- a/network/src/main/java/de/muehlencord/shared/network/mail/MailReaderConfiguration.java +++ b/network/src/main/java/de/muehlencord/shared/network/mail/MailReaderConfiguration.java @@ -5,25 +5,38 @@ package de.muehlencord.shared.network.mail; * @author Joern Muehlencord */ public class MailReaderConfiguration { - /** server for smtp sessions */ + + /** + * server for smtp sessions + */ private String smtpHost = null; - /** server for incoming emails - e.g. the imap or pop3 server */ + /** + * server for incoming emails - e.g. the imap or pop3 server + */ private String readerHost = null; - /** the username to connect with */ + /** + * the username to connect with + */ private String userName = null; - /** the password to connect with */ + /** + * the password to connect with + */ private String password = null; - + /** + * the protocol to use, currently supported imap and imaps + */ + private MailProtocol protocol = null; + // TODO add checkConfig to asure the config is valid for the reader it is attached to - /** * creates a new config + * * @param mailHost the smtp host to connect to * @param host the mailreader host (imap, exchange, pop, ...) to connect to * @param user the username to connect with * @param pw the password to connect with */ - public MailReaderConfiguration (String mailHost, String host, String user, String pw) { + public MailReaderConfiguration(String mailHost, String host, String user, String pw) { this.smtpHost = mailHost; this.readerHost = host; this.userName = user; @@ -57,5 +70,13 @@ public class MailReaderConfiguration { public String getPassword() { return password; } - + + public MailProtocol getProtocol() { + return protocol; + } + + public void setProtocol(MailProtocol protocol) { + this.protocol = protocol; + } + } diff --git a/network/src/main/java/de/muehlencord/shared/network/mail/imap/ImapMailReader.java b/network/src/main/java/de/muehlencord/shared/network/mail/imap/ImapMailReader.java index 0963aec..6bda2da 100644 --- a/network/src/main/java/de/muehlencord/shared/network/mail/imap/ImapMailReader.java +++ b/network/src/main/java/de/muehlencord/shared/network/mail/imap/ImapMailReader.java @@ -1,11 +1,13 @@ package de.muehlencord.shared.network.mail.imap; import de.muehlencord.shared.network.mail.DefaultMailReader; +import de.muehlencord.shared.network.mail.MailProtocol; import de.muehlencord.shared.network.mail.MailReaderConfiguration; import de.muehlencord.shared.network.mail.MailReaderConnectionException; import javax.mail.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + /** * Implementation of MaiLReader to connect to an IMAP server * @@ -13,7 +15,9 @@ import org.slf4j.LoggerFactory; */ public class ImapMailReader extends DefaultMailReader { - /** the logger object */ + /** + * the logger object + */ private final static Logger LOGGER = LoggerFactory.getLogger(ImapMailReader.class); /** @@ -32,8 +36,13 @@ public class ImapMailReader extends DefaultMailReader { String hostName = getConfiguration().getReaderHost(); String userName = getConfiguration().getUserName(); String password = getConfiguration().getPassword(); + MailProtocol protocol = getConfiguration().getProtocol(); + if (protocol == null) { + protocol = MailProtocol.IMAP; + } + try { - setStore(session.getStore("imap")); // TODO add ssl support + setStore(session.getStore(protocol.getName())); getStore().connect(hostName, userName, password); } catch (Exception ex) { String hint = "Error while connecting to mailbox " + getConnectionShortCut(); @@ -42,7 +51,7 @@ public class ImapMailReader extends DefaultMailReader { } LOGGER.info("Connected to " + getConnectionShortCut()); } - + /** * returns userName@hostname *