public InputStream getBodyInputStream() { try { return ((SingleBody) message.getBody()).getInputStream(); } catch (IOException e) { return null; } }
@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); }
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(); }
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(); }
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(); }
Body body = msg.getBody();
Body body = msg.getBody();
/** * Creates a new <code>Message</code> from the specified * <code>Message</code>. The <code>Message</code> instance is * initialized with copies of header and body of the specified * <code>Message</code>. The parent entity of the new message is * <code>null</code>. * * @param other * message to copy. * @throws UnsupportedOperationException * if <code>other</code> contains a {@link SingleBody} that * does not support the {@link SingleBody#copy() copy()} * operation. * @throws IllegalArgumentException * if <code>other</code> contains a <code>Body</code> that * is neither a {@link MessageImpl}, {@link Multipart} or * {@link SingleBody}. */ public Message copy(Message other) { MessageImpl copy = newMessageImpl(); if (other.getHeader() != null) { copy.setHeader(copy(other.getHeader())); } if (other.getBody() != null) { copy.setBody(copy(other.getBody())); } return copy; }
public List<MessageAttachment> retrieveAttachments(InputStream fullContent) throws MimeException, 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(); } }
@Test public void convertToMimeShouldGenerateMultipartWhenHtmlBodyAndTextBodyProvided() throws Exception { // Given MIMEMessageConverter sut = new MIMEMessageConverter(); 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()); // 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); }
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; }
@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"); }
@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"); }
@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"); }
@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"); }
@Test public void convertToMimeShouldSetTextBodyWhenProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(); TextBody expected = new BasicBodyFactory().textBody("Hello all!", StandardCharsets.UTF_8); CreationMessage testMessage = CreationMessage.builder() .mailboxId("dead-bada55") .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .textBody("Hello all!") .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 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; }