MessageContentExtractor { public MessageContent extract(org.apache.james.mime4j.dom.Message message) throws IOException { Body body = message.getBody(); if (body instanceof TextBody) { return parseTextBody(message, (TextBody)body); } if (body instanceof Multipart) { return parseMultipart(message, (Multipart)body); } return MessageContent.empty(); }  MessageContent extract(org.apache.james.mime4j.dom.Message message); static final String CONTENT_ID; static final String MULTIPART_ALTERNATIVE; static final String TEXT_HTML; static final String TEXT_PLAIN; }
@Test void extractShouldReturnEmptyWhenBinaryContentOnly() throws IOException { Message message = Message.Builder.of() .setBody(BasicBodyFactory.INSTANCE.binaryBody(BINARY_CONTENT, StandardCharsets.UTF_8)) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).isEmpty(); assertThat(actual.getHtmlBody()).isEmpty(); }
@Test void extractShouldReturnTextOnlyWhenTextOnlyBody() throws IOException { Message message = Message.Builder.of() .setBody(TEXT_CONTENT, StandardCharsets.UTF_8) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).contains(TEXT_CONTENT); assertThat(actual.getHtmlBody()).isEmpty(); }
@Test void extractShouldReturnHtmlOnlyWhenHtmlOnlyBody() throws IOException { Message message = Message.Builder.of() .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).isEmpty(); assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
@Test void extractShouldReturnHtmlAndTextWhenMultipartAlternative() throws IOException { Multipart multipart = MultipartBuilder.create("alternative") .addBodyPart(textPart) .addBodyPart(htmlPart) .build(); Message message = Message.Builder.of() .setBody(multipart) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).contains(TEXT_CONTENT); assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
@Test void extractShouldReturnHtmlWhenMultipartAlternativeWithoutPlainPart() throws IOException { Multipart multipart = MultipartBuilder.create("alternative") .addBodyPart(htmlPart) .build(); Message message = Message.Builder.of() .setBody(multipart) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).isEmpty(); assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
@Test void extractShouldReturnTextWhenMultipartAlternativeWithoutHtmlPart() throws IOException { Multipart multipart = MultipartBuilder.create("alternative") .addBodyPart(textPart) .build(); Message message = Message.Builder.of() .setBody(multipart) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).contains(TEXT_CONTENT); assertThat(actual.getHtmlBody()).isEmpty(); }
@Test void extractShouldReturnFirstNonAttachmentPartWhenMultipartMixed() throws IOException { Multipart multipart = MultipartBuilder.create("mixed") .addBodyPart(textAttachment) .addBodyPart(htmlPart) .addBodyPart(textPart) .build(); Message message = Message.Builder.of() .setBody(multipart) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); assertThat(actual.getTextBody()).isEmpty(); }
@Test void extractShouldReturnInlinedTextBodyWithoutCIDWhenNoOtherValidParts() throws IOException { String textBody = "body 1"; Multipart multipart = MultipartBuilder.create("report") .addBodyPart(BodyPartBuilder.create() .setBody(textBody, "plain", StandardCharsets.UTF_8) .setContentDisposition("inline")) .addBodyPart(BodyPartBuilder.create() .setBody("body 2", "rfc822-headers", StandardCharsets.UTF_8) .setContentDisposition("inline")) .build(); Message message = Message.Builder.of() .setBody(multipart) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).contains(textBody); }
@Test void extractShouldReturnEmptyWhenMultipartMixedAndFirstPartIsATextAttachment() throws IOException { Multipart multipart = MultipartBuilder.create("mixed") .addBodyPart(textAttachment) .build(); Message message = Message.Builder.of() .setBody(multipart) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).isEmpty(); assertThat(actual.getHtmlBody()).isEmpty(); }
@Test void extractShouldReturnFirstPartOnlyWhenMultipartMixedAndFirstPartIsHtml() throws IOException { Multipart multipart = MultipartBuilder.create("mixed") .addBodyPart(htmlPart) .addBodyPart(textPart) .build(); Message message = Message.Builder.of() .setBody(multipart) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).isEmpty(); assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
@Test void extractShouldReturnHtmlAndTextWhenMultipartMixedAndFirstPartIsMultipartAlternative() throws IOException { BodyPart multipartAlternative = BodyPartBuilder.create() .setBody(MultipartBuilder.create("alternative") .addBodyPart(htmlPart) .addBodyPart(textPart) .build()) .build(); Multipart multipartMixed = MultipartBuilder.create("mixed") .addBodyPart(multipartAlternative) .build(); Message message = Message.Builder.of() .setBody(multipartMixed) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).contains(TEXT_CONTENT); assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
@Test void extractShouldReturnHtmlWhenMultipartRelated() throws IOException { Multipart multipart = MultipartBuilder.create("related") .addBodyPart(htmlPart) .build(); Message message = Message.Builder.of() .setBody(multipart) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).isEmpty(); assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
@Test void extractShouldReturnHtmlAndTextWhenMultipartAlternativeAndFirstPartIsMultipartRelated() throws IOException { BodyPart multipartRelated = BodyPartBuilder.create() .setBody(MultipartBuilder.create("related") .addBodyPart(htmlPart) .build()) .build(); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(multipartRelated) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
@Test void extractShouldRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithoutCid() throws IOException { BodyPart inlinedHTMLPart = BodyPartBuilder.create() .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/html; charset=utf-8")); inlinedHTMLPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedHTMLPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
@Test void extractShouldNotRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithCid() throws IOException { BodyPart inlinedHTMLPart = BodyPartBuilder.create() .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/html; charset=utf-8")); inlinedHeader.addField(CONTENT_ID_FIELD); inlinedHTMLPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedHTMLPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getHtmlBody()).isEmpty(); }
@Test void extractShouldRetrieveTextBodyWithOneInlinedTextAttachmentWithoutCid() throws IOException { BodyPart inlinedTextPart = BodyPartBuilder.create() .setBody(TEXT_CONTENT, "text", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/plain; charset=utf-8")); inlinedTextPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedTextPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).contains(TEXT_CONTENT); }
@Test void extractShouldNotRetrieveTextBodyWithOneInlinedTextAttachmentWithCid() throws IOException { BodyPart inlinedTextPart = BodyPartBuilder.create() .setBody(TEXT_CONTENT, "text", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/plain; charset=utf-8")); inlinedHeader.addField(CONTENT_ID_FIELD); inlinedTextPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedTextPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).isEmpty(); }
@Test void extractShouldRetrieveTextAndHtmlBodyWhenOneInlinedTextAttachmentAndMainContentInMultipart() throws IOException { BodyPart multipartAlternative = BodyPartBuilder.create() .setBody(MultipartBuilder.create("alternative") .addBodyPart(textPart) .addBodyPart(htmlPart) .build()) .build(); Multipart multipartMixed = MultipartBuilder.create("mixed") .addBodyPart(multipartAlternative) .addBodyPart(inlineText) .build(); Message message = Message.Builder.of() .setBody(multipartMixed) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).contains(TEXT_CONTENT); assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
@Test void extractShouldRetrieveTextBodyAndHtmlBodyWhenTextBodyInMainMultipartAndHtmlBodyInInnerMultipart() throws IOException { BodyPart multipartRelated = BodyPartBuilder.create() .setBody(MultipartBuilder.create("related") .addBodyPart(htmlPart) .addBodyPart(inlineImage) .build()) .build(); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(textPart) .addBodyPart(multipartRelated) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).contains(TEXT_CONTENT); assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
@Test void extractShouldRespectCharsetWhenOtherThanUTF8() throws IOException { String text = "éééé\r\nèèèè\r\nàààà"; Message message = Message.Builder.of() .setBody(text, Charset.forName("windows-1252")) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).contains(text); }
@Test void extractShouldRespectCharsetWhenUTF8() throws IOException { String text = "éééé\r\nèèèè\r\nàààà"; Message message = Message.Builder.of() .setBody(text, StandardCharsets.UTF_8) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).contains(text); }
@Test void extractShouldUseUSASCIIWhenNoCharset() throws IOException { String text = "éééé\r\nèèèè\r\nàààà"; Message message = Message.Builder.of() .setBody(text, null) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).contains("????\r\n????\r\n????"); }