Search in sources :

Example 1 with BodyPart

use of org.apache.james.mime4j.message.BodyPart 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)

Example 2 with BodyPart

use of org.apache.james.mime4j.message.BodyPart in project sling by apache.

the class MessageStoreImpl method recursiveMultipartProcessing.

static void recursiveMultipartProcessing(Multipart multipart, StringBuilder plainBody, StringBuilder htmlBody, Boolean hasBody, List<BodyPart> attachments) throws IOException {
    for (Entity enitiy : multipart.getBodyParts()) {
        BodyPart part = (BodyPart) enitiy;
        if (part.getDispositionType() != null && !part.getDispositionType().equals("")) {
            // if DispositionType is null or empty, it means that it's multipart, not attached file
            attachments.add(part);
        } else {
            if (part.isMimeType(PLAIN_MIMETYPE) && !hasBody) {
                plainBody.append(getTextPart(part));
                hasBody = true;
            } else if (part.isMimeType(HTML_MIMETYPE) && !hasBody) {
                htmlBody.append(getTextPart(part));
            } else if (part.isMultipart()) {
                recursiveMultipartProcessing((Multipart) part.getBody(), plainBody, htmlBody, hasBody, attachments);
            }
        }
    }
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) BodyPart(org.apache.james.mime4j.message.BodyPart)

Example 3 with BodyPart

use of org.apache.james.mime4j.message.BodyPart in project sling by apache.

the class MessageStoreImplAttachmentsTest method recursiveMultipartMessageTest.

@Test
public void recursiveMultipartMessageTest() throws IOException {
    Multipart multipart = new MultipartImpl("mixed");
    BodyPart att1 = createRandomBinaryAttachment(100);
    multipart.addBodyPart(att1);
    BodyPart att2 = createRandomBinaryAttachment(133);
    multipart.addBodyPart(att2);
    Multipart nestedMultipart = new MultipartImpl("mixed");
    BodyPart nBody = createTextBody("Some sample text here...?!", "plain", false);
    nestedMultipart.addBodyPart(nBody);
    BodyPart nAtt1 = createRandomBinaryAttachment(300);
    nestedMultipart.addBodyPart(nAtt1);
    BodyPart NAtt2 = createRandomBinaryAttachment(100);
    nestedMultipart.addBodyPart(NAtt2);
    BodyPart nAtt3 = createTextBody("Some other text here...<br>?!", "html", true);
    nestedMultipart.addBodyPart(nAtt3);
    BodyPart nestedMessage = new BodyPart();
    nestedMessage.setMultipart(nestedMultipart);
    multipart.addBodyPart(nestedMessage);
    MessageImpl message = new MessageImpl();
    message.setMultipart(multipart);
    message.setSubject("Template message");
    message.setDate(new Date());
    message.getHeader().setField(new RawField(LIST_ID, "<list.example.com>"));
    assertSaveMessageWithAttachments(message, 5);
}
Also used : BodyPart(org.apache.james.mime4j.message.BodyPart) Multipart(org.apache.james.mime4j.dom.Multipart) MultipartImpl(org.apache.james.mime4j.message.MultipartImpl) RawField(org.apache.james.mime4j.stream.RawField) MessageImpl(org.apache.james.mime4j.message.MessageImpl) Date(java.util.Date) Test(org.junit.Test)

Example 4 with BodyPart

use of org.apache.james.mime4j.message.BodyPart in project sling by apache.

the class MessageStoreImplAttachmentsTest method simpleMultipartMessageTest.

