Search in sources :

Example 1 with Entity

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

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

the class MimeToHtmlAdapter method getContent.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.kyj.fx.voeditor.visual.framework.word.AbstractMimeAdapter#getContent(
	 * )
	 */
@Override
public String getContent() {
    StringBuilder sb = new StringBuilder();
    try {
        final MessageBuilder builder = new DefaultMessageBuilder();
        Message message = builder.parseMessage(new ByteArrayInputStream(this.content));
        Body body = ((Entity) message).getBody();
        append(sb, body);
    } catch (MimeException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) StringBuilder(scala.collection.mutable.StringBuilder) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Message(org.apache.james.mime4j.dom.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) MimeException(org.apache.james.mime4j.MimeException) IOException(java.io.IOException) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) Body(org.apache.james.mime4j.dom.Body) TextBody(org.apache.james.mime4j.dom.TextBody)

Example 3 with Entity

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

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

the class MimeToHtmlWordAdapter method getContent.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.kyj.fx.voeditor.visual.framework.word.AbstractMimeAdapter#getContent(
	 * )
	 */
@Override
public String getContent() {
    StringBuilder sb = new StringBuilder();
    try {
        final MessageBuilder builder = new DefaultMessageBuilder();
        Message message = builder.parseMessage(new ByteArrayInputStream(this.content));
        Body body = ((Entity) message).getBody();
        append(sb, body);
    } catch (MimeException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) StringBuilder(scala.collection.mutable.StringBuilder) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Message(org.apache.james.mime4j.dom.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) MimeException(org.apache.james.mime4j.MimeException) IOException(java.io.IOException) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) Body(org.apache.james.mime4j.dom.Body) TextBody(org.apache.james.mime4j.dom.TextBody)

Example 5 with Entity

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

Aggregations

Entity (org.apache.james.mime4j.dom.Entity)8 TextBody (org.apache.james.mime4j.dom.TextBody)7 BinaryBody (org.apache.james.mime4j.dom.BinaryBody)6 IOException (java.io.IOException)5 Multipart (org.apache.james.mime4j.dom.Multipart)4 Reader (java.io.Reader)3 Body (org.apache.james.mime4j.dom.Body)3 Message (org.apache.james.mime4j.dom.Message)3 MessageBuilder (org.apache.james.mime4j.dom.MessageBuilder)3 DefaultMessageBuilder (org.apache.james.mime4j.message.DefaultMessageBuilder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 MimeException (org.apache.james.mime4j.MimeException)2 StringBuilder (scala.collection.mutable.StringBuilder)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 Scene (javafx.scene.Scene)1 BorderPane (javafx.scene.layout.BorderPane)1 WebEngine (javafx.scene.web.WebEngine)1 WebView (javafx.scene.web.WebView)1