我正在与MIME4J一起从电子邮件堆栈转储中读取MIME事件。我试图将给定的消息事件(由START_MESSAGE和END_MESSAGE头定义)读取为一个完整的事件,因为我将最终将进程移动到分布式文件系统,并且需要计划文件分割边界遍历。
对于mime4j中基于事件的解析,需要一个ContentHandler接口,解析器从它调用方法,需要将处理程序设置为它。我试验了来自另一个扩展mime4j打包的SimpleContentHandler的示例Handler,但是它实际上只解析了头。
我正在尝试构建我的自定义ContentHandler类,以便将完整的消息作为一个事件来收集。然后,我需要将事件放在一个临时对象中,这样我就可以解析标题、它们的字段以及其中字段的内容。最终目标是将这种行为调整到MapReduce中,因此必须处理电子邮件的一个部分位于一个文件中,而另一个部分位于另一个文件part上的可能性。
对于我的自定义ContentHandler,我已经了解到:
public class CustomContentHandler extends AbstractContentHandler {}作为一项主要内容,我使用:
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();
}
}
}因此,任何关于如何在处理程序中构建信息的帮助都是非常有帮助的。我尝试设置一个新的MessageImpl,然后使用一个构建器将一个解析的流复制到它中,我还尝试从流的解析中构建一个newMessage,然后在读取END_MESSAGE头时打印消息,但是它打印了空值。
我可能也在经历一个概念上的盲点。如果是这样的话,我不介意被指出。谢谢!
发布于 2014-07-18 12:02:18
这是一段对我有用的代码摘录。一旦在基于状态的解析器中找到一条有趣的消息,我就切换到dom解析器来创建一个消息对象。
/**
* check the MessageStream
*
* @param in - the inputstream to check
* @param id - the id of a message to search for
* @param encoding - the encoding of the stream e.g. ISO-8859
* @return - the message with the given id of null if none is found
* @throws IOException
* @throws MimeException
*/
public Message checkMessageStream(InputStream in, String id, Charset encoding)
throws IOException, MimeException {
// https://james.apache.org/mime4j/usage.html
String messageString = new String(StreamUtils.getBytes(in));
messageString = fixMessageString(messageString);
InputStream instream = new ByteArrayInputStream(
messageString.getBytes(encoding));
MimeTokenStream stream = new MimeTokenStream();
stream.parse(instream);
for (EntityState state = stream.getState(); state != EntityState.T_END_OF_STREAM; state = stream
.next()) {
switch (state) {
case T_BODY:
if (debug) {
System.out.println("Body detected, contents = "
+ stream.getInputStream() + ", header data = "
+ stream.getBodyDescriptor());
}
break;
case T_FIELD:
Field field = stream.getField();
if (debug) {
System.out.println("Header field detected: " + stream.getField());
}
if (field.getName().toLowerCase().equals("message-id")) {
// System.out.println("id: " + field.getBody() + "=" + id + "?");
if (field.getBody().equals("<" + id + ">")) {
InputStream messageStream = new ByteArrayInputStream(
messageString.getBytes(encoding));
Message message = MessageServiceFactory.newInstance()
.newMessageBuilder().parseMessage(messageStream);
return message;
} else {
return null;
}
}
break;
case T_START_MULTIPART:
if (debug) {
System.out.println("Multipart message detexted," + " header data = "
+ stream.getBodyDescriptor());
}
break;
}
}
return null;
}https://stackoverflow.com/questions/21170222
复制相似问题