Java Code Examples for org.apache.james.mime4j.dom.Multipart#getBodyParts()

The following examples show how to use org.apache.james.mime4j.dom.Multipart#getBodyParts() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageStoreImpl.java    From sling-samples with Apache License 2.0 6 votes vote down vote up
static void recursiveMultipartProcessing(Multipart multipart, StringBuilder plainBody, StringBuilder htmlBody, Boolean hasBody, List<BodyPart> attachments) throws IOException {
    for (Entity enitiy : multipart.getBodyParts()) {
        BodyPart part = (BodyPart) enitiy;
        if (part.getDispositionType() != null && !part.getDispositionType().equals("")) {
            // if DispositionType is null or empty, it means that it's multipart, not attached file
            attachments.add(part);
        } else {
            if (part.isMimeType(PLAIN_MIMETYPE) && !hasBody) {
                plainBody.append(getTextPart(part));
                hasBody = true;
            } else if (part.isMimeType(HTML_MIMETYPE) && !hasBody) {
                htmlBody.append(getTextPart(part));
            } else if (part.isMultipart()) {
                recursiveMultipartProcessing((Multipart) part.getBody(), plainBody, htmlBody, hasBody, attachments);
            } 
        }
    }
}
 
Example 2
Source File: Mime4jMboxParserImplTest.java    From sling-samples with Apache License 2.0 6 votes vote down vote up
/**
 *        code taken from http://www.mozgoweb.com/posts/how-to-parse-mime-message-using-mime4j-library/
 */
private static String getPlainBody(Message msg) throws IOException {
    if (!msg.isMultipart()) {
        return getTextPart(msg);
    } else {
        Multipart multipart = (Multipart) msg.getBody();
        for (Entity enitiy : multipart.getBodyParts()) {
            BodyPart part = (BodyPart) enitiy;
            if (part.isMimeType("text/plain")) {
                return getTextPart(part);
            }
        }
    }

    return null;
}
 
Example 3
Source File: MboxReader.java    From baleen with Apache License 2.0 5 votes vote down vote up
/** Process a multipart body part */
private boolean processMultipart(JCas jCas, Multipart mp, String sourceUri) throws IOException {
  boolean doneBody = false;

  for (Entity e : mp.getBodyParts()) {
    if (e.getFilename() != null) {
      // Part has a filename, and is therefore an attachment
      String extension = FilenameUtils.getExtension(e.getFilename()).toLowerCase();
      if (ignoreExtensionsList.contains(extension)) {
        getMonitor().info("Skipping attachment {}", e.getFilename());
        continue;
      }

      attachments.put(sourceUri + "/" + e.getFilename(), e.getBody());
    } else if (!doneBody) {
      // Part has no filename, and we've not already processed a part to use as a body
      processBody(jCas, e.getBody(), sourceUri);

      // Add metadata
      for (Field f : e.getParent().getHeader().getFields()) {
        addMetadata(jCas, f.getName(), f.getBody());
      }

      doneBody = true;
    }
  }

  return doneBody;
}