*/
public EmailData[] readInbox(boolean clear) throws XmlBlasterException {
// if (isShutdown()) Does it recover automatically after a shutdown?
// throw new XmlBlasterException(glob, ErrorCode.INTERNAL_ILLEGALSTATE,
// Pop3Driver.class.getName(), "The plugin is shutdown");
Store store = null;
Folder inbox = null;
try {
store = getStore();
Folder root = store.getDefaultFolder();
inbox = root.getFolder(POP3_FOLDER);
inbox.open(Folder.READ_WRITE);
Message[] msgs = inbox.getMessages();
if (msgs == null)
msgs = new Message[0];
EmailData[] datas = new EmailData[msgs.length];
for (int i = 0; i < msgs.length; i++) {
log.fine("Reading message #" + (i+1) + "/" + msgs.length + " from INBOX");
MimeMessage msg = (MimeMessage) msgs[i];
if (clear)
msg.setFlag(Flags.Flag.DELETED, true);
Address[] froms = msg.getFrom();
String from = (froms != null && froms.length > 0) ? froms[0]
.toString() : "";
Address[] arr = msg.getAllRecipients();
if (arr == null)
arr = new Address[0];
String[] recips = new String[arr.length];
for (int j = 0; j < arr.length; j++)
recips[j] = arr[j].toString();
//String content = retrieveContent(msg); // Would sometimes deliver an attachment
String content = "";
datas[i] = new EmailData(recips, from, msg.getSubject(), content);
datas[i].setSentDate(msg.getSentDate());
datas[i].setReplyTo((InternetAddress[])msg.getReplyTo());
/*
String[] expires = msg.getHeader(EmailData.EXPIRES_HEADER);
// "X-xmlBlaster-ExpiryDate: 2005-12-24 16:45:55.322"
if (expires != null && expires.length > 0) {
// expires[0]="2005-12-24 16:45:55.322"
String value = expires[0].trim();
try {
datas[i].setExpiryTime(java.sql.Timestamp.valueOf(value)); // Change to IsoDateParser with UTC!
}
catch (Throwable e) {
System.err.println("xmlBlaster Pop3Driver.java: Ignoring illegal email header '" + expires[0] + "'");
e.printStackTrace();
}
}
else {
*/
// Expires: Thu, 15 Dec 2005 21:45:01 +0100 (CET)
String[] expires = msg.getHeader(EmailData.EXPIRES_HEADER_RFC2156);
if (expires != null && expires.length > 0) {
// Date: Thu, 17 Nov 2005 16:45:12 +0100 (CET)
String value = expires[0].trim();
try {
datas[i].setExpiryTime(MailUtil.dateTimeTS(value));
}
catch (Throwable e) {
System.err.println("xmlBlaster Pop3Driver.java: Ignoring illegal email header '" + expires[0] + "'");
e.printStackTrace();
}
}
//}
datas[i].setAttachments(MailUtil.accessAttachments(msg));
}
return datas;
} catch (MessagingException e) {
throw new XmlBlasterException(this.glob,
ErrorCode.RESOURCE_CONFIGURATION, Pop3Driver.class.getName(),
"Problems to read POP3 email from '" + getUrlWithoutPassword()
+ "'", e);
} finally {
try {
if (inbox != null) {
inbox.close(true);
inbox = null;
}
} catch (Exception e) { // MessagingException, IOException
log.warning("Ignoring inbox close problem: " + e.toString());
}
try {
if (store != null) {
store.close();
store = null;
}
} catch (Exception e) { // MessagingException, IOException
log.warning("Ignoring store close problem: " + e.toString());
}