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 16:28:10 UTC

[camel] branch camel-2.x updated: Revert "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 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 bba770b  Revert "Pick up recipient headers from configuration for camel-mail, if they are not first specified as a header"
bba770b is described below

commit bba770b51150aa4db0101e971b923053f9ffcb52
Author: Colm O hEigeartaigh <co...@apache.org>
AuthorDate: Wed Feb 6 16:27:05 2019 +0000

    Revert "Pick up recipient headers from configuration for camel-mail, if they are not first specified as a header"
    
    This reverts commit 4aa04cdccd85e6e6f4083b8ce382ca4034aca332.
---
 .../apache/camel/component/mail/MailBinding.java   | 44 +++++++++++-----------
 .../camel/component/mail/MailRecipientsTest.java   |  7 ++--
 2 files changed, 26 insertions(+), 25 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 fb99750..d50a04c 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,7 +81,13 @@ public class MailBinding {
     public void populateMailMessage(MailEndpoint endpoint, MimeMessage mimeMessage, Exchange exchange)
         throws MessagingException, IOException {
 
-        setRecipients(mimeMessage, endpoint.getConfiguration(), exchange);
+        // 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);
+        }
 
         // 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.
@@ -402,11 +408,7 @@ public class MailBinding {
         }
     }
 
-    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;
+    private void setRecipientFromCamelMessage(MimeMessage mimeMessage, MailConfiguration configuration, Exchange exchange) throws MessagingException, IOException {
         for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
             String headerName = entry.getKey();
             Object headerValue = entry.getValue();
@@ -423,27 +425,25 @@ 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 {
 
-        // 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));
+        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));
         }
-        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.CC)) {
+            appendRecipientToMimeMessage(mimeMessage, endpoint.getConfiguration(), exchange, Message.RecipientType.CC.toString(), recipients.get(Message.RecipientType.CC));
         }
-        if (!bccSet && recipients.containsKey(Message.RecipientType.BCC)) {
-            appendRecipientToMimeMessage(mimeMessage, configuration, exchange, Message.RecipientType.BCC.toString(), recipients.get(Message.RecipientType.BCC));
+        if (recipients.containsKey(Message.RecipientType.BCC)) {
+            appendRecipientToMimeMessage(mimeMessage, endpoint.getConfiguration(), 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 02c66e1..490d81e 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,6 +20,7 @@ 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;
@@ -92,8 +93,7 @@ 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. It picks up
-        // cc from the configuration
+        // direct:c blocks the "cc" message header - so only "to" will be used here
         Map<String, Object> headers = new HashMap<>();
         headers.put("to", "to@riders.org");
         headers.put("cc", "header@riders.org");
@@ -103,7 +103,8 @@ 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());
-        assertEquals("me@you.org", msg.getRecipients(Message.RecipientType.CC)[0].toString());
+        assertNull(msg.getRecipients(Message.RecipientType.CC));
+        // TODO assertEquals("me@you.org", msg.getRecipients(Message.RecipientType.CC)[0].toString());
     }
 
     @Test