SearchMessagesThread smt = new SearchMessagesThread(session, folder, messageIDTerm);
smt.start();
Message[] message = smt.finishUp();
for (Message msg : message) {
RepositoryDocument rd = new RepositoryDocument();
Date setDate = msg.getSentDate();
rd.setFileName(msg.getFileName());
is = msg.getInputStream();
rd.setBinary(is, msg.getSize());
String subject = StringUtils.EMPTY;
for (String metadata : requiredMetadata) {
if (metadata.toLowerCase().equals(EmailConfig.EMAIL_TO)) {
Address[] to = msg.getRecipients(Message.RecipientType.TO);
String[] toStr = new String[to.length];
int j = 0;
for (Address address : to) {
toStr[j] = address.toString();
}
rd.addField(EmailConfig.EMAIL_TO, toStr);
} else if (metadata.toLowerCase().equals(EmailConfig.EMAIL_FROM)) {
Address[] from = msg.getFrom();
String[] fromStr = new String[from.length];
int j = 0;
for (Address address : from) {
fromStr[j] = address.toString();
}
rd.addField(EmailConfig.EMAIL_TO, fromStr);
} else if (metadata.toLowerCase().equals(EmailConfig.EMAIL_SUBJECT)) {
subject = msg.getSubject();
rd.addField(EmailConfig.EMAIL_SUBJECT, subject);
} else if (metadata.toLowerCase().equals(EmailConfig.EMAIL_BODY)) {
Multipart mp = (Multipart) msg.getContent();
for (int k = 0, n = mp.getCount(); k < n; k++) {
Part part = mp.getBodyPart(k);
String disposition = part.getDisposition();
if ((disposition == null)) {
MimeBodyPart mbp = (MimeBodyPart) part;
if (mbp.isMimeType(EmailConfig.MIMETYPE_TEXT_PLAIN)) {
rd.addField(EmailConfig.EMAIL_BODY, mbp.getContent().toString());
} else if (mbp.isMimeType(EmailConfig.MIMETYPE_HTML)) {
rd.addField(EmailConfig.EMAIL_BODY, mbp.getContent().toString()); //handle html accordingly. Returns content with html tags
}
}
}
} else if (metadata.toLowerCase().equals(EmailConfig.EMAIL_DATE)) {
Date sentDate = msg.getSentDate();
rd.addField(EmailConfig.EMAIL_DATE, sentDate.toString());
} else if (metadata.toLowerCase().equals(EmailConfig.EMAIL_ATTACHMENT_ENCODING)) {
Multipart mp = (Multipart) msg.getContent();
if (mp != null) {
String[] encoding = new String[mp.getCount()];
for (int k = 0, n = mp.getCount(); k < n; k++) {
Part part = mp.getBodyPart(k);
String disposition = part.getDisposition();
if ((disposition != null) &&
((disposition.equals(Part.ATTACHMENT) ||
(disposition.equals(Part.INLINE))))) {
encoding[k] = part.getFileName().split("\\?")[1];
}
}
rd.addField(EmailConfig.ENCODING_FIELD, encoding);
}
} else if (metadata.toLowerCase().equals(EmailConfig.EMAIL_ATTACHMENT_MIMETYPE)) {
Multipart mp = (Multipart) msg.getContent();
String[] MIMEType = new String[mp.getCount()];
for (int k = 0, n = mp.getCount(); k < n; k++) {
Part part = mp.getBodyPart(k);
String disposition = part.getDisposition();
if ((disposition != null) &&
((disposition.equals(Part.ATTACHMENT) ||
(disposition.equals(Part.INLINE))))) {
MIMEType[k] = part.getContentType();
}
}
rd.addField(EmailConfig.MIMETYPE_FIELD, MIMEType);
}
}
String documentURI = makeDocumentURI(urlTemplate, folderName, id);
activities.ingestDocumentWithException(id, version, documentURI, rd);