org.apache.james.mime4j.dom.Entity Java Examples

The following examples show how to use org.apache.james.mime4j.dom.Entity. 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: MessageContentExtractor.java    From james-project with Apache License 2.0 6 votes vote down vote up
private Stream<MessageContent> extractContentIfReadable(Entity entity) throws IOException {
    if (TEXT_HTML.equals(entity.getMimeType()) && entity.getBody() instanceof TextBody) {
        return Stream.of(
                MessageContent.ofHtmlOnly(asString((TextBody)entity.getBody())));
    }
    if (TEXT_PLAIN.equals(entity.getMimeType()) && entity.getBody() instanceof TextBody) {
        return Stream.of(
                MessageContent.ofTextOnly(asString((TextBody)entity.getBody())));
    }
    if (entity.isMultipart() && entity.getBody() instanceof Multipart) {
        MessageContent innerMultipartContent = parseMultipart(entity, (Multipart)entity.getBody());
        if (!innerMultipartContent.isEmpty()) {
            return Stream.of(innerMultipartContent);
        }
    }
    return Stream.empty();
}
 
Example #4
Source File: MessageParser.java    From james-project with Apache License 2.0 6 votes vote down vote up
private ParsedAttachment retrieveAttachment(Entity entity) throws IOException {
    Optional<ContentTypeField> contentTypeField = getContentTypeField(entity);
    Optional<ContentDispositionField> contentDispositionField = getContentDispositionField(entity);
    Optional<ContentType> contentType = contentTypeField.map(ContentTypeField::getBody)
        .filter(string -> !string.isEmpty())
        .map(ContentType::of);
    Optional<String> name = name(contentTypeField, contentDispositionField);
    Optional<Cid> cid = cid(readHeader(entity, CONTENT_ID, ContentIdField.class));
    boolean isInline = isInline(readHeader(entity, CONTENT_DISPOSITION, ContentDispositionField.class)) && cid.isPresent();

    return ParsedAttachment.builder()
            .contentType(contentType.orElse(DEFAULT_CONTENT_TYPE))
            .content(getContent(entity.getBody()))
            .name(name)
            .cid(cid)
            .inline(isInline);
}
 
Example #5
Source File: ExtractMDNOriginalJMAPMessageId.java    From james-project with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting Optional<Entity> extractReport(Message message) {
    LOGGER.debug("Extracting report");
    if (!message.isMultipart()) {
        LOGGER.debug("MDN Message must be multipart");
        return Optional.empty();
    }
    List<Entity> bodyParts = ((Multipart) message.getBody()).getBodyParts();
    if (bodyParts.size() < 2) {
        LOGGER.debug("MDN Message must contain at least two parts");
        return Optional.empty();
    }
    Entity report = bodyParts.get(1);
    if (!isDispositionNotification(report)) {
        LOGGER.debug("MDN Message second part must be of type " + MESSAGE_DISPOSITION_NOTIFICATION);
        return Optional.empty();
    }
    return Optional.of(report);
}
 
Example #6
Source File: MessageStoreImpl.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
/**
 *	code taken from http://www.mozgoweb.com/posts/how-to-parse-mime-message-using-mime4j-library/
 */
static String getTextPart(Entity part) throws IOException {
    TextBody tb = (TextBody) part.getBody();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    tb.writeTo(baos);
    return baos.toString(MailArchiveServerConstants.DEFAULT_ENCODER.charset().name());
}
 
Example #7
Source File: MessageContentExtractor.java    From james-project with Apache License 2.0 5 votes vote down vote up
private Optional<String> getFirstMatchingTextBody(Multipart multipart, String mimeType, Predicate<Entity> condition) {
    Function<TextBody, Optional<String>> textBodyOptionalFunction = Throwing
        .function(this::asString).sneakyThrow();

    return multipart.getBodyParts()
        .stream()
        .filter(part -> mimeType.equals(part.getMimeType()))
        .filter(condition)
        .map(Entity::getBody)
        .filter(TextBody.class::isInstance)
        .map(TextBody.class::cast)
        .findFirst()
        .flatMap(textBodyOptionalFunction);
}
 
