I am working with MIME4J to read MIME events from an email stack dump. I am attempting to read a given message event, as defined by the START_MESSAGE and END_MESSAGE headers, as an entire event, as I will be moving the process to a distributed filesystem eventually and need to plan for file-split boundary traversing.
For the event-based parsing in mime4j, a ContentHandler interface is required, and methods are called from it by the parser, which requires the handler be set to it. I have experimented with a sample Handler from another SO answer that extends the mime4j packaged SimpleContentHandler, but that one really only parses headers.
I am trying to build my custom ContentHandler class to gather the complete message as one event. I would then need to have the event in a temporary object so I could parse headers, their fields, and the contents of the fields out of it. The end goal is to adapt this behavior into MapReduce, so coping with the chance that one part of the email will be on one filesplit, and another part in a different filesplit is necessary.
For my custom ContentHandler, I've gotten as far as:
public class CustomContentHandler extends AbstractContentHandler {}
And for a main, I am using:
public class Reader
{
public static void main( String[] args ) throws FileNotFoundException, IOException,
MimeException
{
QaContentHandler handler = new CustomContentHandler();
MimeConfig config = new MimeConfig();
MimeStreamParser parser = new MimeStreamParser(config);
InputStream stream = new FileInputStream("/home/javadev1/lib/INBOX");
parser.setContentHandler(handler);
try
{
do
{
parser.parse(stream);
}
while (stream.read() != -1);
}
finally
{
stream.close();
}
}
}
So, any help on how to build the information in the handler would be really helpful. I've tried setting a new MessageImpl, then using a builder to copy a parsed stream into it, and I have also tried to build a newMessage from a parse of the stream, and then print the Message when the END_MESSAGE header is read, but it printed nulls.
I may be experiencing a conceptual blind spot, too. If that's the case, I am ok with it being pointed out. Thanks!
Here is a code excerpt that works for me. As soon as I find an interesting message with the statebased parser i switch to the dom parser to create a message object.