Java Code Examples for org.apache.james.mime4j.dom.Message#getBody()
The following examples show how to use
org.apache.james.mime4j.dom.Message#getBody() .
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: 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 2
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 3
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 4
Source File: MboxReader.java From baleen with Apache License 2.0 | 5 votes |
@Override protected void doGetNext(JCas jCas) throws IOException, CollectionException { if (!attachments.isEmpty()) { // If we have attachments, first process those Map.Entry<String, Body> entry = attachments.firstEntry(); getMonitor().info("Processing attachment {}", entry.getKey()); processBody(jCas, entry.getValue(), entry.getKey()); attachments.remove(entry.getKey()); } else { // No attachments so process the next message String raw = mboxIterator.next().toString(); count++; String uri = "mbox://" + mbox + "#" + count; getMonitor().info("Processing message {}", uri); // Parse message and get body Message msg = messageBuilder.parseMessage(new ByteArrayInputStream(raw.getBytes(charset))); Body body = msg.getBody(); boolean doneBody = false; // Decide how to process body of message if (body instanceof SingleBody) { doneBody = processBody(jCas, body, uri); } else if (body instanceof Multipart) { Multipart mp = (Multipart) body; doneBody = processMultipart(jCas, mp, uri); } // No body found (just attachments? Or invalid message?) if (!doneBody) { throw new IOException("No processable body found"); } } }
Example 5
Source File: MIMEMessageConverterTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void convertToMimeShouldHaveMixedMultipart() throws Exception { MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader); CreationMessage testMessage = CreationMessage.builder() .mailboxIds(ImmutableList.of("dead-bada55")) .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .htmlBody("Hello <b>all<b>!") .build(); String text = "123456"; MessageAttachmentMetadata attachment = MessageAttachmentMetadata.builder() .name("ديناصور.png") .attachment(AttachmentMetadata.builder() .attachmentId(AttachmentId.from("blodId")) .size(text.getBytes().length) .type("image/png") .build()) .cid(Cid.from("cid")) .isInline(false) .build(); when(attachmentContentLoader.load(attachment.getAttachment(), session)) .thenReturn(new ByteArrayInputStream(text.getBytes())); Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment), session); assertThat(result.getBody()).isInstanceOf(Multipart.class); Multipart typedResult = (Multipart)result.getBody(); assertThat(typedResult.getSubType()).isEqualTo("mixed"); }
Example 6
Source File: MIMEMessageConverterTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void convertToMimeShouldNotHaveInnerMultipartWhenNoInline() throws Exception { MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader); CreationMessage testMessage = CreationMessage.builder() .mailboxIds(ImmutableList.of("dead-bada55")) .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .htmlBody("Hello <b>all<b>!") .build(); String text = "123456"; MessageAttachmentMetadata attachment = MessageAttachmentMetadata.builder() .name("ديناصور.png") .attachment(AttachmentMetadata.builder() .attachmentId(AttachmentId.from("blodId")) .size(text.getBytes().length) .type("image/png") .build()) .cid(Cid.from("cid")) .isInline(false) .build(); when(attachmentContentLoader.load(attachment.getAttachment(), session)) .thenReturn(new ByteArrayInputStream(text.getBytes())); Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment), session); Multipart typedResult = (Multipart)result.getBody(); assertThat(typedResult.getBodyParts()) .noneMatch(Entity::isMultipart); }
Example 7
Source File: MIMEMessageConverterTest.java From james-project with Apache License 2.0 | 5 votes |
@Test void convertToMimeShouldHaveChildrenAttachmentParts() throws Exception { MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader); CreationMessage testMessage = CreationMessage.builder() .mailboxIds(ImmutableList.of("dead-bada55")) .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .htmlBody("Hello <b>all<b>!") .build(); String text = "123456"; MessageAttachmentMetadata attachment = MessageAttachmentMetadata.builder() .name("ديناصور.png") .attachment(AttachmentMetadata.builder() .attachmentId(AttachmentId.from("blodId")) .size(text.getBytes().length) .type("image/png") .build()) .cid(Cid.from("cid")) .isInline(false) .build(); when(attachmentContentLoader.load(attachment.getAttachment(), session)) .thenReturn(new ByteArrayInputStream(text.getBytes())); Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment), session); Multipart typedResult = (Multipart)result.getBody(); assertThat(typedResult.getBodyParts()) .extracting(Entity::getDispositionType) .anySatisfy(contentDisposition -> assertThat(contentDisposition).isEqualTo("attachment")); }
Example 8
Source File: MessageStoreImpl.java From sling-samples with Apache License 2.0 | 4 votes |
private void save(ResourceResolver resolver, Message msg) throws IOException, LoginException { // apply message processors for(MessageProcessor processor : getSortedMessageProcessors()) { logger.debug("Calling {}", processor); processor.processMessage(msg); } // into path: archive/domain/list/thread/message final Map<String, Object> msgProps = new HashMap<String, Object>(); final List<BodyPart> attachments = new ArrayList<BodyPart>(); msgProps.put(resourceTypeKey, MailArchiveServerConstants.MESSAGE_RT); StringBuilder plainBody = new StringBuilder(); StringBuilder htmlBody = new StringBuilder(); Boolean hasBody = false; if (!msg.isMultipart()) { plainBody = new StringBuilder(getTextPart(msg)); } else { Multipart multipart = (Multipart) msg.getBody(); recursiveMultipartProcessing(multipart, plainBody, htmlBody, hasBody, attachments); } msgProps.put(PLAIN_BODY, plainBody.toString().replaceAll("\r\n", "\n")); if (htmlBody.length() > 0) { msgProps.put(HTML_BODY, htmlBody.toString()); } msgProps.putAll(getMessagePropertiesFromHeader(msg.getHeader())); ByteArrayOutputStream baos = new ByteArrayOutputStream(); new DefaultMessageWriter().writeHeader(msg.getHeader(), baos); String origHdr = baos.toString(MailArchiveServerConstants.DEFAULT_ENCODER.charset().name()); msgProps.put(X_ORIGINAL_HEADER, origHdr); final Header hdr = msg.getHeader(); final String listIdRaw = hdr.getField(LIST_ID).getBody(); final String listId = listIdRaw.substring(1, listIdRaw.length()-1); // remove < and > final String list = getListNodeName(listId); final String domain = getDomainNodeName(listId); final String subject = (String) msgProps.get(SUBJECT); final String threadPath = threadKeyGen.getThreadKey(subject); final String threadName = removeRe(subject); Resource parentResource = assertEachNode(resolver, archivePath, domain, list, threadPath, threadName); String msgNodeName = makeJcrFriendly((String) msgProps.get(NAME)); boolean isMsgNodeCreated = assertResource(resolver, parentResource, msgNodeName, msgProps); if (isMsgNodeCreated) { Resource msgResource = resolver.getResource(parentResource, msgNodeName); for (BodyPart att : attachments) { if (!attachmentFilter.isEligible(att)) { continue; } final Map<String, Object> attProps = new HashMap<String, Object>(); parseHeaderToProps(att.getHeader(), attProps); Body body = att.getBody(); if (body instanceof BinaryBody) { attProps.put(CONTENT, ((BinaryBody) body).getInputStream()); } else if (body instanceof TextBody) { attProps.put(CONTENT, ((TextBody) body).getInputStream()); } String attNodeName = Text.escapeIllegalJcrChars(att.getFilename()); assertResource(resolver, msgResource, attNodeName, attProps); } updateThread(resolver, parentResource, msgProps); } }
Example 9
Source File: MIMEMessageConverterTest.java From james-project with Apache License 2.0 | 4 votes |
@Test void convertToMimeShouldAddAttachment() throws Exception { // Given MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader); CreationMessage testMessage = CreationMessage.builder() .mailboxId("dead-bada55") .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .htmlBody("Hello <b>all<b>!") .build(); String expectedCID = "cid"; String expectedMimeType = "image/png"; String text = "123456"; TextBody expectedBody = new BasicBodyFactory().textBody(text.getBytes(), StandardCharsets.UTF_8); AttachmentId blodId = AttachmentId.from("blodId"); MessageAttachmentMetadata attachment = MessageAttachmentMetadata.builder() .attachment(AttachmentMetadata.builder() .attachmentId(blodId) .size(text.getBytes().length) .type(expectedMimeType) .build()) .cid(Cid.from(expectedCID)) .isInline(true) .build(); when(attachmentContentLoader.load(attachment.getAttachment(), session)) .thenReturn(new ByteArrayInputStream(text.getBytes())); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment), session); Multipart typedResult = (Multipart)result.getBody(); assertThat(typedResult.getBodyParts()) .hasSize(1) .extracting(entity -> (Multipart) entity.getBody()) .flatExtracting(Multipart::getBodyParts) .anySatisfy(part -> { assertThat(part.getBody()).isEqualToComparingOnlyGivenFields(expectedBody, "content"); assertThat(part.getDispositionType()).isEqualTo("inline"); assertThat(part.getMimeType()).isEqualTo(expectedMimeType); assertThat(part.getHeader().getField("Content-ID").getBody()).isEqualTo(expectedCID); assertThat(part.getContentTransferEncoding()).isEqualTo("base64"); }); }
Example 10
Source File: MIMEMessageConverterTest.java From james-project with Apache License 2.0 | 4 votes |
@Test void convertToMimeShouldPreservePartCharset() throws Exception { // Given MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader); CreationMessage testMessage = CreationMessage.builder() .mailboxId("dead-bada55") .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .htmlBody("Hello <b>all<b>!") .build(); String expectedCID = "cid"; String expectedMimeType = "text/calendar; charset=\"iso-8859-1\""; String text = "123456"; TextBody expectedBody = new BasicBodyFactory().textBody(text.getBytes(), StandardCharsets.UTF_8); AttachmentId blodId = AttachmentId.from("blodId"); MessageAttachmentMetadata attachment = MessageAttachmentMetadata.builder() .attachment(AttachmentMetadata.builder() .attachmentId(blodId) .size(text.getBytes().length) .type(expectedMimeType) .build()) .cid(Cid.from(expectedCID)) .isInline(true) .build(); when(attachmentContentLoader.load(attachment.getAttachment(), session)) .thenReturn(new ByteArrayInputStream(text.getBytes())); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment), session); Multipart typedResult = (Multipart)result.getBody(); assertThat(typedResult.getBodyParts()) .hasSize(1) .extracting(entity -> (Multipart) entity.getBody()) .flatExtracting(Multipart::getBodyParts) .anySatisfy(part -> { assertThat(part.getBody()).isEqualToComparingOnlyGivenFields(expectedBody, "content"); assertThat(part.getDispositionType()).isEqualTo("inline"); assertThat(part.getMimeType()).isEqualTo("text/calendar"); assertThat(part.getCharset()).isEqualTo("iso-8859-1"); assertThat(part.getHeader().getField("Content-ID").getBody()).isEqualTo(expectedCID); assertThat(part.getContentTransferEncoding()).isEqualTo("base64"); }); }
Example 11
Source File: MIMEMessageConverterTest.java From james-project with Apache License 2.0 | 4 votes |
@Test void convertToMimeShouldAddAttachmentAndMultipartAlternativeWhenOneAttachementAndTextAndHtmlBody() 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(); TextBody expectedTextBody = new BasicBodyFactory().textBody("Hello all!".getBytes(), StandardCharsets.UTF_8); TextBody expectedHtmlBody = new BasicBodyFactory().textBody("Hello <b>all<b>!".getBytes(), StandardCharsets.UTF_8); String expectedCID = "cid"; String expectedMimeType = "image/png"; String text = "123456"; TextBody expectedAttachmentBody = new BasicBodyFactory().textBody(text.getBytes(), StandardCharsets.UTF_8); MessageAttachmentMetadata attachment = MessageAttachmentMetadata.builder() .attachment(AttachmentMetadata.builder() .attachmentId(AttachmentId.from("blodId")) .size(text.getBytes().length) .type(expectedMimeType) .build()) .cid(Cid.from(expectedCID)) .isInline(true) .build(); when(attachmentContentLoader.load(attachment.getAttachment(), session)) .thenReturn(new ByteArrayInputStream(text.getBytes())); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment), session); Multipart typedResult = (Multipart)result.getBody(); assertThat(typedResult.getBodyParts()) .hasSize(1) .extracting(entity -> (Multipart) entity.getBody()) .flatExtracting(Multipart::getBodyParts) .satisfies(part -> { assertThat(part.getBody()).isInstanceOf(Multipart.class); assertThat(part.isMultipart()).isTrue(); assertThat(part.getMimeType()).isEqualTo("multipart/alternative"); assertThat(((Multipart)part.getBody()).getBodyParts()).hasSize(2); Entity textPart = ((Multipart)part.getBody()).getBodyParts().get(0); Entity htmlPart = ((Multipart)part.getBody()).getBodyParts().get(1); assertThat(textPart.getBody()).isEqualToComparingOnlyGivenFields(expectedTextBody, "content"); assertThat(htmlPart.getBody()).isEqualToComparingOnlyGivenFields(expectedHtmlBody, "content"); }, Index.atIndex(0)) .satisfies(part -> { assertThat(part.getBody()).isEqualToComparingOnlyGivenFields(expectedAttachmentBody, "content"); assertThat(part.getDispositionType()).isEqualTo("inline"); assertThat(part.getMimeType()).isEqualTo(expectedMimeType); assertThat(part.getHeader().getField("Content-ID").getBody()).isEqualTo(expectedCID); }, Index.atIndex(1)); }