Search in sources :

Example 1 with BinaryBody

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

the class AttachmentFilterImpl method isEligible.

@Override
public boolean isEligible(BodyPart attachment) {
    // extension check
    final String filename = attachment.getFilename();
    String ext = "";
    int idx = filename.lastIndexOf('.');
    if (idx > -1) {
        ext = filename.substring(idx + 1);
    }
    if (eligibleExtensions != null && !eligibleExtensions.contains(ext)) {
        return false;
    }
    // size check
    final Body body = attachment.getBody();
    try {
        if (body instanceof BinaryBody && IOUtils.toByteArray(((BinaryBody) body).getInputStream()).length > maxSize || body instanceof TextBody && IOUtils.toByteArray(((TextBody) body).getInputStream()).length > maxSize) {
            return false;
        }
    } catch (IOException e) {
        return false;
    }
    // true, if nothing wrong
    return true;
}
Also used : TextBody(org.apache.james.mime4j.dom.TextBody) IOException(java.io.IOException) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) Body(org.apache.james.mime4j.dom.Body) TextBody(org.apache.james.mime4j.dom.TextBody)

Example 2 with BinaryBody

use of org.apache.james.mime4j.dom.BinaryBody in project Gargoyle by callakrsos.

the class MimeToHtmlAdapter method append.

/***************************************************************************/
/*  sb에 번역된 텍스트데이터를 덧붙임 */
/***************************************************************************/
private void append(StringBuilder sb, Body body) {
    if (body instanceof TextBody) {
        /*
			 * A text body. Display its contents.
			 */
        TextBody textBody = (TextBody) body;
        try {
            Reader r = textBody.getReader();
            int c;
            while ((c = r.read()) != -1) {
                sb.append((char) c);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    } else if (body instanceof BinaryBody) {
        BinaryBody bBody = (BinaryBody) body;
        append(sb, bBody);
    } else if (body instanceof Multipart) {
        Multipart mbody = (Multipart) body;
        for (Entity part : mbody.getBodyParts()) {
            append(sb, part);
        }
    } else /*
		 * Ignore Fields </br>
		 * 
		 * ContentTypeField,AddressListField,DateTimeField UnstructuredField,
		 * Field
		 * 
		 */
    {
        LOGGER.debug("{}", body);
        sb.append(body.toString());
    }
}
Also used : TextBody(org.apache.james.mime4j.dom.TextBody) Entity(org.apache.james.mime4j.dom.Entity) Multipart(org.apache.james.mime4j.dom.Multipart) Reader(java.io.Reader) IOException(java.io.IOException) BinaryBody(org.apache.james.mime4j.dom.BinaryBody)

Example 3 with BinaryBody

use of org.apache.james.mime4j.dom.BinaryBody in project Gargoyle by callakrsos.

the class MimeViewerExam method extracted.

private void extracted(StringBuilder sb, Body body) {
    if (body instanceof Multipart) {
        Multipart mbody = (Multipart) body;
        for (Entity part : mbody.getBodyParts()) {
            extracted(sb, part);
        }
    } else if (body instanceof MessageImpl) {
        extracted(sb, body);
    } else if (body instanceof TextBody) {
        /*
			 * A text body. Display its contents.
			 */
        TextBody textBody = (TextBody) body;
        try {
            Reader r = textBody.getReader();
            StringBuilder _sb = new StringBuilder();
            int c;
            while ((c = r.read()) != -1) {
                _sb.append((char) c);
            }
            System.out.println(_sb.toString());
            sb.append(_sb.toString());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    } else if (body instanceof BinaryBody) {
        BinaryBody bBody = (BinaryBody) body;
        Entity parent = bBody.getParent();
        //			String dispositionType = parent.getDispositionType();
        //			String filename = parent.getFilename();
        //			String contentTransferEncoding = parent.getContentTransferEncoding();
        //			String mimeType = parent.getMimeType();
        Field field = parent.getHeader().getField("Content-ID");
        String body2 = field.getBody();
        String contentId = body2.replace("<", "").replace(">", "");
        StringBuffer buf = new StringBuffer();
        try (InputStream is = bBody.getInputStream()) {
            int read = -1;
            while ((read = is.read()) != -1) {
                buf.append((char) read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        meta.put(contentId, buf.toString());
    } else /*
		 * Ignore Fields </br>
		 * 
		 * ContentTypeField,AddressListField,DateTimeField UnstructuredField,
		 * Field
		 * 
		 */
    {
        sb.append(body.toString());
    }
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) Multipart(org.apache.james.mime4j.dom.Multipart) InputStream(java.io.InputStream) Reader(java.io.Reader) IOException(java.io.IOException) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) TextBody(org.apache.james.mime4j.dom.TextBody) Field(org.apache.james.mime4j.stream.Field) MessageImpl(org.apache.james.mime4j.message.MessageImpl)

Example 4 with BinaryBody

use of org.apache.james.mime4j.dom.BinaryBody 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 5 with BinaryBody

use of org.apache.james.mime4j.dom.BinaryBody 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

BinaryBody (org.apache.james.mime4j.dom.BinaryBody)8 TextBody (org.apache.james.mime4j.dom.TextBody)7 IOException (java.io.IOException)4 Multipart (org.apache.james.mime4j.dom.Multipart)4 Reader (java.io.Reader)3 Body (org.apache.james.mime4j.dom.Body)3 Entity (org.apache.james.mime4j.dom.Entity)3 BodyPart (org.apache.james.mime4j.message.BodyPart)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Field (org.apache.james.mime4j.stream.Field)2 Resource (org.apache.sling.api.resource.Resource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)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 Header (org.apache.james.mime4j.dom.Header)1 DefaultMessageWriter (org.apache.james.mime4j.message.DefaultMessageWriter)1