Search in sources :

Example 6 with TextBody

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

Example 7 with TextBody

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

the class MessageStoreImplAttachmentsTest method createTextBody.

/*
     * taken from http://svn.apache.org/repos/asf/james/mime4j/trunk/examples/src/main/java/org/apache/james/mime4j/samples/transform/TransformMessage.java
     */
private static BodyPart createTextBody(String text, String subtype, boolean isAttachment) {
    TextBody body = new StorageBodyFactory().textBody(text, MailArchiveServerConstants.DEFAULT_ENCODER.charset().name());
    BodyPart bodyPart = new BodyPart();
    if (isAttachment) {
        bodyPart.setContentDisposition("attachment", "file" + Math.random());
    }
    bodyPart.setText(body, subtype);
    return bodyPart;
}
Also used : TextBody(org.apache.james.mime4j.dom.TextBody) StorageBodyFactory(org.apache.james.mime4j.storage.StorageBodyFactory) BodyPart(org.apache.james.mime4j.message.BodyPart)

Example 8 with TextBody

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

the class MimeToHtmlWordAdapter 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
		 * 
		 */
    {
        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 9 with TextBody

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

Aggregations

TextBody (org.apache.james.mime4j.dom.TextBody)9 BinaryBody (org.apache.james.mime4j.dom.BinaryBody)6 Multipart (org.apache.james.mime4j.dom.Multipart)5 IOException (java.io.IOException)4 Entity (org.apache.james.mime4j.dom.Entity)4 Reader (java.io.Reader)3 BodyPart (org.apache.james.mime4j.message.BodyPart)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Body (org.apache.james.mime4j.dom.Body)2 Field (org.apache.james.mime4j.stream.Field)2 Resource (org.apache.sling.api.resource.Resource)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Queue (java.util.Queue)1 Header (org.apache.james.mime4j.dom.Header)1 DefaultMessageWriter (org.apache.james.mime4j.message.DefaultMessageWriter)1 MessageImpl (org.apache.james.mime4j.message.MessageImpl)1