You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by co...@apache.org on 2019/02/06 15:54:56 UTC

[camel] branch master updated: Pick up recipient headers from configuration for camel-mail, if they are not first specified as a header

This is an automated email from the ASF dual-hosted git repository.

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


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

commit 4975484654d7c9d2e0a6379afa58748fda60d019
Author: Colm O hEigeartaigh <co...@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 a4d3096..e8e16cc 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
@@ -82,13 +82,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.
@@ -409,7 +403,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();
@@ -426,25 +424,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