Repository: camel Updated Branches: refs/heads/master 07a14ae4c -> 9b69a1660
CAMEL-10302: Extract body from nested multiparts Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/9b69a166 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9b69a166 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9b69a166 Branch: refs/heads/master Commit: 9b69a1660223e6b72fcd6cea62129e70ecc61c82 Parents: 07a14ae Author: Stephan Siano <[email protected]> Authored: Fri Sep 9 10:50:19 2016 +0200 Committer: Stephan Siano <[email protected]> Committed: Fri Sep 9 10:54:02 2016 +0200 ---------------------------------------------------------------------- .../camel/component/mail/MailBinding.java | 11 +- .../camel/component/mail/MailConverters.java | 10 +- .../mail/NestedMimeMessageConsumeTest.java | 98 +++++++++++++ .../src/test/resources/nested-multipart.elm | 138 +++++++++++++++++++ 4 files changed, 255 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/9b69a166/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java ---------------------------------------------------------------------- diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java index fccca6a..ec416e8 100644 --- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java +++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java @@ -22,7 +22,6 @@ import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; import java.util.ArrayList; import java.util.Enumeration; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -614,8 +613,18 @@ public class MailBinding { int size = multipart.getCount(); for (int i = 0; i < size; i++) { BodyPart part = multipart.getBodyPart(i); + content = part.getContent(); + // in case of nested multiparts iterate into them + while (content instanceof MimeMultipart) { + if (multipart.getCount() < 1) { + break; + } + part = ((MimeMultipart)content).getBodyPart(0); + content = part.getContent(); + } if (part.getContentType().toLowerCase().startsWith("text")) { answer.put(Exchange.CONTENT_TYPE, part.getContentType()); + break; } } } http://git-wip-us.apache.org/repos/asf/camel/blob/9b69a166/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java ---------------------------------------------------------------------- diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java index c61d516..0b7e84c 100644 --- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java +++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java @@ -65,7 +65,7 @@ public final class MailConverters { @Converter public static String toString(Message message) throws MessagingException, IOException { Object content = message.getContent(); - if (content instanceof MimeMultipart) { + while (content instanceof MimeMultipart) { MimeMultipart multipart = (MimeMultipart) content; if (multipart.getCount() > 0) { BodyPart part = multipart.getBodyPart(0); @@ -87,6 +87,14 @@ public final class MailConverters { int size = multipart.getCount(); for (int i = 0; i < size; i++) { BodyPart part = multipart.getBodyPart(i); + Object content = part.getContent(); + while (content instanceof MimeMultipart) { + if (multipart.getCount() < 1) { + break; + } + part = ((MimeMultipart)content).getBodyPart(0); + content = part.getContent(); + } if (part.getContentType().toLowerCase().startsWith("text")) { return part.getContent().toString(); } http://git-wip-us.apache.org/repos/asf/camel/blob/9b69a166/components/camel-mail/src/test/java/org/apache/camel/component/mail/NestedMimeMessageConsumeTest.java ---------------------------------------------------------------------- diff --git a/components/camel-mail/src/test/java/org/apache/camel/component/mail/NestedMimeMessageConsumeTest.java b/components/camel-mail/src/test/java/org/apache/camel/component/mail/NestedMimeMessageConsumeTest.java new file mode 100644 index 0000000..8f25c35 --- /dev/null +++ b/components/camel-mail/src/test/java/org/apache/camel/component/mail/NestedMimeMessageConsumeTest.java @@ -0,0 +1,98 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.mail; + +import java.io.InputStream; +import java.util.Set; + +import javax.activation.DataHandler; +import javax.mail.Folder; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Store; +import javax.mail.internet.MimeMessage; + +import org.apache.camel.Attachment; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; +import org.jvnet.mock_javamail.Mailbox; + +import static org.hamcrest.core.AnyOf.anyOf; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.StringContains.containsString; + +public class NestedMimeMessageConsumeTest extends CamelTestSupport { + + @Test + public void testNestedMultipart() throws Exception { + Mailbox.clearAll(); + + MockEndpoint resultEndpoint = getMockEndpoint("mock:result"); + resultEndpoint.expectedMinimumMessageCount(1); + + prepareMailbox("james3"); + + resultEndpoint.assertIsSatisfied(); + + Exchange exchange = resultEndpoint.getReceivedExchanges().get(0); + String text = exchange.getIn().getBody(String.class); + assertThat(text, containsString("Test with bold face, pictures and attachments")); + assertEquals("text/plain; charset=us-ascii", exchange.getIn().getHeader("Content-Type")); + + Set<String> attachmentNames = exchange.getIn().getAttachmentNames(); + assertNotNull("attachments got lost", attachmentNames); + assertEquals(2, attachmentNames.size()); + for (String s : attachmentNames) { + Attachment att = exchange.getIn().getAttachmentObject(s); + DataHandler dh = att.getDataHandler(); + Object content = dh.getContent(); + assertNotNull("Content should not be empty", content); + assertThat(dh.getName(), anyOf(equalTo("image001.png"), equalTo("test.txt"))); + } + } + + private void prepareMailbox(String user) throws MessagingException { + // connect to mailbox + JavaMailSender sender = new DefaultJavaMailSender(); + Store store = sender.getSession().getStore("pop3"); + store.connect("localhost", 25, user, "secret"); + Folder folder = store.getFolder("INBOX"); + folder.open(Folder.READ_WRITE); + folder.expunge(); + + InputStream is = getClass().getResourceAsStream("/nested-multipart.elm"); + Message hurzMsg = new MimeMessage(sender.getSession(), is); + Message[] messages = new Message[] {hurzMsg}; + + // insert one signed message + folder.appendMessages(messages); + folder.close(true); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("pop3://james3@localhost?consumer.delay=1000").removeHeader("to").to("smtp://james4@localhost"); + from("pop3://james4@localhost?consumer.delay=2000").convertBodyTo(String.class).to("mock:result"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/9b69a166/components/camel-mail/src/test/resources/nested-multipart.elm ---------------------------------------------------------------------- diff --git a/components/camel-mail/src/test/resources/nested-multipart.elm b/components/camel-mail/src/test/resources/nested-multipart.elm new file mode 100644 index 0000000..afaee20 --- /dev/null +++ b/components/camel-mail/src/test/resources/nested-multipart.elm @@ -0,0 +1,138 @@ +From: "Siano, Stephan" <[email protected]> +To: Stephan Siano <[email protected]> +Subject: Test Mail +Thread-Topic: Test Mail +Thread-Index: AdIJvUzrLPnYH+DfQHWP0a78iiFL1w== +Date: Thu, 8 Sep 2016 10:44:22 +0000 +Message-ID: <[email protected]> +Accept-Language: en-US +Content-Language: en-US +X-MS-Has-Attach: yes +X-MS-TNEF-Correlator: +x-ms-exchange-transport-fromentityheader: Hosted +x-originating-ip: [10.10.10.10] +Content-Type: multipart/mixed; + boundary="_006_1badc7031641466588958cd33857479aDEWDFE13DE39globalcorps_" +MIME-Version: 1.0 +Envelope-To: <[email protected]> + +--_006_1badc7031641466588958cd33857479aDEWDFE13DE39globalcorps_ +Content-Type: multipart/related; + boundary="_005_1badc7031641466588958cd33857479aDEWDFE13DE39globalcorps_"; + type="multipart/alternative" + +--_005_1badc7031641466588958cd33857479aDEWDFE13DE39globalcorps_ +Content-Type: multipart/alternative; + boundary="_000_1badc7031641466588958cd33857479aDEWDFE13DE39globalcorps_" + +--_000_1badc7031641466588958cd33857479aDEWDFE13DE39globalcorps_ +Content-Type: text/plain; charset=us-ascii +Content-Transfer-Encoding: quoted-printable + +Test with bold face, pictures and attachments + +[cid:[email protected]] + + +--_000_1badc7031641466588958cd33857479aDEWDFE13DE39globalcorps_ +Content-Type: text/html; charset="us-ascii" +Content-Transfer-Encoding: quoted-printable + +<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr= +osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" = +xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:= +//www.w3.org/TR/REC-html40"> +<head> +<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"= +> +<meta name=3D"Generator" content=3D"Microsoft Word 15 (filtered medium)"> +<!--[if !mso]><style>v\:* {behavior:url(#default#VML);} +o\:* {behavior:url(#default#VML);} +w\:* {behavior:url(#default#VML);} +.shape {behavior:url(#default#VML);} +</style><![endif]--><style><!-- +/* Font Definitions */ +@font-face + {font-family:"Cambria Math"; + panose-1:2 4 5 3 5 4 6 3 2 4;} +@font-face + {font-family:Calibri; + panose-1:2 15 5 2 2 2 4 3 2 4;} +/* Style Definitions */ +p.MsoNormal, li.MsoNormal, div.MsoNormal + {margin:0cm; + margin-bottom:.0001pt; + font-size:11.0pt; + font-family:"Calibri",sans-serif;} +a:link, span.MsoHyperlink + {mso-style-priority:99; + color:#0563C1; + text-decoration:underline;} +a:visited, span.MsoHyperlinkFollowed + {mso-style-priority:99; + color:#954F72; + text-decoration:underline;} +span.EmailStyle17 + {mso-style-type:personal; + font-family:"Calibri",sans-serif; + color:windowtext;} +.MsoChpDefault + {mso-style-type:export-only; + font-family:"Calibri",sans-serif;} +@page WordSection1 + {size:612.0pt 792.0pt; + margin:70.85pt 70.85pt 2.0cm 70.85pt;} +div.WordSection1 + {page:WordSection1;} +--></style><!--[if gte mso 9]><xml> +<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" /> +</xml><![endif]--><!--[if gte mso 9]><xml> +<o:shapelayout v:ext=3D"edit"> +<o:idmap v:ext=3D"edit" data=3D"1" /> +</o:shapelayout></xml><![endif]--> +</head> +<body lang=3D"EN-US" link=3D"#0563C1" vlink=3D"#954F72"> +<div class=3D"WordSection1"> +<p class=3D"MsoNormal">Test with <b>bold</b> face, pictures and attachments= +<o:p></o:p></p> +<p class=3D"MsoNormal"><o:p> </o:p></p> +<p class=3D"MsoNormal"><img width=3D"65" height=3D"43" style=3D"width:.677i= +n;height:.4479in" id=3D"Picture_x0020_1" src=3D"cid:[email protected]= +B9B87E0"><o:p></o:p></p> +<p class=3D"MsoNormal"><o:p> </o:p></p> +</div> +</body> +</html> + +--_000_1badc7031641466588958cd33857479aDEWDFE13DE39globalcorps_-- + +--_005_1badc7031641466588958cd33857479aDEWDFE13DE39globalcorps_ +Content-Type: image/png; name="image001.png" +Content-Description: image001.png +Content-Disposition: inline; filename="image001.png"; size=300; + creation-date="Thu, 08 Sep 2016 10:44:22 GMT"; + modification-date="Thu, 08 Sep 2016 10:44:22 GMT" +Content-ID: <[email protected]> +Content-Transfer-Encoding: base64 + +iVBORw0KGgoAAAANSUhEUgAAAEEAAAArCAIAAACo+nIuAAAAAXNSR0IArs4c6QAAAARnQU1BAACx +jwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADBSURBVGhD7ZPLDYQwDAWpi4Koh2pohmJCvGCC +kv0QwmHQvjkR20hvTOjC85EDAzkwkAMDOTD4J4ehKxmmrXmKeewr3zhL5XewIP04b6cqyA5WclLG +QzXOH2eu7uAzrQ5TvGSePDXtKd/5u9o9tDnkStHodbR6HpjskOFdb+3TZIfvudJdozoU54KU/Ofo +ZVr/6bWUWJu2fWffvQ/eLlLpgEQODOTAQA4M5MBADgzkwEAODOTAQA4M5MBADgye7xDCAheO16uO +CgbLAAAAAElFTkSuQmCC + +--_005_1badc7031641466588958cd33857479aDEWDFE13DE39globalcorps_-- + +--_006_1badc7031641466588958cd33857479aDEWDFE13DE39globalcorps_ +Content-Type: text/plain; name="test.txt" +Content-Description: test.txt +Content-Disposition: attachment; filename="test.txt"; size=4; + creation-date="Thu, 08 Sep 2016 10:43:33 GMT"; + modification-date="Thu, 08 Sep 2016 10:43:45 GMT" +Content-Transfer-Encoding: base64 + +dGVzdA== + +--_006_1badc7031641466588958cd33857479aDEWDFE13DE39globalcorps_-- +
