Search in sources :

Example 1 with MimeException

use of org.apache.james.mime4j.MimeException in project k-9 by k9mail.

the class MimePartStreamParser method parse.

public static MimeBodyPart parse(FileFactory fileFactory, InputStream inputStream) throws MessagingException, IOException {
    MimeBodyPart parsedRootPart = new MimeBodyPart();
    MimeConfig parserConfig = new MimeConfig();
    parserConfig.setMaxHeaderLen(-1);
    parserConfig.setMaxLineLen(-1);
    parserConfig.setMaxHeaderCount(-1);
    MimeStreamParser parser = new MimeStreamParser(parserConfig);
    parser.setContentHandler(new PartBuilder(fileFactory, parsedRootPart));
    parser.setRecurse();
    try {
        parser.parse(new EOLConvertingInputStream(inputStream));
    } catch (MimeException e) {
        throw new MessagingException("Failed to parse decrypted content", e);
    }
    return parsedRootPart;
}
Also used : MimeConfig(org.apache.james.mime4j.stream.MimeConfig) MessagingException(com.fsck.k9.mail.MessagingException) MimeStreamParser(org.apache.james.mime4j.parser.MimeStreamParser) MimeException(org.apache.james.mime4j.MimeException) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) EOLConvertingInputStream(org.apache.james.mime4j.io.EOLConvertingInputStream)

Example 2 with MimeException

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

the class RawMailParser method parse.

/**
   * Parses a MailMessage from a string.
   *
   * @param raw String as received over the wire
   * @return Parsed MailMessage
   * @throws MailParsingException
   */
public static MailMessage parse(String raw) throws MailParsingException {
    MailMessage.Builder messageBuilder = MailMessage.builder();
    messageBuilder.rawContentUTF(raw);
    Message mimeMessage;
    try {
        MessageBuilder builder = new DefaultMessageBuilder();
        mimeMessage = builder.parseMessage(new ByteArrayInputStream(raw.getBytes(UTF_8)));
    } catch (IOException | MimeException e) {
        throw new MailParsingException("Can't parse email", e);
    }
    // Add general headers
    if (mimeMessage.getMessageId() != null) {
        messageBuilder.id(mimeMessage.getMessageId());
    }
    if (mimeMessage.getSubject() != null) {
        messageBuilder.subject(mimeMessage.getSubject());
    }
    messageBuilder.dateReceived(new DateTime(mimeMessage.getDate()));
    // Add From, To and Cc
    if (mimeMessage.getFrom() != null && mimeMessage.getFrom().size() > 0) {
        Mailbox from = mimeMessage.getFrom().get(0);
        messageBuilder.from(new Address(from.getName(), from.getAddress()));
    }
    if (mimeMessage.getTo() != null) {
        for (Mailbox m : mimeMessage.getTo().flatten()) {
            messageBuilder.addTo(new Address(m.getName(), m.getAddress()));
        }
    }
    if (mimeMessage.getCc() != null) {
        for (Mailbox m : mimeMessage.getCc().flatten()) {
            messageBuilder.addCc(new Address(m.getName(), m.getAddress()));
        }
    }
    // Add additional headers
    mimeMessage.getHeader().getFields().stream().filter(f -> !MAIN_HEADERS.contains(f.getName().toLowerCase())).forEach(f -> messageBuilder.addAdditionalHeader(f.getName() + ": " + f.getBody()));
    // Add text and html body parts
    StringBuilder textBuilder = new StringBuilder();
    StringBuilder htmlBuilder = new StringBuilder();
    try {
        handleMimePart(mimeMessage, textBuilder, htmlBuilder);
    } catch (IOException e) {
        throw new MailParsingException("Can't parse email", e);
    }
    messageBuilder.textContent(Strings.emptyToNull(textBuilder.toString()));
    messageBuilder.htmlContent(Strings.emptyToNull(htmlBuilder.toString()));
    try {
        // required attributes are missing, so that the caller doesn't fall over.
        return messageBuilder.build();
    } catch (IllegalStateException e) {
        throw new MailParsingException("Missing required attributes after email was parsed", e);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Message(org.apache.james.mime4j.dom.Message) MimeException(org.apache.james.mime4j.MimeException) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) TextBody(org.apache.james.mime4j.dom.TextBody) Ints(com.google.common.primitives.Ints) InputStreamReader(java.io.InputStreamReader) Multipart(org.apache.james.mime4j.dom.Multipart) Strings(com.google.common.base.Strings) Address(com.google.gerrit.server.mail.Address) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) ImmutableList(com.google.common.collect.ImmutableList) ByteArrayInputStream(java.io.ByteArrayInputStream) CharStreams(com.google.common.io.CharStreams) Entity(org.apache.james.mime4j.dom.Entity) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Mailbox(org.apache.james.mime4j.dom.address.Mailbox) Message(org.apache.james.mime4j.dom.Message) Address(com.google.gerrit.server.mail.Address) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Mailbox(org.apache.james.mime4j.dom.address.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeException(org.apache.james.mime4j.MimeException)

Example 3 with MimeException

use of org.apache.james.mime4j.MimeException in project tika by apache.

the class MailContentHandler method body.

public void body(BodyDescriptor body, InputStream is) throws MimeException, IOException {
    // use a different metadata object
    // in order to specify the mime type of the
    // sub part without damaging the main metadata
    Metadata submd = new Metadata();
    submd.set(Metadata.CONTENT_TYPE, body.getMimeType());
    submd.set(Metadata.CONTENT_ENCODING, body.getCharset());
    try {
        if (extractor.shouldParseEmbedded(submd)) {
            // Wrap the InputStream before passing on, as the James provided
            //  one misses many features we might want eg mark/reset
            TikaInputStream tis = TikaInputStream.get(is);
            extractor.parseEmbedded(tis, handler, submd, false);
        }
    } catch (SAXException e) {
        throw new MimeException(e);
    }
}
Also used : Metadata(org.apache.tika.metadata.Metadata) TikaInputStream(org.apache.tika.io.TikaInputStream) MimeException(org.apache.james.mime4j.MimeException) SAXException(org.xml.sax.SAXException)

Example 4 with MimeException

use of org.apache.james.mime4j.MimeException in project tika by apache.

the class RFC822Parser method parse.

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    // Get the mime4j configuration, or use a default one
    MimeConfig config = new MimeConfig();
    config.setMaxLineLen(100000);
    // max length of any individual header
    config.setMaxHeaderLen(100000);
    config = context.get(MimeConfig.class, config);
    MimeStreamParser parser = new MimeStreamParser(config);
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    MailContentHandler mch = new MailContentHandler(xhtml, metadata, context, config.isStrictParsing());
    parser.setContentHandler(mch);
    parser.setContentDecoding(true);
    TikaInputStream tstream = TikaInputStream.get(stream);
    try {
        parser.parse(tstream);
    } catch (IOException e) {
        tstream.throwIfCauseOf(e);
        throw new TikaException("Failed to parse an email message", e);
    } catch (MimeException e) {
        // Unwrap the exception in case it was not thrown by mime4j
        Throwable cause = e.getCause();
        if (cause instanceof TikaException) {
            throw (TikaException) cause;
        } else if (cause instanceof SAXException) {
            throw (SAXException) cause;
        } else {
            throw new TikaException("Failed to parse an email message", e);
        }
    }
}
Also used : TikaException(org.apache.tika.exception.TikaException) MimeConfig(org.apache.james.mime4j.stream.MimeConfig) MimeStreamParser(org.apache.james.mime4j.parser.MimeStreamParser) TikaInputStream(org.apache.tika.io.TikaInputStream) MimeException(org.apache.james.mime4j.MimeException) IOException(java.io.IOException) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) SAXException(org.xml.sax.SAXException)

