Search in sources :

Example 1 with MimeConfig

use of org.apache.james.mime4j.stream.MimeConfig 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 MimeConfig

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

use of org.apache.james.mime4j.stream.MimeConfig 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.Builder().setMaxHeaderLen(-1).setMaxLineLen(-1).setMaxHeaderCount(-1).build();
    MimeStreamParser parser = new MimeStreamParser(parserConfig);
    parser.setContentHandler(new PartBuilder(parser, 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 4 with MimeConfig

use of org.apache.james.mime4j.stream.MimeConfig in project tika by apache.

the class RFC822ParserTest method testLongHeader.

/**
     * Test for TIKA-640, increase header max beyond 10k bytes
     */
@Test
public void testLongHeader() throws Exception {
    StringBuilder inputBuilder = new StringBuilder();
    for (int i = 0; i < 2000; ++i) {
        //len > 50
        inputBuilder.append("really really really really really really long name ");
    }
    String name = inputBuilder.toString();
    byte[] data = ("From: " + name + "\r\n\r\n").getBytes(US_ASCII);
    Parser parser = new RFC822Parser();
    ContentHandler handler = new DefaultHandler();
    Metadata metadata = new Metadata();
    ParseContext context = new ParseContext();
    try {
        parser.parse(new ByteArrayInputStream(data), handler, metadata, context);
        fail();
    } catch (TikaException expected) {
    }
    MimeConfig config = new MimeConfig();
    config.setMaxHeaderLen(-1);
    config.setMaxLineLen(-1);
    context.set(MimeConfig.class, config);
    parser.parse(new ByteArrayInputStream(data), handler, metadata, context);
    assertEquals(name.trim(), metadata.get(TikaCoreProperties.CREATOR));
}
Also used : TikaException(org.apache.tika.exception.TikaException) Metadata(org.apache.tika.metadata.Metadata) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) Parser(org.apache.tika.parser.Parser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) DefaultHandler(org.xml.sax.helpers.DefaultHandler) MimeConfig(org.apache.james.mime4j.stream.MimeConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseContext(org.apache.tika.parser.ParseContext) TesseractOCRParserTest(org.apache.tika.parser.ocr.TesseractOCRParserTest) Test(org.junit.Test) TikaTest(org.apache.tika.TikaTest)

Example 5 with MimeConfig

use of org.apache.james.mime4j.stream.MimeConfig 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;
    xOriginalTo = null;
    deliveredTo = null;
    xEnvelopeTo = null;
    mMessageId = null;
    mReferences = null;
    mInReplyTo = null;
    mSentDate = null;
    mBody = null;
    MimeConfig parserConfig = new MimeConfig.Builder().setMaxHeaderLen(-1).setMaxLineLen(-1).setMaxHeaderCount(-1).build();
    MimeStreamParser parser = new MimeStreamParser(parserConfig);
    parser.setContentHandler(new MimeMessageBuilder(new DefaultBodyFactory()));
    if (recurse) {
        parser.setRecurse();
    }
    try {
        parser.parse(new EOLConvertingInputStream(in));
    } catch (MimeException me) {
        throw new MessagingException(me.getMessage(), me);
    }
}
Also used : DefaultBodyFactory(com.fsck.k9.mail.DefaultBodyFactory) 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

MimeConfig (org.apache.james.mime4j.stream.MimeConfig)5 MimeStreamParser (org.apache.james.mime4j.parser.MimeStreamParser)4 MimeException (org.apache.james.mime4j.MimeException)3 MessagingException (com.fsck.k9.mail.MessagingException)2 EOLConvertingInputStream (org.apache.james.mime4j.io.EOLConvertingInputStream)2 TikaException (org.apache.tika.exception.TikaException)2 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)2 DefaultBodyFactory (com.fsck.k9.mail.DefaultBodyFactory)1 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 TikaTest (org.apache.tika.TikaTest)1 TikaInputStream (org.apache.tika.io.TikaInputStream)1 Metadata (org.apache.tika.metadata.Metadata)1 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)1 ParseContext (org.apache.tika.parser.ParseContext)1 Parser (org.apache.tika.parser.Parser)1 TesseractOCRParserTest (org.apache.tika.parser.ocr.TesseractOCRParserTest)1 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)1 Test (org.junit.Test)1