public MIMEMessageConverter() { this.bodyFactory = new BasicBodyFactory(); }
public BinaryBody buildBinary() throws IOException { Charset cs = this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET; if (this.bodyFactory != null) { if (this.text != null) { return this.bodyFactory.binaryBody(InputStreams.create(this.text, cs)); } else if (this.bin != null) { return this.bodyFactory.binaryBody(InputStreams.create(this.bin)); } else { return this.bodyFactory.binaryBody((InputStreams.create(new byte[]{}))); } } else { if (this.bin != null) { return BasicBodyFactory.INSTANCE.binaryBody(this.bin); } else if (this.text != null) { return BasicBodyFactory.INSTANCE.binaryBody(this.text, cs); } else { return BasicBodyFactory.INSTANCE.binaryBody(new byte [] {}); } } }
public TextBody textBody(final String text) { return textBody(text, Charsets.DEFAULT_CHARSET); }
@Test public void convertToMimeShouldSetEmptyTextBodyWhenProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(); TextBody expected = new BasicBodyFactory().textBody("", StandardCharsets.UTF_8); CreationMessage testMessage = CreationMessage.builder() .mailboxId("dead-bada55") .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .textBody("") .build(); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of()); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); assertThat(result.getMimeType()).isEqualTo("text/plain"); }
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(); }
public TextBody textBody(final String text, final String mimeCharset) throws UnsupportedEncodingException { if (text == null) { throw new IllegalArgumentException("Text may not be null"); } return new StringBody1(text, resolveCharset(mimeCharset)); }
@Test public void convertToMimeShouldSetEmptyHtmlBodyWhenProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(); TextBody expected = new BasicBodyFactory().textBody("", StandardCharsets.UTF_8); CreationMessage testMessage = CreationMessage.builder() .mailboxId("dead-bada55") .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .htmlBody("") .build(); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of()); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); assertThat(result.getMimeType()).isEqualTo("text/html"); }
public TextBody textBody(final InputStream content, final String mimeCharset) throws IOException { if (content == null) { throw new IllegalArgumentException("Input stream may not be null"); } return new StringBody2(ContentUtil.buffer(content), resolveCharset(mimeCharset)); }
@Test public void convertToMimeShouldSetEmptyBodyWhenNoBodyProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(); TextBody expected = new BasicBodyFactory().textBody("", StandardCharsets.UTF_8); CreationMessage testMessage = CreationMessage.builder() .mailboxId("dead-bada55") .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .build(); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of()); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); }
/** * 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); }
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(); }
private TextBody createTextBody(CreationMessage newMessage) { String body = newMessage.getHtmlBody() .orElse(newMessage.getTextBody() .orElse("")); return bodyFactory.textBody(body, StandardCharsets.UTF_8); }
.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); TextBody expectedAttachmentBody = new BasicBodyFactory().textBody(text.getBytes(), StandardCharsets.UTF_8); MessageAttachment attachment = MessageAttachment.builder() .attachment(org.apache.james.mailbox.model.Attachment.builder()
/** * Sets binary content of this message with the given MIME type. * * @param bin * the body. * @param mimeType * the MIME media type of the specified body * ("type/subtype"). */ public Builder 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); }
public Message parseMessage(final InputStream is) throws IOException, MimeIOException { try { MessageImpl message = newMessageImpl(); MimeConfig cfg = config != null ? config : MimeConfig.DEFAULT; boolean strict = cfg.isStrictParsing(); DecodeMonitor mon = monitor != null ? monitor : strict ? DecodeMonitor.STRICT : DecodeMonitor.SILENT; BodyDescriptorBuilder bdb = bodyDescBuilder != null ? bodyDescBuilder : new DefaultBodyDescriptorBuilder(null, fieldParser != null ? fieldParser : strict ? DefaultFieldParser.getParser() : LenientFieldParser.getParser(), mon); BodyFactory bf = bodyFactory != null ? bodyFactory : new BasicBodyFactory(!strict); MimeStreamParser parser = new MimeStreamParser(cfg, mon, bdb); parser.setContentHandler(new ParserStreamContentHandler(message, bf)); parser.setContentDecoding(contentDecoding); if (flatMode) { parser.setFlat(); } else { parser.setRecurse(); } parser.parse(is); return message; } catch (MimeException e) { throw new MimeIOException(e); } }
public TextBody buildText() throws IOException { Charset cs = this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET; if (this.bodyFactory != null) { if (this.text != null) { return this.bodyFactory.textBody(InputStreams.create(this.text, cs), cs.name()); } else if (this.bin != null) { return this.bodyFactory.textBody(InputStreams.create(this.bin), cs.name()); } else { return this.bodyFactory.textBody(InputStreams.create(new byte [] {}), cs.name()); } } else { if (this.text != null) { return BasicBodyFactory.INSTANCE.textBody(this.text, cs); } else if (this.bin != null) { return BasicBodyFactory.INSTANCE.textBody(this.bin, cs); } else { return BasicBodyFactory.INSTANCE.textBody(new byte [] {}, cs); } } }
@Test public void convertToMimeShouldSetHtmlBodyWhenProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(); TextBody expected = new BasicBodyFactory().textBody("Hello <b>all</b>!", StandardCharsets.UTF_8); CreationMessage testMessage = CreationMessage.builder() .mailboxId("dead-bada55") .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .htmlBody("Hello <b>all</b>!") .build(); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of()); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); }
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); }
public Builder parse(final InputStream is) throws IOException { MimeConfig currentConfig = config != null ? config : MimeConfig.DEFAULT; boolean strict = currentConfig.isStrictParsing(); DecodeMonitor currentMonitor = monitor != null ? monitor : strict ? DecodeMonitor.STRICT : DecodeMonitor.SILENT; BodyDescriptorBuilder currentBodyDescBuilder = bodyDescBuilder != null ? bodyDescBuilder : new DefaultBodyDescriptorBuilder(null, fieldParser != null ? fieldParser : strict ? DefaultFieldParser.getParser() : LenientFieldParser.getParser(), currentMonitor); BodyFactory currentBodyFactory = bodyFactory != null ? bodyFactory : new BasicBodyFactory(!strict); MimeStreamParser parser = new MimeStreamParser(currentConfig, currentMonitor, currentBodyDescBuilder); Message message = new MessageImpl(); parser.setContentHandler(new ParserStreamContentHandler(message, currentBodyFactory)); parser.setContentDecoding(!rawContent); if (flatMode) { parser.setFlat(); } try { parser.parse(is); } catch (MimeException e) { throw new MimeIOException(e); } clearFields(); final List<Field> fields = message.getHeader().getFields(); for (Field field: fields) { addField(field); } setBody(message.getBody()); return this; }
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); }