camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1445073 - in /camel/trunk/components/camel-mail/src: main/java/org/apache/camel/component/mail/MailProducer.java test/java/org/apache/camel/component/mail/MailProducerTest.java
Date Tue, 12 Feb 2013 09:18:56 GMT
Author: davsclaus
Date: Tue Feb 12 09:18:55 2013
New Revision: 1445073

URL: http://svn.apache.org/r1445073
Log:
CAMEL-6063: Mail producer now detects if the message body is a MimeMessage and send that as
is. Thanks to Thomas Vautrin for the patch.

Modified:
    camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
    camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerTest.java

Modified: camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java?rev=1445073&r1=1445072&r2=1445073&view=diff
==============================================================================
--- camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
(original)
+++ camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
Tue Feb 12 09:18:55 2013
@@ -27,8 +27,6 @@ import org.slf4j.LoggerFactory;
 
 /**
  * A Producer to send messages using JavaMail.
- *  
- * @version 
  */
 public class MailProducer extends DefaultProducer {
     private static final transient Logger LOG = LoggerFactory.getLogger(MailProducer.class);
@@ -40,9 +38,18 @@ public class MailProducer extends Defaul
     }
 
     public void process(final Exchange exchange) {
-        MimeMessage mimeMessage = new MimeMessage(sender.getSession());
         try {
-            getEndpoint().getBinding().populateMailMessage(getEndpoint(), mimeMessage, exchange);
+            MimeMessage mimeMessage;
+
+            final Object body = exchange.getIn().getBody();
+            if (body instanceof MimeMessage) {
+                // Body is directly a MimeMessage
+                mimeMessage = (MimeMessage) body;
+            } else {
+                // Create a message with exchange data
+                mimeMessage = new MimeMessage(sender.getSession());
+                getEndpoint().getBinding().populateMailMessage(getEndpoint(), mimeMessage,
exchange);
+            }
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Sending MimeMessage: {}", MailUtils.dumpMessage(mimeMessage));
             }
@@ -55,7 +62,7 @@ public class MailProducer extends Defaul
             exchange.setException(e);
         }
     }
-    
+
     @Override
     public MailEndpoint getEndpoint() {
         return (MailEndpoint) super.getEndpoint();

Modified: camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerTest.java?rev=1445073&r1=1445072&r2=1445073&view=diff
==============================================================================
--- camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerTest.java
(original)
+++ camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerTest.java
Tue Feb 12 09:18:55 2013
@@ -16,6 +16,12 @@
  */
 package org.apache.camel.component.mail;
 
+import javax.mail.Address;
+import javax.mail.Message.RecipientType;
+import javax.mail.Session;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.test.junit4.CamelTestSupport;
@@ -38,6 +44,34 @@ public class MailProducerTest extends Ca
         Mailbox box = Mailbox.get("someone@localhost");
         assertEquals(1, box.size());
     }
+    
+    @Test
+    public void testProducerBodyIsMimeMessage() throws Exception {
+        Mailbox.clearAll();
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        Address from = new InternetAddress("fromCamelTest@localhost");
+        Address to = new InternetAddress("recipient2@localhost");
+        Session session = Session.getDefaultInstance(System.getProperties());
+        MimeMessage mimeMessage = new MimeMessage(session);
+        mimeMessage.setFrom(from);
+        mimeMessage.addRecipient(RecipientType.TO, to);
+        mimeMessage.setSubject("This is the subject.");
+        mimeMessage.setText("This is the message");        
+        
+        template.sendBodyAndHeader("direct:start", mimeMessage, "To", "someone@localhost");
+        assertMockEndpointsSatisfied();
+        // need to check the message header
+        Exchange exchange = getMockEndpoint("mock:result").getExchanges().get(0);
+        assertNotNull("The message id should not be null", exchange.getIn().getHeader(MailConstants.MAIL_MESSAGE_ID));
+
+        Mailbox box = Mailbox.get("someone@localhost");
+        assertEquals(0, box.size());
+        
+        // Check if the mimeMessagea has override body and headers
+        Mailbox box2 = Mailbox.get("recipient2@localhost");
+        assertEquals(1, box2.size());
+    }
 
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {



Mime
View raw message