private Multipart asMime4JMultipart() throws IOException { MultipartBuilder builder = MultipartBuilder.create(REPORT_SUB_TYPE); builder.addContentTypeParameter(new NameValuePair("report-type", DISPOSITION_NOTIFICATION_REPORT_TYPE)); builder.addBodyPart(BodyPartBuilder.create() .use(new BasicBodyFactory()) .setBody(humanReadableText, Charsets.UTF_8) .setContentType("text/plain", UTF_8_CHARSET)); builder.addBodyPart(BodyPartBuilder.create() .use(new BasicBodyFactory()) .setBody(report.formattedValue(), Charsets.UTF_8) .setContentType(DISPOSITION_CONTENT_TYPE, UTF_8_CHARSET)); return builder.build(); }
@Test public void extractReportShouldRejectSecondPartWithBadContentType() throws IOException { ExtractMDNOriginalJMAPMessageId testee = new ExtractMDNOriginalJMAPMessageId(mock(MailboxManager.class)); Message message = Message.Builder.of() .setBody(MultipartBuilder.create() .setSubType("report") .addTextPart("first", StandardCharsets.UTF_8) .addTextPart("second", StandardCharsets.UTF_8) .build()) .build(); assertThat(testee.extractReport(message)).isEmpty(); }
public static MultipartBuilder create(String subType) { return new MultipartBuilder().setSubType(subType); }
public static MultipartBuilder createCopy(Multipart other) { return new MultipartBuilder().copy(other); }
private Multipart createMultipartAlternativeBody(CreationMessage newMessage) throws IOException { MultipartBuilder bodyBuilder = MultipartBuilder.create(ALTERNATIVE_SUB_TYPE); addText(bodyBuilder, newMessage.getTextBody()); addHtml(bodyBuilder, newMessage.getHtmlBody()); return bodyBuilder.build(); }
@Test public 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(); }
.generateMessageId(InetAddress.getLocalHost().getCanonicalHostName()) .setBody(MultipartBuilder.create("mixed") .use(bodyFactory) .setPreamble("This is a multi-part message in MIME format.") .addBodyPart(BodyPartBuilder.create() .use(bodyFactory) .setBody("Why so serious?", Charsets.UTF_8) .build()) .addBodyPart(BodyPartBuilder.create() .use(bodyFactory) .setBody(createImageBody(bodyFactory, renderSampleImage())) .build())
@Test @SuppressWarnings("unchecked") public void processShouldNotOverrideTextBodyWhenItIsThere() throws Exception { MessageManager inbox = mailboxManager.getMailbox(inboxPath, session); ComposedMessageId message = inbox.appendMessage( AppendCommand.from(org.apache.james.mime4j.dom.Message.Builder.of() .setSubject("message subject") .setBody(MultipartBuilder.create() .setSubType("alternative") .addBodyPart(BodyPartBuilder.create() .setBody("My plain message", "plain", StandardCharsets.UTF_8)) .addBodyPart(BodyPartBuilder.create() .setBody("<a>The </a> <strong>HTML</strong> message", "html", StandardCharsets.UTF_8)) .build())), session); GetMessagesRequest request = GetMessagesRequest.builder() .ids(ImmutableList.of(message.getMessageId())) .build(); List<JmapResponse> result = testee.process(request, clientId, session).collect(Collectors.toList()); assertThat(result).hasSize(1) .extracting(JmapResponse::getResponse) .hasOnlyElementsOfType(GetMessagesResponse.class) .extracting(GetMessagesResponse.class::cast) .flatExtracting(GetMessagesResponse::list) .extracting(Message::getId, Message::getTextBody, Message::getHtmlBody) .containsOnly(Tuple.tuple(message.getMessageId(), Optional.of("My plain message"), Optional.of("<a>The </a> <strong>HTML</strong> message"))); }
@Test public void extractReportShouldExtractMDNWhenValidMDN() throws IOException { ExtractMDNOriginalJMAPMessageId testee = new ExtractMDNOriginalJMAPMessageId(mock(MailboxManager.class)); BodyPart mdn = BodyPartBuilder .create() .setBody(SingleBodyBuilder.create() .setText( "Reporting-UA: linagora.com; Evolution 3.26.5-1+b1 \n" + "Final-Recipient: rfc822; homer@linagora.com\n" + "Original-Message-ID: <1521557867.2614.0.camel@apache.org>\n" + "Disposition: manual-action/MDN-sent-manually;displayed\n") .buildText()) .setContentType("message/disposition-notification") .build(); Message message = Message.Builder.of() .setBody(MultipartBuilder.create("report") .addTextPart("first", StandardCharsets.UTF_8) .addBodyPart(mdn) .build()) .build(); assertThat(testee.extractReport(message)) .isNotEmpty() .contains(mdn); } }
private Consumer<MessageAttachment> addAttachment(MultipartBuilder builder) { return att -> { try { builder.addBodyPart(attachmentBodyPart(att)); } catch (IOException e) { LOGGER.error("Error while creating attachment", e); throw new RuntimeException(e); } }; }
public Builder copy(Message other) { if (other == null) { return this; } clearFields(); final Header otherHeader = other.getHeader(); if (otherHeader != null) { final List<Field> otherFields = otherHeader.getFields(); for (Field field: otherFields) { addField(field); } } Body body = null; Body otherBody = other.getBody(); if (otherBody instanceof Message) { body = Builder.of((Message) otherBody).build(); } else if (otherBody instanceof Multipart) { body = MultipartBuilder.createCopy((Multipart) otherBody).build(); } else if (otherBody instanceof SingleBody) { body = ((SingleBody) otherBody).copy(); } setBody(body); return this; }
public Multipart newMultipart(MultipartBuilder source) { return newMultipart(source.build()); }
public static MultipartBuilder create() { return new MultipartBuilder(); }
@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); }
private Multipart createMultipart(CreationMessage newMessage, ImmutableList<MessageAttachment> messageAttachments) { try { if (hasAttachment(messageAttachments)) { MultipartBuilder builder = MultipartBuilder.create(MIXED_SUB_TYPE); addBody(newMessage, builder); Consumer<MessageAttachment> addAttachment = addAttachment(builder); messageAttachments.forEach(addAttachment); return builder.build(); } else { return createMultipartAlternativeBody(newMessage); } } catch (IOException e) { LOGGER.error("Error while creating textBody \n{}\n or htmlBody \n{}", newMessage.getTextBody().get(), newMessage.getHtmlBody().get(), e); throw new RuntimeException(e); } }
public MultipartBuilder addBodyPart(BodyPartBuilder bodyPart) { return this.addBodyPart(bodyPart.build()); }
body = MessageBuilder.createCopy((Message) otherBody).build(); } else if (otherBody instanceof Multipart) { body = MultipartBuilder.createCopy((Multipart) otherBody).build(); } else if (otherBody instanceof SingleBody) { body = ((SingleBody) otherBody).copy();
public AbstractEntityBuilder setBody(MultipartBuilder multipart) { return this.setBody(multipart.build()); }
@Test public 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 public void extractReportShouldRejectMultipartWithSinglePart() throws Exception { ExtractMDNOriginalJMAPMessageId testee = new ExtractMDNOriginalJMAPMessageId(mock(MailboxManager.class)); Message message = Message.Builder.of() .setBody( MultipartBuilder.create() .setSubType("report") .addTextPart("content", StandardCharsets.UTF_8) .build()) .build(); assertThat(testee.extractReport(message)).isEmpty(); }