Search in sources :

Example 1 with Header

use of org.apache.james.mime4j.dom.Header in project sling by apache.

the class MessageStoreImplRepositoryTestUtil method getResourcePath.

static String getResourcePath(Message msg, MessageStoreImpl store) {
    final Header hdr = msg.getHeader();
    final String listIdRaw = hdr.getField("List-Id").getBody();
    // remove < and >
    final String listId = listIdRaw.substring(1, listIdRaw.length() - 1);
    String msgId;
    final Field msgIdField = hdr.getField("Message-ID");
    if (msgIdField != null) {
        msgId = msgIdField.getBody();
        msgId = msgId.substring(1, msgId.length() - 1);
    } else {
        msgId = Integer.toHexString(hdr.getField("Date").hashCode());
    }
    msgId = makeJcrFriendly(msgId);
    String subject = null;
    final Field subjectField = hdr.getField("Subject");
    if (subjectField != null) {
        subject = subjectField.getBody();
    }
    String threadPath = store.threadKeyGen.getThreadKey(subject);
    String path = store.archivePath + getDomainNodeName(listId) + "/" + getListNodeName(listId) + "/" + threadPath + "/" + msgId;
    return path;
}
Also used : Field(org.apache.james.mime4j.stream.Field) Header(org.apache.james.mime4j.dom.Header)

Example 2 with Header

use of org.apache.james.mime4j.dom.Header 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);
    }
}
Also used : BodyPart(org.apache.james.mime4j.message.BodyPart) Multipart(org.apache.james.mime4j.dom.Multipart) HashMap(java.util.HashMap) MessageProcessor(org.apache.sling.mailarchiveserver.api.MessageProcessor) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) DefaultMessageWriter(org.apache.james.mime4j.message.DefaultMessageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) TextBody(org.apache.james.mime4j.dom.TextBody) Header(org.apache.james.mime4j.dom.Header) Body(org.apache.james.mime4j.dom.Body) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) TextBody(org.apache.james.mime4j.dom.TextBody)

Aggregations

Header (org.apache.james.mime4j.dom.Header)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 BinaryBody (org.apache.james.mime4j.dom.BinaryBody)1 Body (org.apache.james.mime4j.dom.Body)1 Multipart (org.apache.james.mime4j.dom.Multipart)1 TextBody (org.apache.james.mime4j.dom.TextBody)1 BodyPart (org.apache.james.mime4j.message.BodyPart)1 DefaultMessageWriter (org.apache.james.mime4j.message.DefaultMessageWriter)1 Field (org.apache.james.mime4j.stream.Field)1 Resource (org.apache.sling.api.resource.Resource)1 MessageProcessor (org.apache.sling.mailarchiveserver.api.MessageProcessor)1