Search in sources :

Example 6 with BinaryBody

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

the class MessageStoreImplAttachmentsTest method createRandomBinaryAttachment.

/*
     * 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 createRandomBinaryAttachment(int numberOfBytes) throws IOException {
    byte[] data = new byte[numberOfBytes];
    new Random().nextBytes(data);
    Body body = new StorageBodyFactory().binaryBody(new ByteArrayInputStream(data));
    BodyPart bodyPart = new BodyPart();
    bodyPart.setContentDisposition("attachment", "file" + Math.random());
    bodyPart.setBody(body, "application/octet-stream");
    return bodyPart;
}
Also used : StorageBodyFactory(org.apache.james.mime4j.storage.StorageBodyFactory) BodyPart(org.apache.james.mime4j.message.BodyPart) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) Body(org.apache.james.mime4j.dom.Body) TextBody(org.apache.james.mime4j.dom.TextBody) BinaryBody(org.apache.james.mime4j.dom.BinaryBody)

Example 7 with BinaryBody

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

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