org.apache.james.mime4j.dom.Multipart Java Examples
The following examples show how to use
org.apache.james.mime4j.dom.Multipart.
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: MessageParser.java From james-project with Apache License 2.0 | 6 votes |
public List<ParsedAttachment> retrieveAttachments(InputStream fullContent) throws IOException { DefaultMessageBuilder defaultMessageBuilder = new DefaultMessageBuilder(); defaultMessageBuilder.setMimeEntityConfig(MimeConfig.PERMISSIVE); defaultMessageBuilder.setDecodeMonitor(DecodeMonitor.SILENT); Message message = defaultMessageBuilder.parseMessage(fullContent); Body body = message.getBody(); try { if (isAttachment(message, Context.BODY)) { return ImmutableList.of(retrieveAttachment(message)); } if (body instanceof Multipart) { Multipart multipartBody = (Multipart) body; return listAttachments(multipartBody, Context.fromSubType(multipartBody.getSubType())) .collect(Guavate.toImmutableList()); } else { return ImmutableList.of(); } } finally { body.dispose(); } }
Example #2
Source File: MessageContentExtractor.java From james-project with Apache License 2.0 | 6 votes |
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 #3
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@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); }
Example #4
Source File: MIMEMessageConverter.java From james-project with Apache License 2.0 | 6 votes |
private Multipart createMultipartWithAttachments(CreationMessage newMessage, ImmutableList<MessageAttachmentMetadata> messageAttachments, MailboxSession session) throws IOException { MultipartBuilder mixedMultipartBuilder = MultipartBuilder.create(MIXED_SUB_TYPE); List<MessageAttachmentMetadata> inlineAttachments = messageAttachments.stream() .filter(MessageAttachmentMetadata::isInline) .collect(Guavate.toImmutableList()); List<MessageAttachmentMetadata> besideAttachments = messageAttachments.stream() .filter(attachment -> !attachment.isInline()) .collect(Guavate.toImmutableList()); if (inlineAttachments.size() > 0) { mixedMultipartBuilder.addBodyPart(relatedInnerMessage(newMessage, inlineAttachments, session)); } else { addBody(newMessage, mixedMultipartBuilder); } addAttachments(besideAttachments, mixedMultipartBuilder, session); return mixedMultipartBuilder.build(); }
Example #5
Source File: MIMEMessageConverterTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void convertToMimeShouldGenerateMultipartWhenHtmlBodyAndTextBodyProvided() 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(); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(), session); // Then assertThat(result.getBody()).isInstanceOf(Multipart.class); assertThat(result.isMultipart()).isTrue(); assertThat(result.getMimeType()).isEqualTo("multipart/alternative"); Multipart typedResult = (Multipart)result.getBody(); assertThat(typedResult.getBodyParts()).hasSize(2); }
Example #6
Source File: ExtractMDNOriginalJMAPMessageId.java From james-project with Apache License 2.0 | 6 votes |
@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 #7
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@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); }
Example #8
Source File: AbstractMessageSearchIndexTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void searchWithPDFAttachmentShouldReturnMailsWhenAttachmentContentMatches() throws Exception { assumeTrue(storeMailboxManager.getSupportedSearchCapabilities().contains(MailboxManager.SearchCapabilities.Attachment)); byte[] attachmentContent = ClassLoaderUtils.getSystemResourceAsByteArray("eml/attachment.pdf"); Multipart multipart = MultipartBuilder.create("mixed") .addBodyPart(BodyPartBuilder.create() .setBody(attachmentContent, "application/pdf") .setContentDisposition("attachment")) .addBodyPart(BodyPartBuilder.create() .setBody("The message has a PDF attachment.", "plain", StandardCharsets.UTF_8)) .build(); Message message = Message.Builder.of() .setBody(multipart) .build(); ComposedMessageId messageWithBeautifulBananaAsPDFAttachment = myFolderMessageManager .appendMessage(MessageManager.AppendCommand.from(message), session).getId(); await(); SearchQuery searchQuery = SearchQuery.of(SearchQuery.attachmentContains("beautiful banana")); assertThat(messageSearchIndex.search(session, mailbox2, searchQuery)) .containsExactly(messageWithBeautifulBananaAsPDFAttachment.getUid()); }
Example #9
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@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); }
Example #10
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void extractShouldRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithoutCid() throws IOException { //Given 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(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
Example #11
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void extractShouldNotRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithCid() throws IOException { //Given 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(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getHtmlBody()).isEmpty(); }
Example #12
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void extractShouldRetrieveTextBodyWithOneInlinedTextAttachmentWithoutCid() throws IOException { //Given 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(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getTextBody()).contains(TEXT_CONTENT); }
Example #13
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void extractShouldNotRetrieveTextBodyWithOneInlinedTextAttachmentWithCid() throws IOException { //Given 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(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getTextBody()).isEmpty(); }
Example #14
Source File: Mime4jMboxParserImplTest.java From sling-samples with Apache License 2.0 | 6 votes |
/** * 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 #15
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@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); }
Example #16
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@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); }
Example #17
Source File: MessageStoreImplAttachmentsTest.java From sling-samples with Apache License 2.0 | 6 votes |
@Test public void simpleMultipartMessageTest() throws IOException { Multipart multipart = new MultipartImpl("mixed"); BodyPart att0 = createTextBody("This is the first part of the template..", "plain", true); multipart.addBodyPart(att0); BodyPart att1 = createRandomBinaryAttachment(200); multipart.addBodyPart(att1); BodyPart att2 = createRandomBinaryAttachment(300); multipart.addBodyPart(att2); BodyPart att3 = createTextBody("Some sample text here...?!", "html", true); multipart.addBodyPart(att3); BodyPart att4 = createRandomBinaryAttachment(100); multipart.addBodyPart(att4); BodyPart att5 = createTextBody("Some other text here...?!", "plain", true); multipart.addBodyPart(att5); MessageImpl message = new MessageImpl(); message.setMultipart(multipart); message.setSubject("Template message"); message.setDate(new Date()); message.getHeader().setField(new RawField(LIST_ID, "<list.example.com>")); assertSaveMessageWithAttachments(message, 6); }
Example #18
Source File: MessageStoreImpl.java From sling-samples with Apache License 2.0 | 6 votes |
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 #19
Source File: MessageContentExtractor.java From james-project with Apache License 2.0 | 5 votes |
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 #20
Source File: MessageContentExtractor.java From james-project with Apache License 2.0 | 5 votes |
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 #21
Source File: MessageContentExtractor.java From james-project with Apache License 2.0 | 5 votes |
private Optional<String> getFirstMatchingTextBody(Multipart multipart, String mimeType) throws IOException { Optional<String> firstMatchingTextBody = getFirstMatchingTextBody(multipart, mimeType, this::isNotAttachment); if (firstMatchingTextBody.isPresent()) { return firstMatchingTextBody; } Optional<String> fallBackInlinedBodyWithoutCid = getFirstMatchingTextBody(multipart, mimeType, this::isInlinedWithoutCid); return fallBackInlinedBodyWithoutCid; }
Example #22
Source File: MessageContentExtractor.java From james-project with Apache License 2.0 | 5 votes |
private Optional<MessageContent> retrieveFirstReadablePartMatching(Multipart multipart, Predicate<Entity> predicate) { return multipart.getBodyParts() .stream() .filter(predicate) .flatMap(Throwing.function(this::extractContentIfReadable).sneakyThrow()) .findFirst(); }
Example #23
Source File: MessageContentExtractor.java From james-project with Apache License 2.0 | 5 votes |
private MessageContent retrieveHtmlAndPlainTextContent(Multipart multipart) throws IOException { Optional<String> textBody = getFirstMatchingTextBody(multipart, TEXT_PLAIN); Optional<String> htmlBody = getFirstMatchingTextBody(multipart, TEXT_HTML); MessageContent directChildTextBodies = new MessageContent(textBody, htmlBody); if (!directChildTextBodies.isComplete()) { MessageContent fromInnerMultipart = parseFirstFoundMultipart(multipart); return directChildTextBodies.merge(fromInnerMultipart); } return directChildTextBodies; }
Example #24
Source File: DsnMailCreator.java From mireka with Apache License 2.0 | 5 votes |
private Multipart multipartReport() { Multipart result = new MultipartImpl("report"); result.addBodyPart(humanReadableTextBodyPart()); result.addBodyPart(deliveryStatusBodyPart()); result.addBodyPart(originalMessageBodyPart()); return result; }
Example #25
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 5 votes |
@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); }
Example #26
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 5 votes |
@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); }
Example #27
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 5 votes |
@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(); }
Example #28
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 5 votes |
@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(); }
Example #29
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 5 votes |
@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(); }
Example #30
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 5 votes |
@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); }