@Test
public void simpleMultipartMessageTest() throws IOException {
    Multipart multipart = new MultipartImpl("mixed");
    BodyPart att0 = createTextBody("This is the first part of the template..", "plain", true);
    multipart.addBodyPart(att0);
    BodyPart att1 = createRandomBinaryAttachment(200);
    multipart.addBodyPart(att1);
    BodyPart att2 = createRandomBinaryAttachment(300);
    multipart.addBodyPart(att2);
    BodyPart att3 = createTextBody("Some sample text here...?!", "html", true);
    multipart.addBodyPart(att3);
    BodyPart att4 = createRandomBinaryAttachment(100);
    multipart.addBodyPart(att4);
    BodyPart att5 = createTextBody("Some other text here...?!", "plain", true);
    multipart.addBodyPart(att5);
    MessageImpl message = new MessageImpl();
    message.setMultipart(multipart);
    message.setSubject("Template message");
    message.setDate(new Date());
    message.getHeader().setField(new RawField(LIST_ID, "<list.example.com>"));
    assertSaveMessageWithAttachments(message, 6);
}
Also used : BodyPart(org.apache.james.mime4j.message.BodyPart) Multipart(org.apache.james.mime4j.dom.Multipart) MultipartImpl(org.apache.james.mime4j.message.MultipartImpl) RawField(org.apache.james.mime4j.stream.RawField) MessageImpl(org.apache.james.mime4j.message.MessageImpl) Date(java.util.Date) Test(org.junit.Test)

Example 5 with BodyPart

use of org.apache.james.mime4j.message.BodyPart in project sling by apache.

the class MessageStoreImplAttachmentsTest method assertSaveMessageWithAttachments.

private void assertSaveMessageWithAttachments(Message msg, int num) throws IOException {
    store.save(msg);
    List<BodyPart> attList = new LinkedList<BodyPart>();
    MessageStoreImpl.recursiveMultipartProcessing((Multipart) msg.getBody(), new StringBuilder(), new StringBuilder(), false, attList);
    @SuppressWarnings("unchecked") Queue<BodyPart> attachmentsMsg = (Queue<BodyPart>) attList;
    assertTrue("No attachments found", attachmentsMsg.size() > 0);
    assertEquals("", num, attachmentsMsg.size());
    final Resource r = resolver.getResource(getResourcePath(msg, store));
    assertNotNull("Expecting non-null Resource", r);
    for (Resource aRes : r.getChildren()) {
        final ModifiableValueMap aMap = aRes.adaptTo(ModifiableValueMap.class);
        BodyPart aMsg = attachmentsMsg.poll();
        assertNotNull("JCR contains more attachments", aMsg);
        for (Field f : aMsg.getHeader().getFields()) {
            String name = f.getName();
            assertEquals("Field " + name + " is different", (aMap.get(name, String.class)), f.getBody());
        }
        if (aMsg.getBody() instanceof TextBody) {
            assertEquals("Content is not the same", MessageStoreImpl.getTextPart(aMsg), aMap.get(CONTENT, String.class));
        } else if (aMsg.getBody() instanceof BinaryBody) {
            assertEquals("Content is not the same", getBinPart(aMsg), aMap.get(CONTENT, String.class));
        } else {
            fail("Unknown type of attachment body");
        }
    }
    assertEquals("Message contains more attachments", attachmentsMsg.poll(), null);
}
Also used : BodyPart(org.apache.james.mime4j.message.BodyPart) Resource(org.apache.sling.api.resource.Resource) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) LinkedList(java.util.LinkedList) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) TextBody(org.apache.james.mime4j.dom.TextBody) RawField(org.apache.james.mime4j.stream.RawField) Field(org.apache.james.mime4j.stream.Field) Queue(java.util.Queue)

Aggregations

BodyPart (org.apache.james.mime4j.message.BodyPart)7 TextBody (org.apache.james.mime4j.dom.TextBody)4 BinaryBody (org.apache.james.mime4j.dom.BinaryBody)3 Multipart (org.apache.james.mime4j.dom.Multipart)3 RawField (org.apache.james.mime4j.stream.RawField)3 Date (java.util.Date)2 Body (org.apache.james.mime4j.dom.Body)2 MessageImpl (org.apache.james.mime4j.message.MessageImpl)2 MultipartImpl (org.apache.james.mime4j.message.MultipartImpl)2 StorageBodyFactory (org.apache.james.mime4j.storage.StorageBodyFactory)2 Resource (org.apache.sling.api.resource.Resource)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Queue (java.util.Queue)1 Random (java.util.Random)1 Entity (org.apache.james.mime4j.dom.Entity)1