camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject [camel] branch camel-2.x updated: Pick up recipient headers from configuration for camel-mail, if they are not first specified as a header
Date Wed, 06 Feb 2019 16:04:04 GMT
This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch camel-2.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.x by this push:
     new 4aa04cd  Pick up recipient headers from configuration for camel-mail, if they are
not first specified as a header
4aa04cd is described below

commit 4aa04cdccd85e6e6f4083b8ce382ca4034aca332
Author: Colm O hEigeartaigh <coheigea@apache.org>
AuthorDate: Wed Feb 6 15:52:32 2019 +0000

    Pick up recipient headers from configuration for camel-mail, if they are not first specified
as a header
---
 .../apache/camel/component/mail/MailBinding.java   | 44 +++++++++++-----------
 .../camel/component/mail/MailRecipientsTest.java   |  7 ++--
 2 files changed, 25 insertions(+), 26 deletions(-)

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 d50a04c..fb99750 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
@@ -81,13 +81,7 @@ public class MailBinding {
     public void populateMailMessage(MailEndpoint endpoint, MimeMessage mimeMessage, Exchange
exchange)
         throws MessagingException, IOException {
 
-        // camel message headers takes precedence over endpoint configuration
-        if (hasRecipientHeaders(exchange)) {
-            setRecipientFromCamelMessage(mimeMessage, endpoint.getConfiguration(), exchange);
-        } else {
-            // fallback to endpoint configuration
-            setRecipientFromEndpointConfiguration(mimeMessage, endpoint, exchange);
-        }
+        setRecipients(mimeMessage, endpoint.getConfiguration(), exchange);
 
         // set the replyTo if it was passed in as an option in the uri. Note: if it is in
both the URI
         // and headers the headers win.
@@ -408,7 +402,11 @@ public class MailBinding {
         }
     }
 
-    private void setRecipientFromCamelMessage(MimeMessage mimeMessage, MailConfiguration
configuration, Exchange exchange) throws MessagingException, IOException {
+    private void setRecipients(MimeMessage mimeMessage, MailConfiguration configuration,
Exchange exchange) throws MessagingException, IOException {
+        // First we check the Message headers for the recipients - they have priority
+        boolean toSet = false;
+        boolean ccSet = false;
+        boolean bccSet = false;
         for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet())
{
             String headerName = entry.getKey();
             Object headerValue = entry.getValue();
@@ -425,25 +423,27 @@ public class MailBinding {
                     appendRecipientToMimeMessage(mimeMessage, configuration, exchange,
                                                  StringHelper.removeCRLF(headerName), asString(exchange,
headerValue));
                 }
+                
+                if (Message.RecipientType.TO.toString().equalsIgnoreCase(headerName)) {
+                    toSet = true;
+                } else if (Message.RecipientType.CC.toString().equalsIgnoreCase(headerName))
{
+                    ccSet = true;
+                } else if (Message.RecipientType.BCC.toString().equalsIgnoreCase(headerName))
{
+                    bccSet = true;
+                }
             }
         }
-    }
-
-    /**
-     * Appends the Mail headers from the endpoint configuration.
-     */
-    protected void setRecipientFromEndpointConfiguration(MimeMessage mimeMessage, MailEndpoint
endpoint, Exchange exchange)
-        throws MessagingException, IOException {
 
-        Map<Message.RecipientType, String> recipients = endpoint.getConfiguration().getRecipients();
-        if (recipients.containsKey(Message.RecipientType.TO)) {
-            appendRecipientToMimeMessage(mimeMessage, endpoint.getConfiguration(), exchange,
Message.RecipientType.TO.toString(), recipients.get(Message.RecipientType.TO));
+        // Otherwise we check the configuration
+        Map<Message.RecipientType, String> recipients = configuration.getRecipients();
+        if (!toSet && recipients.containsKey(Message.RecipientType.TO)) {
+            appendRecipientToMimeMessage(mimeMessage, configuration, exchange, Message.RecipientType.TO.toString(),
recipients.get(Message.RecipientType.TO));
         }
-        if (recipients.containsKey(Message.RecipientType.CC)) {
-            appendRecipientToMimeMessage(mimeMessage, endpoint.getConfiguration(), exchange,
Message.RecipientType.CC.toString(), recipients.get(Message.RecipientType.CC));
+        if (!ccSet && recipients.containsKey(Message.RecipientType.CC)) {
+            appendRecipientToMimeMessage(mimeMessage, configuration, exchange, Message.RecipientType.CC.toString(),
recipients.get(Message.RecipientType.CC));
         }
-        if (recipients.containsKey(Message.RecipientType.BCC)) {
-            appendRecipientToMimeMessage(mimeMessage, endpoint.getConfiguration(), exchange,
Message.RecipientType.BCC.toString(), recipients.get(Message.RecipientType.BCC));
+        if (!bccSet && recipients.containsKey(Message.RecipientType.BCC)) {
+            appendRecipientToMimeMessage(mimeMessage, configuration, exchange, Message.RecipientType.BCC.toString(),
recipients.get(Message.RecipientType.BCC));
         }
     }
 
diff --git a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRecipientsTest.java
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRecipientsTest.java
index 490d81e..02c66e1 100644
--- a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRecipientsTest.java
+++ b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRecipientsTest.java
@@ -20,7 +20,6 @@ import java.util.HashMap;
 import java.util.Map;
 
 import javax.mail.Message;
-import javax.mail.internet.InternetAddress;
 
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.test.junit4.CamelTestSupport;
@@ -93,7 +92,8 @@ public class MailRecipientsTest extends CamelTestSupport {
     public void testSpecificHeaderBlocked() throws Exception {
         Mailbox.clearAll();
 
-        // direct:c blocks the "cc" message header - so only "to" will be used here
+        // direct:c blocks the "cc" message header - so only "to" will be used here. It picks
up
+        // cc from the configuration
         Map<String, Object> headers = new HashMap<>();
         headers.put("to", "to@riders.org");
         headers.put("cc", "header@riders.org");
@@ -103,8 +103,7 @@ public class MailRecipientsTest extends CamelTestSupport {
         Mailbox box = Mailbox.get("to@riders.org");
         Message msg = box.get(0);
         assertEquals("to@riders.org", msg.getRecipients(Message.RecipientType.TO)[0].toString());
-        assertNull(msg.getRecipients(Message.RecipientType.CC));
-        // TODO assertEquals("me@you.org", msg.getRecipients(Message.RecipientType.CC)[0].toString());
+        assertEquals("me@you.org", msg.getRecipients(Message.RecipientType.CC)[0].toString());
     }
 
     @Test


Mime
View raw message