2014-01-16 15 views
1

我正在使用MIME4J從電子郵件堆棧轉儲中讀取MIME事件。我試圖將START_MESSAGE和END_MESSAGE標頭定義的給定消息事件作爲整個事件讀取,因爲我將最終將流程移至分佈式文件系統,並需要計劃文件分割邊界遍歷。MIME4J中基於事件的解析 - 如何從InputStream填充新消息?

對於mime4j中基於事件的解析,需要一個ContentHandler接口,並且解析器從它調用方法,這需要爲其設置處理程序。我從另一個SO回答中嘗試了一個示例Handler,它擴展了mime4j打包的SimpleContentHandler,但是那個只是解析頭文件。

我想構建我的自定義ContentHandler類來收集完整的消息作爲一個事件。然後,我需要將事件放在一個臨時對象中,以便我可以解析標題,它們的字段以及字段的內容。最終目標是將這種行爲調整爲MapReduce,以便應對電子郵件的一部分將在一個文件分割上的可能性,並且另一部分在不同的文件分割中是必要的。

對於我定製的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頭時打印該消息,但它打印了空值。

我也可能會遇到概念盲點。如果是這樣的話,我確信它會被指出。謝謝!

+0

使用dom解析器可能會容易得多。 –

回答

1

這是一段代碼摘錄,適合我。只要我發現一個有趣的消息與基於狀態的解析器我切換到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; 
}