Example 5 with MimeException

use of org.apache.james.mime4j.MimeException in project tika by apache.

the class MailContentHandler method startBodyPart.

public void startBodyPart() throws MimeException {
    try {
        handler.startElement("div", "class", "email-entry");
        handler.startElement("p");
    } catch (SAXException e) {
        throw new MimeException(e);
    }
}
Also used : MimeException(org.apache.james.mime4j.MimeException) SAXException(org.xml.sax.SAXException)

Aggregations

MimeException (org.apache.james.mime4j.MimeException)14 IOException (java.io.IOException)8 MimeStreamParser (org.apache.james.mime4j.parser.MimeStreamParser)4 SAXException (org.xml.sax.SAXException)4 MessagingException (com.fsck.k9.mail.MessagingException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Entity (org.apache.james.mime4j.dom.Entity)3 Message (org.apache.james.mime4j.dom.Message)3 MessageBuilder (org.apache.james.mime4j.dom.MessageBuilder)3 TextBody (org.apache.james.mime4j.dom.TextBody)3 DefaultMessageBuilder (org.apache.james.mime4j.message.DefaultMessageBuilder)3 MimeConfig (org.apache.james.mime4j.stream.MimeConfig)3 ArrayList (java.util.ArrayList)2 BinaryBody (org.apache.james.mime4j.dom.BinaryBody)2 Body (org.apache.james.mime4j.dom.Body)2 Mailbox (org.apache.james.mime4j.dom.address.Mailbox)2 EOLConvertingInputStream (org.apache.james.mime4j.io.EOLConvertingInputStream)2 EntityState (org.apache.james.mime4j.stream.EntityState)2 TikaInputStream (org.apache.tika.io.TikaInputStream)2 StringBuilder (scala.collection.mutable.StringBuilder)2