org.apache.james.mime4j.message.BodyPart Java Examples
The following examples show how to use
org.apache.james.mime4j.message.BodyPart.
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: MessageStoreImpl.java From sling-samples with Apache License 2.0 | 6 votes |
static void recursiveMultipartProcessing(Multipart multipart, StringBuilder plainBody, StringBuilder htmlBody, Boolean hasBody, List<BodyPart> attachments) throws IOException { for (Entity enitiy : multipart.getBodyParts()) { BodyPart part = (BodyPart) enitiy; if (part.getDispositionType() != null && !part.getDispositionType().equals("")) { // if DispositionType is null or empty, it means that it's multipart, not attached file attachments.add(part); } else { if (part.isMimeType(PLAIN_MIMETYPE) && !hasBody) { plainBody.append(getTextPart(part)); hasBody = true; } else if (part.isMimeType(HTML_MIMETYPE) && !hasBody) { htmlBody.append(getTextPart(part)); } else if (part.isMultipart()) { recursiveMultipartProcessing((Multipart) part.getBody(), plainBody, htmlBody, hasBody, attachments); } } } }
Example #2
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void extractShouldRetrieveTextBodyAndHtmlBodyWhenTextBodyInMainMultipartAndHtmlBodyInInnerMultipart() throws IOException { BodyPart multipartRelated = BodyPartBuilder.create() .setBody(MultipartBuilder.create("related") .addBodyPart(htmlPart) .addBodyPart(inlineImage) .build()) .build(); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(textPart) .addBodyPart(multipartRelated) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).contains(TEXT_CONTENT); assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
Example #3
Source File: MessageStoreImplAttachmentsTest.java From sling-samples with Apache License 2.0 | 6 votes |
@Test public void simpleMultipartMessageTest() throws IOException { Multipart multipart = new MultipartImpl("mixed"); BodyPart att0 = createTextBody("This is the first part of the template..", "plain", true); multipart.addBodyPart(att0); BodyPart att1 = createRandomBinaryAttachment(200); multipart.addBodyPart(att1); BodyPart att2 = createRandomBinaryAttachment(300); multipart.addBodyPart(att2); BodyPart att3 = createTextBody("Some sample text here...?!", "html", true); multipart.addBodyPart(att3); BodyPart att4 = createRandomBinaryAttachment(100); multipart.addBodyPart(att4); BodyPart att5 = createTextBody("Some other text here...?!", "plain", true); multipart.addBodyPart(att5); MessageImpl message = new MessageImpl(); message.setMultipart(multipart); message.setSubject("Template message"); message.setDate(new Date()); message.getHeader().setField(new RawField(LIST_ID, "<list.example.com>")); assertSaveMessageWithAttachments(message, 6); }
Example #4
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void extractShouldRetrieveTextAndHtmlBodyWhenOneInlinedTextAttachmentAndMainContentInMultipart() throws IOException { BodyPart multipartAlternative = BodyPartBuilder.create() .setBody(MultipartBuilder.create("alternative") .addBodyPart(textPart) .addBodyPart(htmlPart) .build()) .build(); Multipart multipartMixed = MultipartBuilder.create("mixed") .addBodyPart(multipartAlternative) .addBodyPart(inlineText) .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); }
Example #5
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void extractShouldNotRetrieveTextBodyWithOneInlinedTextAttachmentWithCid() throws IOException { //Given BodyPart inlinedTextPart = BodyPartBuilder.create() .setBody(TEXT_CONTENT, "text", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/plain; charset=utf-8")); inlinedHeader.addField(CONTENT_ID_FIELD); inlinedTextPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedTextPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getTextBody()).isEmpty(); }
Example #6
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void extractShouldRetrieveTextBodyWithOneInlinedTextAttachmentWithoutCid() throws IOException { //Given BodyPart inlinedTextPart = BodyPartBuilder.create() .setBody(TEXT_CONTENT, "text", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/plain; charset=utf-8")); inlinedTextPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedTextPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getTextBody()).contains(TEXT_CONTENT); }
Example #7
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void extractShouldNotRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithCid() throws IOException { //Given BodyPart inlinedHTMLPart = BodyPartBuilder.create() .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/html; charset=utf-8")); inlinedHeader.addField(CONTENT_ID_FIELD); inlinedHTMLPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedHTMLPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getHtmlBody()).isEmpty(); }
Example #8
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 #9
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@Test void extractShouldRetrieveHtmlBodyWithOneInlinedHTMLAttachmentWithoutCid() throws IOException { //Given BodyPart inlinedHTMLPart = BodyPartBuilder.create() .setBody(HTML_CONTENT, "html", StandardCharsets.UTF_8) .build(); HeaderImpl inlinedHeader = new HeaderImpl(); inlinedHeader.addField(Fields.contentDisposition(MimeMessage.INLINE)); inlinedHeader.addField(Fields.contentType("text/html; charset=utf-8")); inlinedHTMLPart.setHeader(inlinedHeader); Multipart multipartAlternative = MultipartBuilder.create("alternative") .addBodyPart(inlinedHTMLPart) .build(); Message message = Message.Builder.of() .setBody(multipartAlternative) .build(); //When MessageContent actual = testee.extract(message); //Then assertThat(actual.getHtmlBody()).contains(HTML_CONTENT); }
Example #10
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@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); }
Example #11
Source File: MessageContentExtractorTest.java From james-project with Apache License 2.0 | 6 votes |
@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); }
Example #12
Source File: MIMEMessageConverter.java From james-project with Apache License 2.0 | 5 votes |
private BodyPart attachmentBodyPart(MessageAttachmentMetadata att, MailboxSession session) throws IOException, AttachmentNotFoundException { try (InputStream attachmentStream = attachmentContentLoader.load(att.getAttachment(), session)) { BodyPartBuilder builder = BodyPartBuilder.create() .use(bodyFactory) .setBody(new BasicBodyFactory().binaryBody(ByteStreams.toByteArray(attachmentStream))) .setField(contentTypeField(att)) .setField(contentDispositionField(att.isInline())) .setContentTransferEncoding(BASE64); contentId(builder, att); return builder.build(); } }
Example #13
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 #14
Source File: ExtractMDNOriginalJMAPMessageIdTest.java From james-project with Apache License 2.0 | 5 votes |
@Test public void extractReportShouldExtractMDNWhenValidMDN() throws IOException { ExtractMDNOriginalJMAPMessageId testee = new ExtractMDNOriginalJMAPMessageId(mock(MailboxManager.class), mock(UsersRepository.class)); BodyPart mdn = BodyPartBuilder .create() .setBody(SingleBodyBuilder.create() .setText( "Reporting-UA: linagora.com; Evolution 3.26.5-1+b1 \n" + "Final-Recipient: rfc822; [email protected]\n" + "Original-Message-ID: <[email protected]>\n" + "Disposition: manual-action/MDN-sent-manually;displayed\n") .buildText()) .setContentType("message/disposition-notification") .build(); Message message = Message.Builder.of() .setBody(MultipartBuilder.create("report") .addTextPart("first", StandardCharsets.UTF_8) .addBodyPart(mdn) .build()) .build(); assertThat(testee.extractReport(message)) .isNotEmpty() .contains(mdn); }
Example #15
Source File: MessageStoreImplAttachmentsTest.java From sling-samples with Apache License 2.0 | 5 votes |
private static BodyPart createRandomBinaryAttachment(int numberOfBytes) throws IOException { byte[] data = new byte[numberOfBytes]; new Random().nextBytes(data); Body body = new StorageBodyFactory().binaryBody(new ByteArrayInputStream(data)); BodyPart bodyPart = new BodyPart(); bodyPart.setContentDisposition("attachment", "file"+Math.random()); bodyPart.setBody(body, "application/octet-stream"); return bodyPart; }
Example #16
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 #17
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 #18
Source File: MessageStoreImplAttachmentsTest.java From sling-samples with Apache License 2.0 | 5 votes |
@Test public void recursiveMultipartMessageTest() throws IOException { Multipart multipart = new MultipartImpl("mixed"); BodyPart att1 = createRandomBinaryAttachment(100); multipart.addBodyPart(att1); BodyPart att2 = createRandomBinaryAttachment(133); multipart.addBodyPart(att2); Multipart nestedMultipart = new MultipartImpl("mixed"); BodyPart nBody = createTextBody("Some sample text here...?!", "plain", false); nestedMultipart.addBodyPart(nBody); BodyPart nAtt1 = createRandomBinaryAttachment(300); nestedMultipart.addBodyPart(nAtt1); BodyPart NAtt2 = createRandomBinaryAttachment(100); nestedMultipart.addBodyPart(NAtt2); BodyPart nAtt3 = createTextBody("Some other text here...<br>?!", "html", true); nestedMultipart.addBodyPart(nAtt3); BodyPart nestedMessage = new BodyPart(); nestedMessage.setMultipart(nestedMultipart); multipart.addBodyPart(nestedMessage); MessageImpl message = new MessageImpl(); message.setMultipart(multipart); message.setSubject("Template message"); message.setDate(new Date()); message.getHeader().setField(new RawField(LIST_ID, "<list.example.com>")); assertSaveMessageWithAttachments(message, 5); }
Example #19
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 #20
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 #21
Source File: DsnMailCreator.java From mireka with Apache License 2.0 | 4 votes |
private BodyPart originalMessageBodyPart() { BinaryBody body = new MessageContentBody(originalMail.mailData); BodyPart result = new BodyPart(); result.setBody(body, "message/rfc822"); return result; }
Example #22
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 #23
Source File: AttachmentFilter.java From sling-samples with Apache License 2.0 | votes |
boolean isEligible(BodyPart attachment);