Search in sources :

Example 1 with MimeStreamParser

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

the class MessageHeaderParser method getMimeStreamParser.

private static MimeStreamParser getMimeStreamParser() {
    MimeConfig parserConfig = new MimeConfig();
    parserConfig.setMaxHeaderLen(-1);
    parserConfig.setMaxLineLen(-1);
    parserConfig.setMaxHeaderCount(-1);
    return new MimeStreamParser(parserConfig);
}
Also used : MimeConfig(org.apache.james.mime4j.stream.MimeConfig) MimeStreamParser(org.apache.james.mime4j.parser.MimeStreamParser)

Example 2 with MimeStreamParser

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

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

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

the class MessageHeaderParser method parse.

public static void parse(final Part part, InputStream headerInputStream) throws MessagingException {
    MimeStreamParser parser = getMimeStreamParser();
    parser.setContentHandler(new MessageHeaderParserContentHandler(part));
    try {
        parser.parse(headerInputStream);
    } catch (MimeException me) {
        throw new MessagingException("Error parsing headers", me);
    } catch (IOException e) {
        throw new MessagingException("I/O error parsing headers", e);
    }
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) MimeStreamParser(org.apache.james.mime4j.parser.MimeStreamParser) MimeException(org.apache.james.mime4j.MimeException) IOException(java.io.IOException)

Example 5 with MimeStreamParser

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

the class MimeMessage method parse.

private void parse(InputStream in, boolean recurse) throws IOException, MessagingException {
    mHeader.clear();
    mFrom = null;
    mTo = null;
    mCc = null;
    mBcc = null;
    mReplyTo = null;
    mMessageId = null;
    mReferences = null;
    mInReplyTo = null;
    mSentDate = null;
    mBody = null;
    MimeConfig parserConfig = new MimeConfig();
    // The default is a mere 10k
    parserConfig.setMaxHeaderLen(-1);
    // The default is 1000 characters. Some MUAs generate
    parserConfig.setMaxLineLen(-1);
    // REALLY long References: headers
    // Disable the check for header count.
    parserConfig.setMaxHeaderCount(-1);
    MimeStreamParser parser = new MimeStreamParser(parserConfig);
    parser.setContentHandler(new MimeMessageBuilder());
    if (recurse) {
        parser.setRecurse();
    }
    try {
        parser.parse(new EOLConvertingInputStream(in));
    } catch (MimeException me) {
        throw new MessagingException(me.getMessage(), me);
    }
}
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) EOLConvertingInputStream(org.apache.james.mime4j.io.EOLConvertingInputStream)

Aggregations

MimeStreamParser (org.apache.james.mime4j.parser.MimeStreamParser)5 MimeException (org.apache.james.mime4j.MimeException)4 MimeConfig (org.apache.james.mime4j.stream.MimeConfig)4 MessagingException (com.fsck.k9.mail.MessagingException)3 IOException (java.io.IOException)2 EOLConvertingInputStream (org.apache.james.mime4j.io.EOLConvertingInputStream)2 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)1 TikaException (org.apache.tika.exception.TikaException)1 TikaInputStream (org.apache.tika.io.TikaInputStream)1 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)1 SAXException (org.xml.sax.SAXException)1