added protocol support

This commit is contained in:
jomu
2016-11-24 20:46:30 +00:00
parent c3491b3e2c
commit 1ea84ef84d
4 changed files with 84 additions and 11 deletions

View File

@ -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 {

View File

@ -0,0 +1,41 @@
package de.muehlencord.shared.network.mail;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
*
* @author Joern Muehlencord <joern at muehlencord.de>
*/
public enum MailProtocol {
IMAP("imap"),
IMAPS("imaps");
private final static Map<String, MailProtocol> 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;
}
}

View File

@ -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;
}
}

View File

@ -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
*