org.apache.james.mime4j.dom.TextBody Java Examples
The following examples show how to use
org.apache.james.mime4j.dom.TextBody.
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: MessageContentExtractor.java From james-project with Apache License 2.0 | 6 votes |
private Stream<MessageContent> extractContentIfReadable(Entity entity) throws IOException { if (TEXT_HTML.equals(entity.getMimeType()) && entity.getBody() instanceof TextBody) { return Stream.of( MessageContent.ofHtmlOnly(asString((TextBody)entity.getBody()))); } if (TEXT_PLAIN.equals(entity.getMimeType()) && entity.getBody() instanceof TextBody) { return Stream.of( MessageContent.ofTextOnly(asString((TextBody)entity.getBody()))); } if (entity.isMultipart() && entity.getBody() instanceof Multipart) { MessageContent innerMultipartContent = parseMultipart(entity, (Multipart)entity.getBody()); if (!innerMultipartContent.isEmpty()) { return Stream.of(innerMultipartContent); } } return Stream.empty(); }
Example #2
Source File: MIMEMessageConverterTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void convertToMimeShouldSetEmptyTextBodyWhenProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader); 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(), session); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); assertThat(result.getMimeType()).isEqualTo("text/plain"); }
Example #3
Source File: MIMEMessageConverterTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void convertToMimeShouldSetEmptyHtmlBodyWhenProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader); 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(), session); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); assertThat(result.getMimeType()).isEqualTo("text/html"); }
Example #4
Source File: MIMEMessageConverterTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void convertToMimeShouldSetTextBodyWhenProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader); 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(), session); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); }
Example #5
Source File: MIMEMessageConverterTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void convertToMimeShouldSetEmptyBodyWhenNoBodyProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader); 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(), session); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); }
Example #6
Source File: MIMEMessageConverterTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void convertToMimeShouldSetHtmlBodyWhenProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader); 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(), session); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); }
Example #7
Source File: DsnMailCreator.java From mireka with Apache License 2.0 | 5 votes |
private BodyPart deliveryStatusBodyPart() { BodyPart result = new BodyPart(); TextBody textBody = new BasicBodyFactory().textBody(deliveryStatusText()); result.setBody(textBody, "message/delivery-status"); return result; }
Example #8
Source File: DsnMailCreator.java From mireka with Apache License 2.0 | 5 votes |
private BodyPart humanReadableTextBodyPart() { BodyPart result = new BodyPart(); TextBody textBody = new BasicBodyFactory().textBody(humanReadableText()); result.setText(textBody); return result; }
Example #9
Source File: MessageContentExtractor.java From james-project with Apache License 2.0 | 5 votes |
private Optional<String> getFirstMatchingTextBody(Multipart multipart, String mimeType, Predicate<Entity> condition) { Function<TextBody, Optional<String>> textBodyOptionalFunction = Throwing .function(this::asString).sneakyThrow(); return multipart.getBodyParts() .stream() .filter(part -> mimeType.equals(part.getMimeType())) .filter(condition) .map(Entity::getBody) .filter(TextBody.class::isInstance) .map(TextBody.class::cast) .findFirst() .flatMap(textBodyOptionalFunction); }
Example #10
Source File: MessageContentExtractor.java From james-project with Apache License 2.0 | 5 votes |
private MessageContent parseTextBody(Entity entity, TextBody textBody) throws IOException { Optional<String> bodyContent = asString(textBody); if (TEXT_HTML.equals(entity.getMimeType())) { return MessageContent.ofHtmlOnly(bodyContent); } return MessageContent.ofTextOnly(bodyContent); }
Example #11
Source File: MessageContentExtractor.java From james-project with Apache License 2.0 | 5 votes |
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(); }
Example #12
Source File: MboxReader.java From baleen with Apache License 2.0 | 5 votes |
/** Process a single body part */ private boolean processBody(JCas jCas, Body body, String sourceUri) throws IOException { if (body instanceof TextBody) { // Process plain text body processTextBody(jCas, (TextBody) body); // Add fields from parent for (Field f : body.getParent().getHeader().getFields()) { addMetadata(jCas, f.getName(), f.getBody()); } // Set up document annotation - this is done by the content extractor in other cases DocumentAnnotation doc = UimaSupport.getDocumentAnnotation(jCas); doc.setSourceUri(sourceUri); doc.setTimestamp(System.currentTimeMillis()); } else if (body instanceof BinaryBody) { processBinaryBody(jCas, (BinaryBody) body, sourceUri); } else if (body instanceof Multipart) { // Multipart message, so recurse Multipart mp = (Multipart) body; return processMultipart(jCas, mp, sourceUri); } else { // No body processed return false; } return true; }
Example #13
Source File: MessageStoreImplAttachmentsTest.java From sling-samples with Apache License 2.0 | 5 votes |
private static BodyPart createTextBody(String text, String subtype, boolean isAttachment) { TextBody body = new StorageBodyFactory().textBody(text, MailArchiveServerConstants.DEFAULT_ENCODER.charset().name()); BodyPart bodyPart = new BodyPart(); if (isAttachment) { bodyPart.setContentDisposition("attachment", "file"+Math.random()); } bodyPart.setText(body, subtype); return bodyPart; }
Example #14
Source File: MessageStoreImplAttachmentsTest.java From sling-samples with Apache License 2.0 | 5 votes |
private void assertSaveMessageWithAttachments(Message msg, int num) throws IOException { store.save(msg); List<BodyPart> attList = new LinkedList<BodyPart>(); MessageStoreImpl.recursiveMultipartProcessing((Multipart) msg.getBody(), new StringBuilder(), new StringBuilder(), false, attList); @SuppressWarnings("unchecked") Queue<BodyPart> attachmentsMsg = (Queue<BodyPart>) attList; assertTrue("No attachments found", attachmentsMsg.size() > 0); assertEquals("", num, attachmentsMsg.size()); final Resource r = resolver.getResource(getResourcePath(msg, store)); assertNotNull("Expecting non-null Resource", r); for (Resource aRes : r.getChildren()) { final ModifiableValueMap aMap = aRes.adaptTo(ModifiableValueMap.class); BodyPart aMsg = attachmentsMsg.poll(); assertNotNull("JCR contains more attachments", aMsg); for (Field f : aMsg.getHeader().getFields()) { String name = f.getName(); assertEquals("Field "+name+" is different", (aMap.get(name, String.class)), f.getBody()); } if (aMsg.getBody() instanceof TextBody) { assertEquals("Content is not the same", MessageStoreImpl.getTextPart(aMsg), aMap.get(CONTENT, String.class)); } else if (aMsg.getBody() instanceof BinaryBody) { assertEquals("Content is not the same", getBinPart(aMsg), aMap.get(CONTENT, String.class)); } else { fail("Unknown type of attachment body"); } } assertEquals("Message contains more attachments", attachmentsMsg.poll(), null); }
Example #15
Source File: AttachmentFilterImpl.java From sling-samples with Apache License 2.0 | 5 votes |
@Override public boolean isEligible(BodyPart attachment) { // extension check final String filename = attachment.getFilename(); String ext = ""; int idx = filename.lastIndexOf('.'); if (idx > -1) { ext = filename.substring(idx + 1); } if (eligibleExtensions != null && !eligibleExtensions.contains(ext)) { return false; } // size check final Body body = attachment.getBody(); try { if ( body instanceof BinaryBody && IOUtils.toByteArray(((BinaryBody) body).getInputStream()).length > maxSize || body instanceof TextBody && IOUtils.toByteArray(((TextBody) body).getInputStream()).length > maxSize ) { return false; } } catch (IOException e) { return false; } // true, if nothing wrong return true; }
Example #16
Source File: MessageStoreImpl.java From sling-samples with Apache License 2.0 | 5 votes |
/** * code taken from http://www.mozgoweb.com/posts/how-to-parse-mime-message-using-mime4j-library/ */ static String getTextPart(Entity part) throws IOException { TextBody tb = (TextBody) part.getBody(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); tb.writeTo(baos); return baos.toString(MailArchiveServerConstants.DEFAULT_ENCODER.charset().name()); }
Example #17
Source File: MIMEMessageConverter.java From james-project with Apache License 2.0 | 4 votes |
private TextBody createTextBody(CreationMessage newMessage) { String body = newMessage.getHtmlBody() .orElse(newMessage.getTextBody() .orElse("")); return bodyFactory.textBody(body, StandardCharsets.UTF_8); }
Example #18
Source File: MboxReader.java From baleen with Apache License 2.0 | 4 votes |
/** Process body of message as plain text */ private void processTextBody(JCas jCas, TextBody textBody) throws IOException { String text = CharStreams.toString(textBody.getReader()); jCas.setDocumentText(text.trim()); }
Example #19
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 #20
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 #21
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)); }
Example #22
Source File: MessageContentExtractor.java From james-project with Apache License 2.0 | 4 votes |
private Optional<String> asString(TextBody textBody) throws IOException { return Optional.ofNullable(IOUtils.toString(textBody.getInputStream(), charset(Optional.ofNullable(textBody.getMimeCharset())))); }
Example #23
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); } }