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);
}
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);
}
}
}
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;
}
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));
}
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);
}
}
Aggregations