Example #8
Source File: MessageContentExtractor.java    From james-project with Apache License 2.0 5 votes vote down vote up
private Optional<MessageContent> retrieveFirstReadablePartMatching(Multipart multipart, Predicate<Entity> predicate) {
    return multipart.getBodyParts()
        .stream()
        .filter(predicate)
        .flatMap(Throwing.function(this::extractContentIfReadable).sneakyThrow())
        .findFirst();
}
 
Example #9
Source File: MessageContentExtractor.java    From james-project with Apache License 2.0 5 votes vote down vote up
private MessageContent parseFirstFoundMultipart(Multipart multipart) throws IOException {
    ThrowingFunction<Entity, MessageContent> parseMultipart = firstPart -> parseMultipart(firstPart, (Multipart)firstPart.getBody());
    return multipart.getBodyParts()
        .stream()
        .filter(part -> part.getBody() instanceof Multipart)
        .findFirst()
        .map(Throwing.function(parseMultipart).sneakyThrow())
        .orElse(MessageContent.empty());
}
 
Example #10
Source File: MessageContentExtractor.java    From james-project with Apache License 2.0 5 votes vote down vote up
private MessageContent parseMultipartContent(Entity entity, Multipart multipart) throws IOException {
    switch (entity.getMimeType()) {
    case MULTIPART_ALTERNATIVE:
        return retrieveHtmlAndPlainTextContent(multipart);
    default:
        return retrieveFirstReadablePart(multipart);
    }
}
 
Example #11
Source File: MessageContentExtractor.java    From james-project with Apache License 2.0 5 votes vote down vote up
private MessageContent parseMultipart(Entity entity, Multipart multipart) throws IOException {
    MessageContent messageContent = parseMultipartContent(entity, multipart);
    if (!messageContent.isEmpty()) {
        return messageContent;
    }
    return parseFirstFoundMultipart(multipart);
}
 
Example #12
Source File: MessageContentExtractor.java    From james-project with Apache License 2.0 5 votes vote down vote up
private MessageContent parseTextBody(Entity entity, TextBody textBody) throws IOException {
    Optional<String> bodyContent = asString(textBody);
    if (TEXT_HTML.equals(entity.getMimeType())) {
        return MessageContent.ofHtmlOnly(bodyContent);
    }
    return MessageContent.ofTextOnly(bodyContent);
}
 
Example #13
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void convertToMimeShouldHaveChildrenAttachmentParts() throws Exception {
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    CreationMessage testMessage = CreationMessage.builder()
        .mailboxIds(ImmutableList.of("dead-bada55"))
        .subject("subject")
        .from(DraftEmailer.builder().name("sender").build())
        .htmlBody("Hello <b>all<b>!")
        .build();

    String text = "123456";
    MessageAttachmentMetadata attachment = MessageAttachmentMetadata.builder()
        .name("ديناصور.png")
        .attachment(AttachmentMetadata.builder()
            .attachmentId(AttachmentId.from("blodId"))
            .size(text.getBytes().length)
            .type("image/png")
            .build())
        .cid(Cid.from("cid"))
        .isInline(false)
        .build();
    when(attachmentContentLoader.load(attachment.getAttachment(), session))
        .thenReturn(new ByteArrayInputStream(text.getBytes()));

    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
        CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment), session);
    Multipart typedResult = (Multipart)result.getBody();

    assertThat(typedResult.getBodyParts())
        .extracting(Entity::getDispositionType)
        .anySatisfy(contentDisposition -> assertThat(contentDisposition).isEqualTo("attachment"));
}
 
Example #14
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void convertToMimeShouldNotHaveInnerMultipartWhenNoInline() throws Exception {
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    CreationMessage testMessage = CreationMessage.builder()
        .mailboxIds(ImmutableList.of("dead-bada55"))
        .subject("subject")
        .from(DraftEmailer.builder().name("sender").build())
        .htmlBody("Hello <b>all<b>!")
        .build();

    String text = "123456";
    MessageAttachmentMetadata attachment = MessageAttachmentMetadata.builder()
        .name("ديناصور.png")
        .attachment(AttachmentMetadata.builder()
            .attachmentId(AttachmentId.from("blodId"))
            .size(text.getBytes().length)
            .type("image/png")
            .build())
        .cid(Cid.from("cid"))
        .isInline(false)
        .build();
    when(attachmentContentLoader.load(attachment.getAttachment(), session))
        .thenReturn(new ByteArrayInputStream(text.getBytes()));

    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
        CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment), session);
    Multipart typedResult = (Multipart)result.getBody();

    assertThat(typedResult.getBodyParts())
        .noneMatch(Entity::isMultipart);
}
 
