Repository: camel Updated Branches: refs/heads/master ed7fd8954 -> d88b7c379
CAMEL-10201: More Type Converters for Multipart Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d88b7c37 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d88b7c37 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d88b7c37 Branch: refs/heads/master Commit: d88b7c3791c69203a1bb0cec4dcf5cab5f6d6d75 Parents: ed7fd89 Author: Stephan Siano <[email protected]> Authored: Fri Jul 29 08:17:25 2016 +0200 Committer: Stephan Siano <[email protected]> Committed: Fri Jul 29 08:18:59 2016 +0200 ---------------------------------------------------------------------- .../camel/component/mail/MailConverters.java | 25 +++++++++++++++-- .../component/mail/MailConvertersTest.java | 29 ++++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/d88b7c37/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 d98c4f0..c61d516 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 @@ -35,9 +35,12 @@ import javax.mail.search.SearchTerm; import com.sun.mail.imap.SortTerm; import org.apache.camel.Converter; +import org.apache.camel.Exchange; +import org.apache.camel.FallbackConverter; import org.apache.camel.NoTypeConversionAvailableException; import org.apache.camel.TypeConverter; import org.apache.camel.converter.IOConverter; +import org.apache.camel.spi.TypeConverterRegistry; /** * JavaMail specific converters. @@ -104,12 +107,30 @@ public final class MailConverters { * must be text based (ie start with text). Can return null. */ @Converter - public static InputStream toInputStream(Multipart multipart) throws IOException, MessagingException { + public static InputStream toInputStream(Multipart multipart, Exchange exchange) throws IOException, MessagingException { String s = toString(multipart); if (s == null) { return null; } - return IOConverter.toInputStream(s, null); + return IOConverter.toInputStream(s, exchange); + } + + /** + * Converts a JavaMail multipart into a body of any type a String can be + * converted into. The content-type of the part must be text based. + */ + @FallbackConverter + public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) throws MessagingException, IOException { + if (Multipart.class.isAssignableFrom(value.getClass())) { + TypeConverter tc = registry.lookup(type, String.class); + if (tc != null) { + String s = toString((Multipart)value); + if (s != null) { + return tc.convertTo(type, s); + } + } + } + return null; } public static SearchTerm toSearchTerm(SimpleSearchTerm simple, TypeConverter typeConverter) throws ParseException, NoTypeConversionAvailableException { http://git-wip-us.apache.org/repos/asf/camel/blob/d88b7c37/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailConvertersTest.java ---------------------------------------------------------------------- diff --git a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailConvertersTest.java b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailConvertersTest.java index cca2871..30c3465 100644 --- a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailConvertersTest.java +++ b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailConvertersTest.java @@ -90,9 +90,34 @@ public class MailConvertersTest extends CamelTestSupport { assertNotNull(mailMessage); Object content = mailMessage.getContent(); - Multipart mp = assertIsInstanceOf(Multipart.class, content); + assertIsInstanceOf(Multipart.class, content); + + InputStream is = mock.getReceivedExchanges().get(0).getIn().getBody(InputStream.class); + assertNotNull(is); + assertEquals("Alternative World", context.getTypeConverter().convertTo(String.class, is)); + } + + @Test + public void testMultipartToByteArray() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + + template.send("direct:a", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody("Hello World"); + exchange.getIn().setHeader(MailConstants.MAIL_ALTERNATIVE_BODY, "Alternative World"); + } + }); + + assertMockEndpointsSatisfied(); + + Message mailMessage = mock.getReceivedExchanges().get(0).getIn().getBody(MailMessage.class).getMessage(); + assertNotNull(mailMessage); + + Object content = mailMessage.getContent(); + assertIsInstanceOf(Multipart.class, content); - InputStream is = MailConverters.toInputStream(mp); + byte[] is = mock.getReceivedExchanges().get(0).getIn().getBody(byte[].class); assertNotNull(is); assertEquals("Alternative World", context.getTypeConverter().convertTo(String.class, is)); }
