use of org.apache.james.mime4j.message.DefaultMessageWriter in project sling by apache.
the class MessageStoreImpl method save.
private void save(ResourceResolver resolver, Message msg) throws IOException, LoginException {
// apply message processors
for (MessageProcessor processor : getSortedMessageProcessors()) {
logger.debug("Calling {}", processor);
processor.processMessage(msg);
}
// into path: archive/domain/list/thread/message
final Map<String, Object> msgProps = new HashMap<String, Object>();
final List<BodyPart> attachments = new ArrayList<BodyPart>();
msgProps.put(resourceTypeKey, MailArchiveServerConstants.MESSAGE_RT);
StringBuilder plainBody = new StringBuilder();
StringBuilder htmlBody = new StringBuilder();
Boolean hasBody = false;
if (!msg.isMultipart()) {
plainBody = new StringBuilder(getTextPart(msg));
} else {
Multipart multipart = (Multipart) msg.getBody();
recursiveMultipartProcessing(multipart, plainBody, htmlBody, hasBody, attachments);
}
msgProps.put(PLAIN_BODY, plainBody.toString().replaceAll("\r\n", "\n"));
if (htmlBody.length() > 0) {
msgProps.put(HTML_BODY, htmlBody.toString());
}
msgProps.putAll(getMessagePropertiesFromHeader(msg.getHeader()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new DefaultMessageWriter().writeHeader(msg.getHeader(), baos);
String origHdr = baos.toString(MailArchiveServerConstants.DEFAULT_ENCODER.charset().name());
msgProps.put(X_ORIGINAL_HEADER, origHdr);
final Header hdr = msg.getHeader();
final String listIdRaw = hdr.getField(LIST_ID).getBody();
// remove < and >
final String listId = listIdRaw.substring(1, listIdRaw.length() - 1);
final String list = getListNodeName(listId);
final String domain = getDomainNodeName(listId);
final String subject = (String) msgProps.get(SUBJECT);
final String threadPath = threadKeyGen.getThreadKey(subject);
final String threadName = removeRe(subject);
Resource parentResource = assertEachNode(resolver, archivePath, domain, list, threadPath, threadName);
String msgNodeName = makeJcrFriendly((String) msgProps.get(NAME));
boolean isMsgNodeCreated = assertResource(resolver, parentResource, msgNodeName, msgProps);
if (isMsgNodeCreated) {
Resource msgResource = resolver.getResource(parentResource, msgNodeName);
for (BodyPart att : attachments) {
if (!attachmentFilter.isEligible(att)) {
continue;
}
final Map<String, Object> attProps = new HashMap<String, Object>();
parseHeaderToProps(att.getHeader(), attProps);
Body body = att.getBody();
if (body instanceof BinaryBody) {
attProps.put(CONTENT, ((BinaryBody) body).getInputStream());
} else if (body instanceof TextBody) {
attProps.put(CONTENT, ((TextBody) body).getInputStream());
}
String attNodeName = Text.escapeIllegalJcrChars(att.getFilename());
assertResource(resolver, msgResource, attNodeName, attProps);
}
updateThread(resolver, parentResource, msgProps);
}
}
Aggregations