Example #15
Source File: ExtractMDNOriginalJMAPMessageId.java    From james-project with Apache License 2.0 5 votes vote down vote up
private Optional<MDNReport> parseReport(Entity report) {
    LOGGER.debug("Parsing report");
    try (InputStream inputStream = ((SingleBody) report.getBody()).getInputStream()) {
        Try<MDNReport> result = MDNReportParser.parse(inputStream, report.getCharset());
        if (result.isSuccess()) {
            return Optional.of(result.get());
        } else {
            LOGGER.error("unable to parse MESSAGE_DISPOSITION_NOTIFICATION part", result.failed().get());
            return Optional.empty();
        }
    } catch (IOException e) {
        LOGGER.error("unable to parse MESSAGE_DISPOSITION_NOTIFICATION part", e);
        return Optional.empty();
    }
}
 
Example #16
Source File: MessageParser.java    From james-project with Apache License 2.0 5 votes vote down vote up
private boolean attachmentDispositionCriterion(Entity part) {
    return getContentDispositionField(part)
        .map(ContentDispositionField::getDispositionType)
        .map(dispositionType -> dispositionType.toLowerCase(Locale.US))
        .map(ATTACHMENT_CONTENT_DISPOSITIONS::contains)
        .orElse(false);
}
 
Example #17
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;
}
 
Example #18
Source File: MessageParser.java    From james-project with Apache License 2.0 5 votes vote down vote up
private boolean attachmentContentTypeCriterion(Entity part) {
    return getContentTypeField(part)
        .map(ContentTypeField::getMimeType)
        .map(dispositionType -> dispositionType.toLowerCase(Locale.US))
        .map(ATTACHMENT_CONTENT_TYPES::contains)
        .orElse(false);
}
 
Example #19
Source File: MessageParser.java    From james-project with Apache License 2.0 5 votes vote down vote up
private boolean isTextPart(Entity part) {
    return getContentTypeField(part)
        .filter(header -> !ATTACHMENT_CONTENT_TYPES.contains(header.getMimeType()))
        .map(ContentTypeField::getMediaType)
        .map(TEXT_MEDIA_TYPE::equals)
        .orElse(false);
}
 
Example #20
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 4 votes vote down vote up
private String getNameParameterValue(Entity attachmentPart) {
    return ((ContentTypeField) attachmentPart.getHeader().getField("Content-Type")).getParameter("name");
}
 
Example #21
Source File: MessageContentExtractor.java    From james-project with Apache License 2.0 4 votes vote down vote up
private boolean isInlinedWithoutCid(Entity part) {
    return Objects.equals(part.getDispositionType(), MimeMessage.INLINE)
        && part.getHeader().getField(CONTENT_ID) == null;
}
 
Example #22
Source File: MessageContentExtractor.java    From james-project with Apache License 2.0 4 votes vote down vote up
private boolean isNotAttachment(Entity part) {
    return part.getDispositionType() == null;
}
 
Example #23
Source File: MessageStoreImplAttachmentsTest.java    From sling-samples with Apache License 2.0 4 votes vote down vote up
private String getBinPart(Entity part) throws IOException {
    BinaryBody bb = (BinaryBody) part.getBody();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bb.writeTo(baos);
    return new String(baos.toByteArray());
}
 
Example #24
Source File: MessageParser.java    From james-project with Apache License 2.0 4 votes vote down vote up
private <T extends ParsedField> Optional<T> readHeader(Entity entity, String headerName, Class<T> clazz) {
    return castField(entity.getHeader().getField(headerName), clazz);
}
 
Example #25
Source File: MessageParser.java    From james-project with Apache License 2.0 4 votes vote down vote up
private Optional<ContentTypeField> getContentTypeField(Entity entity) {
    return castField(entity.getHeader().getField(CONTENT_TYPE), ContentTypeField.class);
}
 
