From 1d78a113efb047abee8274cfc8c157d23042f067 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Thu, 20 Jan 2011 12:05:15 -0500 Subject: [PATCH] INT-1713 added initial support for sending a Mail message with content type --- spring-integration-mail/.springBeans | 3 +- .../integration/mail/MailHeaders.java | 2 + .../mail/MailSendingMessageHandler.java | 23 +- .../MessageWithContentTypeTests-context.xml | 41 + .../config/MessageWithContentTypeTests.java | 100 ++ .../integration/mail/config/test.html | 940 ++++++++++++++++++ 6 files changed, 1106 insertions(+), 3 deletions(-) create mode 100644 spring-integration-mail/src/test/java/org/springframework/integration/mail/config/MessageWithContentTypeTests-context.xml create mode 100644 spring-integration-mail/src/test/java/org/springframework/integration/mail/config/MessageWithContentTypeTests.java create mode 100644 spring-integration-mail/src/test/java/org/springframework/integration/mail/config/test.html diff --git a/spring-integration-mail/.springBeans b/spring-integration-mail/.springBeans index a5e8c77289..5a41740f54 100644 --- a/spring-integration-mail/.springBeans +++ b/spring-integration-mail/.springBeans @@ -1,13 +1,14 @@ 1 - + src/test/java/org/springframework/integration/mail/config/MailOutboundWithJavamailProperties-context.xml + src/test/java/org/springframework/integration/mail/config/MessageWithContentTypeTests-context.xml diff --git a/spring-integration-mail/src/main/java/org/springframework/integration/mail/MailHeaders.java b/spring-integration-mail/src/main/java/org/springframework/integration/mail/MailHeaders.java index 37b22ec838..59c5c7cc5d 100644 --- a/spring-integration-mail/src/main/java/org/springframework/integration/mail/MailHeaders.java +++ b/spring-integration-mail/src/main/java/org/springframework/integration/mail/MailHeaders.java @@ -41,5 +41,7 @@ public abstract class MailHeaders { public static final String MULTIPART_MODE = PREFIX + "multipartMode"; public static final String ATTACHMENT_FILENAME = PREFIX + "attachmentFilename"; + + public static final String CONTENT_TYPE = PREFIX + "contentType"; } diff --git a/spring-integration-mail/src/main/java/org/springframework/integration/mail/MailSendingMessageHandler.java b/spring-integration-mail/src/main/java/org/springframework/integration/mail/MailSendingMessageHandler.java index 3e0da21904..40d4726cd0 100644 --- a/spring-integration-mail/src/main/java/org/springframework/integration/mail/MailSendingMessageHandler.java +++ b/spring-integration-mail/src/main/java/org/springframework/integration/mail/MailSendingMessageHandler.java @@ -17,6 +17,7 @@ package org.springframework.integration.mail; import javax.mail.MessagingException; +import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import org.springframework.core.io.ByteArrayResource; @@ -91,8 +92,14 @@ public class MailSendingMessageHandler extends AbstractMessageHandler { mailMessage = this.createMailMessageFromByteArrayMessage((Message) message); } else if (message.getPayload() instanceof String) { - mailMessage = new SimpleMailMessage(); - mailMessage.setText((String) message.getPayload()); + String contentType = (String) message.getHeaders().get(MailHeaders.CONTENT_TYPE); + if (StringUtils.hasText(contentType)){ + mailMessage = this.createMailMessageWithContentType((Message) message, contentType); + } + else { + mailMessage = new SimpleMailMessage(); + mailMessage.setText((String) message.getPayload()); + } } else { throw new MessageHandlingException(message, "Unable to create MailMessage from payload type [" @@ -101,6 +108,18 @@ public class MailSendingMessageHandler extends AbstractMessageHandler { this.applyHeadersToMailMessage(mailMessage, message.getHeaders()); return mailMessage; } + + private MailMessage createMailMessageWithContentType(Message message, String contentType){ + MimeMessage mimeMessage = this.mailSender.createMimeMessage(); + try { + mimeMessage.setContent(message.getPayload(), contentType); + return new MimeMailMessage(mimeMessage); + } + catch (Exception e) { + throw new org.springframework.integration.MessagingException("Failed to creaet MimeMessage with contentType: " + + contentType, e); + } + } private MailMessage createMailMessageFromByteArrayMessage(Message message) { String attachmentFileName = message.getHeaders().get(MailHeaders.ATTACHMENT_FILENAME, String.class); diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/MessageWithContentTypeTests-context.xml b/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/MessageWithContentTypeTests-context.xml new file mode 100644 index 0000000000..eafee38c7f --- /dev/null +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/MessageWithContentTypeTests-context.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + true + true + true + + + diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/MessageWithContentTypeTests.java b/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/MessageWithContentTypeTests.java new file mode 100644 index 0000000000..ae220dbc8d --- /dev/null +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/MessageWithContentTypeTests.java @@ -0,0 +1,100 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed 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.springframework.integration.mail.config; + +import static junit.framework.Assert.assertEquals; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.FileReader; +import java.io.StringWriter; +import java.util.Properties; + +import javax.mail.Session; +import javax.mail.internet.MimeMessage; + +import org.junit.Ignore; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.Message; +import org.springframework.integration.MessageChannel; +import org.springframework.integration.mail.MailHeaders; +import org.springframework.integration.mail.MailSendingMessageHandler; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.support.MessageBuilder; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.util.FileCopyUtils; + +/** + * @author Oleg Zhurakousky + * + */ +public class MessageWithContentTypeTests { + + @Test + @Ignore + public void testSendEmail() throws Exception{ + ApplicationContext ac = new ClassPathXmlApplicationContext("MessageWithContentTypeTests-context.xml", this.getClass()); + MessageChannel inputChannel = ac.getBean("inputChannel", MessageChannel.class); + StringWriter writer = new StringWriter(); + FileReader reader = new FileReader("src/test/java/org/springframework/integration/mail/config/test.html"); + FileCopyUtils.copy(reader, writer); + inputChannel.send(new GenericMessage(writer.getBuffer().toString())); + } + + @Test + public void testMessageConversionWithHtmlAndContentType() throws Exception{ + JavaMailSender sender = mock(JavaMailSender.class); + MailSendingMessageHandler handler = new MailSendingMessageHandler(sender); + StringWriter writer = new StringWriter(); + FileReader reader = new FileReader("test.html"); + FileCopyUtils.copy(reader, writer); + Message message = MessageBuilder.withPayload(writer.getBuffer().toString()) + .setHeader(MailHeaders.TO, "to") + .setHeader(MailHeaders.FROM, "from") + .setHeader(MailHeaders.CONTENT_TYPE, "text/html") + .build(); + MimeMessage mMessage = new TestMimeMessage(); + // MOCKS + when(sender.createMimeMessage()).thenReturn(mMessage); + doAnswer(new Answer() { + public Object answer(InvocationOnMock invocation) throws Throwable { + MimeMessage mimeMessage = (MimeMessage) invocation.getArguments()[0]; + assertEquals("text/html", mimeMessage.getDataHandler().getContentType()); + return null; + } + }).when(sender).send(Mockito.any(MimeMessage.class)); + + // handle message + handler.handleMessage(message); + + verify(sender, times(1)).send(Mockito.any(MimeMessage.class)); + } + + private static class TestMimeMessage extends MimeMessage{ + public TestMimeMessage() { + super(Session.getDefaultInstance(new Properties())); + } + } +} diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/test.html b/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/test.html new file mode 100644 index 0000000000..fc67570a30 --- /dev/null +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/test.html @@ -0,0 +1,940 @@ + + + + + + + + + + + + + + + + + + + + Twitter + + + + + + + + + + + + + + + +
+
+ + +
+
+ + + + + + + + +
+
+

See who’s here

+
    +
  • + + +
    +
    + nate_robinson +
    Nate Robinson 
    + @nate_robinson + Rain city ( word aapp ) | in Sports +
    + +
    +
    Recently tweeted:
    +
    What up y'all ?
    +
    about 10 hours ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + ev +
    Evan Williams 
    + @ev + San Francisco, CA, US | in Business +
    + +
    +
    Recently tweeted:
    +
    @jeanpaul thanks, man.
    +
    7:28 PM Jan 18th
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + nytimes +
    The New York Times 
    + @nytimes + New York, NY | in News +
    + +
    +
    Recently tweeted:
    +
    The Caucus: Chinese President May Get Earful From Lawmakers http://nyti.ms/gVqqut
    +
    20 minutes ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + therealKDUB20 +
    Kyle Wilson 
    + @therealKDUB20 + New York | in Staff Picks: NFL Playoffs +
    + +
    +
    Recently tweeted:
    +
    @tonylogan85 school start yet?
    +
    1:22 PM Jan 18th
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + Oxfam +
    Oxfam International 
    + @Oxfam + in Charity +
    + +
    +
    Recently tweeted:
    +
    Oxfam calls on international donors to fund upcoming @UN $51mn appeal for #SriLanka #floods http://oxf.am/ZMm
    +
    about 2 hours ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + grantimahara +
    Grant Imahara 
    + @grantimahara + San Francisco, CA | in Science +
    + +
    +
    Recently tweeted:
    +
    @bergopolis Ha, thanks!!
    +
    about 8 hours ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + chelseahandler +
    Chelsea Handler 
    + @chelseahandler + Los Angeles, CA | in Entertainment +
    + +
    +
    Recently tweeted:
    +
    @qnzfrogy03 sorry about that. Makes me very happy to send gays and dogs
    +
    about 8 hours ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + kevin_nealon +
    Kevin Nealon 
    + @kevin_nealon + Los Angeles, Ca. | in Funny +
    + +
    +
    Recently tweeted:
    +
    People on Segways are show-offs.
    +
    about 14 hours ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + healthfinder +
    healthfinder.gov 
    + @healthfinder + Washington, DC | in Health +
    + +
    +
    Recently tweeted:
    +
    Fight the #flu. Stay away from people who are sick, wash your hands, and get enough sleep. More quick tips: http://bit.ly/dN812J.
    +
    about 17 hours ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + kevinrose +
    Kevin Rose 
    + @kevinrose + San Francisco, CA | in Technology +
    + +
    +
    Recently tweeted:
    +
    RT @TheOnion: BREAKING #NEWS: Chinese President Hu Jintao Pays For #StateDinner While President Obama In Bathroom
    +
    about 13 hours ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + azizansari +
    Aziz Ansari 
    + @azizansari + Los Angeles, CA | in Staff Picks +
    + +
    +
    Recently tweeted:
    +
    Hey tomorrow I am doing a Twitter Q&A to celebrate the return of Parks & Rec! Starts at noon PST/3pm EST til 4. Use hashtag #AskAziz.
    +
    about 13 hours ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + SimpleMom +
    Tsh Oxenreider 
    + @SimpleMom + Austin | in Family +
    + +
    +
    Recently tweeted:
    +
    @SandyCoughlinRE Glad you like it! I've been thinking about adding Twitterfeed to it, but I don't want to bomboard people w/ my links...
    +
    43 minutes ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + BoltBus +
    BoltBus 
    + @BoltBus + Northeast | in Travel +
    + +
    +
    Recently tweeted:
    +
    @drjwalk All customers received a text message or email. Perhaps you gave the wrong email? Please contact customer service at 1-877-BOLTBUS.
    +
    about 21 hours ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + ArchRecord +
    Architectural Record 
    + @ArchRecord + New York City | in Art & Design +
    + +
    +
    Recently tweeted:
    +
    Our latest Newsmaker Interview with #AIA Gold Medal Winner Fumihiko Maki http://ht.ly/3GzWs
    +
    about 22 hours ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + gtdguy +
    David Allen 
    + @gtdguy + Ojai, California | in Books +
    + +
    +
    Recently tweeted:
    +
    Can't help loving arriving in NYC, no matter the weather. Maybe the crispness, in many forms, on many levels.
    +
    8:55 PM Jan 18th
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + GaelGreene +
    Gael Greene 
    + @GaelGreene + New York City | in Food & Drink +
    + +
    +
    Recently tweeted:
    +
    @ProfMTH Yes,tourist class.The Seminar paid 4 us both & it was just 2 1/2 hours.So I was a good sport. Do u think they paid business 4 Ruth?
    +
    about 16 hours ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + CoryBooker +
    Cory Booker 
    + @CoryBooker + Newark, NJ | in Politics +
    + +
    +
    Recently tweeted:
    +
    Thanks! I will. RT @GlenistraBR: Thats fine you have 24 whole hrs,b4 you miss a day of exercise, fit it in#letsmove
    +
    about 1 hour ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + amazondeals +
    Amazon.com Deals 
    + @amazondeals + Seattle, Washington | in Deals +
    + +
    +
    Recently tweeted:
    +
    Lightning Deal! $109.99 - TomTom XXL 540T 5-Inch Widescreen Portable GPS Navigator (Lifetime Traffic Edition) http://amzn.to/GoldboxDeals
    +
    about 1 hour ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + Support +
    Support 
    + @Support + Twitter HQ | in Twitter +
    + +
    +
    Recently tweeted:
    +
    Users may experience difficulty when filing a ticket in our Help Center or commenting on an article. We're working to resolve these issues.
    +
    about 14 hours ago
    +
    + +
     
    +
    +
  • +
  • + + +
    +
    + _BoF_ +
    Business of Fashion 
    + @_BoF_ + London, New York, Tokyo, Paris | in Fashion +
    + +
    +
    Recently tweeted:
    +
    @fashedatlarge Love Chisou also, the original location on Princes St near Hanover Sq. Have you tried the Horenso Salad? It's amazing.
    +
    about 1 hour ago
    +
    + +
     
    +
    +
  • + +
+
Friends and industry peers you know. Celebrities you watch. Businesses you frequent. Find them all on Twitter.
+
+
+ + +
+
+
+

Top Tweets View all

+ + + + + +
+ +
+
+ + +
+
+ + + +
+
+
Trending right now:
+ +
+ Why? + + Source: What the Trend? +
+
+
 
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + \ No newline at end of file