public MultipartBuilder addBinaryPart(byte[] bin, String mimeType) throws IOException { BinaryBody body = bodyFactory != null ? bodyFactory.binaryBody(InputStreams.create(bin)) : BasicBodyFactory.INSTANCE.binaryBody(bin); BodyPart bodyPart = BodyPartBuilder.create() .setBody(body) .setContentType(mimeType != null ? mimeType : "application/octet-stream") .setContentTransferEncoding("base64") .build(); return addBodyPart(bodyPart); }
@Before public void setup() throws IOException { testee = new MessageContentExtractor(); textPart = BodyPartBuilder.create().setBody(TEXT_CONTENT, "plain", StandardCharsets.UTF_8); htmlPart = BodyPartBuilder.create().setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8); textAttachment = BodyPartBuilder.create() .setBody(ATTACHMENT_CONTENT, "plain", StandardCharsets.UTF_8) .setContentDisposition("attachment"); inlineText = BodyPartBuilder.create() .setBody(ATTACHMENT_CONTENT, "plain", StandardCharsets.UTF_8) .setContentDisposition("inline"); inlineImage = BodyPartBuilder.create() .setBody(new byte[0], "image/png") .setContentDisposition("inline"); }
private void addHtml(MultipartBuilder builder, Optional<String> htmlBody) throws IOException { if (htmlBody.isPresent()) { builder.addBodyPart(BodyPartBuilder.create() .use(bodyFactory) .setBody(htmlBody.get(), StandardCharsets.UTF_8) .setContentType(HTML_MEDIA_TYPE, UTF_8_CHARSET) .setContentTransferEncoding(QUOTED_PRINTABLE)); } }
private BodyPart attachmentBodyPart(MessageAttachment att) throws IOException { BodyPartBuilder builder = BodyPartBuilder.create() .use(bodyFactory) .setBody(new BasicBodyFactory().binaryBody(ByteStreams.toByteArray(att.getAttachment().getStream()))) .setField(contentTypeField(att)) .setField(contentDispositionField(att.isInline())) .setContentTransferEncoding(BASE64); contentId(builder, att); return builder.build(); }
@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 public void getMessageListFileNameFilterShouldNotReturnMessagesWithOnlyAttachmentContentMatching() throws Exception { mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, ALICE, "mailbox"); mailboxProbe.appendMessage(ALICE, MailboxPath.forUser(ALICE, "mailbox"), MessageManager.AppendCommand.builder() .build(Message.Builder.of() .setBody( MultipartBuilder.create("alternative") .addBodyPart(BodyPartBuilder.create() .setContentDisposition("attachment", "nomatch.md") .setBody(SingleBodyBuilder.create() .setText("matchme.txt ...") .setCharset(StandardCharsets.UTF_8) .build()) .build()) .build()))); await(); given() .header("Authorization", aliceAccessToken.serialize()) .body("[[\"getMessageList\", {\"filter\":{\"attachmentFileName\":\"matchme.txt\"}}, \"#0\"]]") .when() .post("/jmap") .then() .statusCode(200) .body(NAME, equalTo("messageList")) .body(ARGUMENTS + ".messageIds", hasSize(0)); }
.addBodyPart(BodyPartBuilder.create() .use(bodyFactory) .setBody("Why so serious?", Charsets.UTF_8) .setContentTransferEncoding("quoted-printable") .build()) .addBodyPart(BodyPartBuilder.create() .use(bodyFactory) .setBody(createImageBody(bodyFactory, renderSampleImage())) .setContentType("image/png") .setContentTransferEncoding("base64") .setContentDisposition("attachment", "smiley.png") .build()) .build())
private void addBody(CreationMessage newMessage, MultipartBuilder builder) throws IOException { if (newMessage.getHtmlBody().isPresent() && newMessage.getTextBody().isPresent()) { Multipart body = createMultipartAlternativeBody(newMessage); builder.addBodyPart(BodyPartBuilder.create().setBody(body)); } else { addText(builder, newMessage.getTextBody()); addHtml(builder, newMessage.getHtmlBody()); } }
/** * Sets binary content of this message with the given MIME type. * * @param body * the body. * @param mimeType * the MIME media type of the specified body * ("type/subtype"). */ public BodyPartBuilder setBody(byte[] bin, String mimeType) throws IOException { setField(Fields.contentType(mimeType != null ? mimeType : "application/octet-stream")); Body binBody; if (bodyFactory != null) { binBody = bodyFactory.binaryBody(InputStreams.create(bin)); } else { binBody = BasicBodyFactory.INSTANCE.binaryBody(bin); } return setBody(binBody); }
/** * Sets text of this message with the charset. * * @param text * the text. * @param charset * the charset of the text. */ public BodyPartBuilder setBody(String text, Charset charset) throws IOException { return setBody(text, null, charset); }
public MultipartBuilder addBodyPart(BodyPartBuilder bodyPart) { return this.addBodyPart(bodyPart.build()); }
@Test public 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); }
.setBody( MultipartBuilder.create("alternative") .addBodyPart(BodyPartBuilder.create() .setContentDisposition("attachment", "matchme.txt") .setBody(SingleBodyBuilder.create() .setText("this is the file content...") .setCharset(StandardCharsets.UTF_8) .build()) .build()) .build())));
.addBodyPart(BodyPartBuilder.create() .use(bodyFactory) .setBody("This is the first part of the template..", Charsets.UTF_8) .setContentTransferEncoding("quoted-printable") .build()) .addBodyPart(BodyPartBuilder.create() .use(bodyFactory) .setBody(createRandomBinary(200), "application/octet-stream") .setContentTransferEncoding("base64") .build()) .addBodyPart(BodyPartBuilder.create() .use(bodyFactory) .setBody(createRandomBinary(300), "application/octet-stream") .setContentTransferEncoding("base64") .build()) .build()) .setSubject("Template message") for (int i = 0; i < count; i++) { String text = "Text inserted after part " + (i + 1); BodyPart bodyPart = BodyPartBuilder.create() .use(bodyFactory) .setBody(text, Charsets.UTF_8) .setContentTransferEncoding("quoted-printable") .build(); multipart.addBodyPart(bodyPart, 2 * i + 1);
@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"))); }
/** * Sets text of this message with the given MIME subtype and charset. * * @param text * the text. * @param charset * the charset of the text. * @param subtype * the text subtype (e.g. "plain", "html" or * "xml"). */ public BodyPartBuilder setBody(String text, String subtype, Charset charset) throws IOException { String mimeType = "text/" + (subtype != null ? subtype : "plain"); if (charset != null) { setField(Fields.contentType(mimeType, new NameValuePair("charset", charset.name()))); } else { setField(Fields.contentType(mimeType)); } Body textBody; if (bodyFactory != null) { textBody = bodyFactory.textBody( InputStreams.create(text, charset), charset != null ? charset.name() : null); } else { textBody = BasicBodyFactory.INSTANCE.textBody(text, charset); } return setBody(textBody); }
@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); }
public MultipartBuilder addTextPart(String text, Charset charset) throws IOException { Charset cs = charset != null ? charset : Charsets.ISO_8859_1; TextBody body = bodyFactory != null ? bodyFactory.textBody( InputStreams.create(text, cs), cs.name()) : BasicBodyFactory.INSTANCE.textBody(text, cs); BodyPart bodyPart = BodyPartBuilder.create() .setBody(body) .setContentType("text/plain", new NameValuePair("charset", cs.name())) .setContentTransferEncoding(Charsets.US_ASCII.equals(cs) ? "7bit" : "quoted-printable") .build(); return addBodyPart(bodyPart); }
@BeforeEach void setup() throws IOException { testee = new MessageContentExtractor(); textPart = BodyPartBuilder.create().setBody(TEXT_CONTENT, "plain", StandardCharsets.UTF_8); htmlPart = BodyPartBuilder.create().setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8); textAttachment = BodyPartBuilder.create() .setBody(ATTACHMENT_CONTENT, "plain", StandardCharsets.UTF_8) .setContentDisposition("attachment"); inlineText = BodyPartBuilder.create() .setBody(ATTACHMENT_CONTENT, "plain", StandardCharsets.UTF_8) .setContentDisposition("inline"); inlineImage = BodyPartBuilder.create() .setBody(new byte[0], "image/png") .setContentDisposition("inline"); }
private void addText(MultipartBuilder builder, Optional<String> textBody) throws IOException { if (textBody.isPresent()) { builder.addBodyPart(BodyPartBuilder.create() .use(bodyFactory) .setBody(textBody.get(), StandardCharsets.UTF_8) .setContentType(PLAIN_TEXT_MEDIA_TYPE, UTF_8_CHARSET) .setContentTransferEncoding(QUOTED_PRINTABLE)); } }