Example #26
Source File: MessageParser.java    From james-project with Apache License 2.0 4 votes vote down vote up
private Optional<ContentDispositionField> getContentDispositionField(Entity entity) {
    return castField(entity.getHeader().getField(CONTENT_DISPOSITION), ContentDispositionField.class);
}
 
Example #27
Source File: MessageParser.java    From james-project with Apache License 2.0 4 votes vote down vote up
private boolean isMultipart(Entity entity) {
    return entity.isMultipart() && entity.getBody() instanceof Multipart;
}
 
Example #28
Source File: MessageParser.java    From james-project with Apache License 2.0 4 votes vote down vote up
private boolean isAttachment(Entity part, Context context) {
    if (context == Context.BODY && isTextPart(part)) {
        return false;
    }
    return attachmentDispositionCriterion(part) || attachmentContentTypeCriterion(part) || hadCID(part);
}
 
Example #29
Source File: MessageParser.java    From james-project with Apache License 2.0 4 votes vote down vote up
public static Context fromEntity(Entity entity) {
    if (isMultipartAlternative(entity)) {
        return BODY;
    }
    return OTHER;
}
 
Example #30
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Test
void convertToMimeShouldAddAttachmentAndMultipartAlternativeWhenOneAttachementAndTextAndHtmlBody() throws Exception {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    CreationMessage testMessage = CreationMessage.builder()
        .mailboxId("dead-bada55")
        .subject("subject")
        .from(DraftEmailer.builder().name("sender").build())
        .textBody("Hello all!")
        .htmlBody("Hello <b>all<b>!")
        .build();
    TextBody expectedTextBody = new BasicBodyFactory().textBody("Hello all!".getBytes(), StandardCharsets.UTF_8);
    TextBody expectedHtmlBody = new BasicBodyFactory().textBody("Hello <b>all<b>!".getBytes(), StandardCharsets.UTF_8);

    String expectedCID = "cid";
    String expectedMimeType = "image/png";
    String text = "123456";
    TextBody expectedAttachmentBody = new BasicBodyFactory().textBody(text.getBytes(), StandardCharsets.UTF_8);
    MessageAttachmentMetadata attachment = MessageAttachmentMetadata.builder()
        .attachment(AttachmentMetadata.builder()
            .attachmentId(AttachmentId.from("blodId"))
            .size(text.getBytes().length)
            .type(expectedMimeType)
            .build())
        .cid(Cid.from(expectedCID))
        .isInline(true)
        .build();
    when(attachmentContentLoader.load(attachment.getAttachment(), session))
        .thenReturn(new ByteArrayInputStream(text.getBytes()));

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
        CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment), session);
    Multipart typedResult = (Multipart)result.getBody();

    assertThat(typedResult.getBodyParts())
        .hasSize(1)
        .extracting(entity -> (Multipart) entity.getBody())
        .flatExtracting(Multipart::getBodyParts)
        .satisfies(part -> {
            assertThat(part.getBody()).isInstanceOf(Multipart.class);
            assertThat(part.isMultipart()).isTrue();
            assertThat(part.getMimeType()).isEqualTo("multipart/alternative");
            assertThat(((Multipart)part.getBody()).getBodyParts()).hasSize(2);
            Entity textPart = ((Multipart)part.getBody()).getBodyParts().get(0);
            Entity htmlPart = ((Multipart)part.getBody()).getBodyParts().get(1);
            assertThat(textPart.getBody()).isEqualToComparingOnlyGivenFields(expectedTextBody, "content");
            assertThat(htmlPart.getBody()).isEqualToComparingOnlyGivenFields(expectedHtmlBody, "content");
        }, Index.atIndex(0))
        .satisfies(part -> {
            assertThat(part.getBody()).isEqualToComparingOnlyGivenFields(expectedAttachmentBody, "content");
            assertThat(part.getDispositionType()).isEqualTo("inline");
            assertThat(part.getMimeType()).isEqualTo(expectedMimeType);
            assertThat(part.getHeader().getField("Content-ID").getBody()).isEqualTo(expectedCID);
        }, Index.atIndex(1));
}