예제 #1
0
  @Nonnull
  private static Content processPart(MimePart part, MimePart parent)
      throws MessagingException, IOException {
    if (part == null) {
      return new Content();
    }

    if (part.getFileName() != null) {
      // Assume that a part which has a filename is an attachment.
      return new Content(part);
    }

    if (part.isMimeType("text/*")) {
      return getContent(part);
    } else if (part.isMimeType("multipart/*")) {
      if (part.isMimeType(MimeType.MULTIPART_RELATED)) {
        return getContentWithAttachments(part);
      } else if (part.isMimeType(MimeType.MULTIPART_ALTERNATIVE)) {
        return getContentOfBestPart(part, parent);
      } else {
        return getJoinedContent(part);
      }
    }

    return new Content();
  }
예제 #2
0
 private static Content getContentOfBestPart(MimePart part, MimePart parent)
     throws IOException, MessagingException {
   MimeBodyPart best = null;
   MimeMultipart mp = (MimeMultipart) part.getContent();
   for (int i = 0; i < mp.getCount(); i++) {
     // Prefer HTML if the parent is a multipart/related part which may contain
     // inline images, because text/plain cannot embed the images.
     boolean isHtmlPreferred = parent != null && parent.isMimeType(MimeType.MULTIPART_RELATED);
     best = better((MimeBodyPart) mp.getBodyPart(i), best, isHtmlPreferred);
   }
   return processPart(best, part);
 }
예제 #3
0
 private static Content getContentWithAttachments(MimePart part)
     throws MessagingException, IOException {
   Content result = new Content();
   String rootId = new ContentType(part.getContentType()).getParameter("start");
   MimeMultipart mp = (MimeMultipart) part.getContent();
   for (int i = 0; i < mp.getCount(); i++) {
     MimePart p = (MimePart) mp.getBodyPart(i);
     if (isRootPart(p, i, rootId)) {
       result = result.merge(processPart(p, part));
     } else {
       result.attachments.add(p);
     }
   }
   return result;
 }
예제 #4
0
  private static Map<String, Attachment> saveAttachments(
      Collection<MimePart> partsToAttach, Resource container)
      throws MessagingException, IOException, NoSuchAlgorithmException {
    Map<String, Attachment> result = new HashMap<>();

    for (MimePart partToAttach : partsToAttach) {
      Attachment attachment = saveAttachment(partToAttach, container);
      if (partToAttach.getContentID() != null) {
        String cid = partToAttach.getContentID().trim();
        cid = cid.replace("<", "");
        cid = cid.replace(">", "");
        result.put(cid, attachment);
      }
    }

    return result;
  }
예제 #5
0
 private static Content getJoinedContent(MimePart part) throws IOException, MessagingException {
   Content result = new Content();
   MimeMultipart mp = (MimeMultipart) part.getContent();
   for (int i = 0; i < mp.getCount(); i++) {
     MimeBodyPart p = (MimeBodyPart) mp.getBodyPart(i);
     result.merge(processPart(p, part));
   }
   return result;
 }
예제 #6
0
 /**
  * Returns true if the given part is root part.
  *
  * <p>The given part is root part, if the part is the first one and the given root id is not
  * defined or the content id of the part equals to the given root id.
  *
  * @param part
  * @param nthPart
  * @param rootId
  * @return
  * @throws MessagingException
  */
 private static boolean isRootPart(MimePart part, int nthPart, String rootId)
     throws MessagingException {
   return (rootId == null && nthPart == 0) || StringUtils.equals(part.getContentID(), rootId);
 }
예제 #7
0
 private static Content getContent(MimePart part) throws IOException, MessagingException {
   Content result = new Content();
   result.body = (String) part.getContent();
   result.type = part.getContentType();
   return result;
 }