in bindings/servicemix-mail/src/main/java/org/apache/servicemix/mail/MailPollerEndpoint.java [169:320]
public void poll() throws Exception {
logger.debug("Polling mailfolder {} at host {} ...", config.getFolderName(), config.getHost());
if (maxFetchSize == 0) {
logger.debug("The configuration is set to poll no new messages at all...skipping.");
return;
}
boolean isPopProtocol = this.config.getProtocol().toLowerCase().indexOf("pop") > -1;
// clear the list each run
this.foundMessagesInFolder.clear();
Store store = null;
Folder folder = null;
Session session;
try {
Properties props = MailUtils.getPropertiesForProtocol(this.config, this.customTrustManagers);
props.put("mail.debug", isDebugMode() ? "true" : "false");
// apply the custom properties
applyCustomProperties(props);
// Get session
session = Session.getInstance(props, config.getAuthenticator());
// debug the session
session.setDebug(this.debugMode);
store = session.getStore(config.getProtocol());
store.connect(config.getHost(), config.getUsername(), config.getPassword());
folder = store.getFolder(config.getFolderName());
if (folder == null || !folder.exists()) {
throw new Exception("Folder not found or invalid: " + config.getFolderName());
}
folder.open(Folder.READ_WRITE);
Message[] messages;
if (isProcessOnlyUnseenMessages() && !isPopProtocol) {
messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
} else {
messages = folder.getMessages();
}
int fetchSize = getMaxFetchSize() == -1 ? messages.length : Math.min(getMaxFetchSize(),
messages.length);
int fetchedMessages = 0;
String uid;
for (Message msg : messages) {
uid = null;
// get the message
MimeMessage mailMsg = (MimeMessage)msg;
if (isProcessOnlyUnseenMessages() && isPopProtocol) {
// POP3 doesn't support flags, so we need to check manually
// if message is new or not
try {
Object ouid = folder.getClass().getMethod("getUID", Message.class).invoke(folder, mailMsg);
// remember each found message
if (ouid != null) {
uid = (String)ouid;
foundMessagesInFolder.add(uid);
}
// check if we already processed the message
if (uid != null && this.seenMessages.contains(uid)) {
// this message was already processed
continue;
}
} catch (Exception ex) {
// this folder doesn't provide UIDs for messages
logger.warn("{}: Unable to determine unique id of mail.", getEndpoint(), ex);
}
}
// only process a message if the max message fetch size isn't
// exceeded then
if (fetchedMessages < fetchSize) {
// create a inOnly exchange
InOnly io = getExchangeFactory().createInOnlyExchange();
// configure the exchange target
configureExchangeTarget(io);
// create the in message
NormalizedMessage normalizedMessage = io.createMessage();
// now let the marshaller convert the mail into a normalized
// message to send to jbi bus
marshaler.convertMailToJBI(io, normalizedMessage, mailMsg);
// then put the in message into the inOnly exchange
io.setInMessage(normalizedMessage);
// and use sendSync to deliver it
sendSync(io);
// increment the fetched messages counter
fetchedMessages++;
// now check if delivery succeeded or went wrong
if (io.getStatus() == ExchangeStatus.ERROR) {
// to ensure reprocessing of the mail we set it to UNSEEN even if we
// did not mark it seen before (seems there are some mail systems out there
// which do set somehow automatically)
mailMsg.setFlag(Flags.Flag.SEEN, false);
Exception e = io.getError();
if (e == null) {
e = new JBIException("Unexpected error occured...");
}
throw e;
} else {
// then mark the mail as processed (only if no errors)
if (deleteProcessedMessages) {
// processed messages have to be marked as deleted
mailMsg.setFlag(Flags.Flag.DELETED, true);
} else {
// processed messages have to be marked as seen
mailMsg.setFlag(Flags.Flag.SEEN, true);
}
// remember the processed mail if needed
if (isProcessOnlyUnseenMessages() && isPopProtocol && uid != null) {
// POP3 doesn't support flags, so we need to
// remember processed mails
this.seenMessages.add(uid);
}
}
}
}
} finally {
// finally clean up and close the folder and store
try {
if (folder != null) {
folder.close(true);
}
if (store != null) {
store.close();
}
// clean up the seen messages list because of maybe deleted
// messages
if (isProcessOnlyUnseenMessages() && isPopProtocol) {
cleanUpSeenMessages();
}
} catch (Exception ignored) {
logger.debug("", ignored);
}
}
}