Search in sources :

Example 1 with TextBody

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

the class RawMailParser method handleMimePart.

/**
   * Traverses a mime tree and parses out text and html parts. All other parts will be dropped.
   *
   * @param part MimePart to parse
   * @param textBuilder StringBuilder to append all plaintext parts
   * @param htmlBuilder StringBuilder to append all html parts
   * @throws IOException
   */
private static void handleMimePart(Entity part, StringBuilder textBuilder, StringBuilder htmlBuilder) throws IOException {
    if (isPlainOrHtml(part.getMimeType()) && !isAttachment(part.getDispositionType())) {
        TextBody tb = (TextBody) part.getBody();
        String result = CharStreams.toString(new InputStreamReader(tb.getInputStream(), tb.getMimeCharset()));
        if (part.getMimeType().equals("text/plain")) {
            textBuilder.append(result);
        } else if (part.getMimeType().equals("text/html")) {
            htmlBuilder.append(result);
        }
    } else if (isMixedOrAlternative(part.getMimeType())) {
        Multipart multipart = (Multipart) part.getBody();
        for (Entity e : multipart.getBodyParts()) {
            handleMimePart(e, textBuilder, htmlBuilder);
        }
    }
}
Also used : TextBody(org.apache.james.mime4j.dom.TextBody) Entity(org.apache.james.mime4j.dom.Entity) Multipart(org.apache.james.mime4j.dom.Multipart) InputStreamReader(java.io.InputStreamReader)

Example 2 with TextBody

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

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

the class MessageStoreImpl method getTextPart.

/**
     *	code taken from http://www.mozgoweb.com/posts/how-to-parse-mime-message-using-mime4j-library/
     */
static String getTextPart(Entity part) throws IOException {
    TextBody tb = (TextBody) part.getBody();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    tb.writeTo(baos);
    return baos.toString(MailArchiveServerConstants.DEFAULT_ENCODER.charset().name());
}
Also used : TextBody(org.apache.james.mime4j.dom.TextBody) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with TextBody

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

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

the class RawMailParser method handleMimePart.

/**
 * Traverses a mime tree and parses out text and html parts. All other parts will be dropped.
 *
 * @param part {@code MimePart} to parse
 * @param textBuilder {@link StringBuilder} to append all plaintext parts
 * @param htmlBuilder {@link StringBuilder} to append all html parts
 * @throws IOException in case of a failure while transforming the input to a {@link String}
 */
private static void handleMimePart(Entity part, StringBuilder textBuilder, StringBuilder htmlBuilder) throws IOException {
    if (isPlainOrHtml(part.getMimeType()) && !isAttachment(part.getDispositionType())) {
        TextBody tb = (TextBody) part.getBody();
        String result = CharStreams.toString(new InputStreamReader(tb.getInputStream(), tb.getMimeCharset()));
        if (part.getMimeType().equals("text/plain")) {
            textBuilder.append(result);
        } else if (part.getMimeType().equals("text/html")) {
            htmlBuilder.append(result);
        }
    } else if (isMultipart(part.getMimeType())) {
        Multipart multipart = (Multipart) part.getBody();
        for (Entity e : multipart.getBodyParts()) {
            handleMimePart(e, textBuilder, htmlBuilder);
        }
    }
}
Also used : TextBody(org.apache.james.mime4j.dom.TextBody) Entity(org.apache.james.mime4j.dom.Entity) Multipart(org.apache.james.mime4j.dom.Multipart) InputStreamReader(java.io.InputStreamReader)

Aggregations

TextBody (org.apache.james.mime4j.dom.TextBody)10 BinaryBody (org.apache.james.mime4j.dom.BinaryBody)6 Multipart (org.apache.james.mime4j.dom.Multipart)6 Entity (org.apache.james.mime4j.dom.Entity)5 IOException (java.io.IOException)4 Reader (java.io.Reader)3 BodyPart (org.apache.james.mime4j.message.BodyPart)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStreamReader (java.io.InputStreamReader)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 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