Author: davsclaus
Date: Thu Aug  7 22:53:51 2008
New Revision: 683862

URL: http://svn.apache.org/viewvc?rev=683862&view=rev
Log:
CAMEL-777: Any To, CC and BCC from the uri configuration overrules any To, CC 
and BCC from in message headers.

Modified:
    
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
    
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
    
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java

Modified: 
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java?rev=683862&r1=683861&r2=683862&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
 (original)
+++ 
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
 Thu Aug  7 22:53:51 2008
@@ -47,9 +47,10 @@
     public void populateMailMessage(MailEndpoint endpoint, MimeMessage 
mimeMessage, Exchange exchange)
         throws MessagingException, IOException {
 
+        // append the headers from the in message at first
         appendHeadersFromCamel(mimeMessage, exchange, exchange.getIn());
 
-        // set the recipients (receivers) of the mail
+        // than override any from the fixed endpoint configuraiton
         Map<Message.RecipientType, String> recipients = 
endpoint.getConfiguration().getRecipients();
         if (recipients.containsKey(Message.RecipientType.TO)) {
             mimeMessage.setRecipients(Message.RecipientType.TO, 
recipients.get(Message.RecipientType.TO));
@@ -61,6 +62,11 @@
             mimeMessage.setRecipients(Message.RecipientType.BCC, 
recipients.get(Message.RecipientType.BCC));
         }
 
+        // fallback to use destination if no TO provided at all
+        if (mimeMessage.getRecipients(Message.RecipientType.TO) == null) {
+            mimeMessage.setRecipients(Message.RecipientType.TO, 
endpoint.getConfiguration().getDestination());
+        }
+
         // must have at least one recipients otherwise we do not know where to 
send the mail
         if (mimeMessage.getAllRecipients() == null) {
             throw new IllegalArgumentException("The mail message does not have 
any recipients set.");
@@ -126,6 +132,22 @@
     }
 
     /**
+     * Does the given camel message contain any To, CC or BCC header names?
+     */
+    private static boolean hasRecipientHeaders(org.apache.camel.Message 
camelMessage) {
+        for (String key : camelMessage.getHeaders().keySet()) {
+            if (Message.RecipientType.TO.toString().equals(key)) {
+                return true;
+            } else if (Message.RecipientType.CC.toString().equals(key)) {
+                return true;
+            } else if (Message.RecipientType.BCC.toString().equals(key)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
      * Appends the Mail attachments from the Camel [EMAIL PROTECTED] 
MailMessage}
      */
     protected void appendAttachmentsFromCamel(MimeMessage mimeMessage, 
org.apache.camel.Message camelMessage,

Modified: 
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java?rev=683862&r1=683861&r2=683862&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
 (original)
+++ 
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
 Thu Aug  7 22:53:51 2008
@@ -54,6 +54,7 @@
     private boolean ignoreUriScheme;
     private boolean processOnlyUnseenMessages;
     private Map<Message.RecipientType, String> recipients = new 
HashMap<Message.RecipientType, String>();
+    private String destination;
     private int fetchSize = -1;
     private boolean debugMode;
     private long connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
@@ -259,14 +260,14 @@
 
     public void setUsername(String username) {
         this.username = username;
-        if (!recipients.containsKey(Message.RecipientType.TO)) {
+        if (destination == null) {
             // set default destination to [EMAIL PROTECTED] for backwards 
compatibility
             // can be overridden by URI parameters
             String address = username;
             if (address.indexOf("@") == -1) {
                 address += "@" + host;
             }
-            recipients.put(Message.RecipientType.TO, address);
+            destination = address;
         }
     }
 
@@ -276,8 +277,7 @@
      * @deprecated use [EMAIL PROTECTED] #getRecipients()}
      */
     public String getDestination() {
-        // for backwards compatibility
-        return recipients.get(Message.RecipientType.TO);
+        return destination;
     }
 
     /**
@@ -286,8 +286,7 @@
      * @deprecated use [EMAIL PROTECTED] #setTo(String)}
      */
     public void setDestination(String destination) {
-        // for backwards compatibility
-        recipients.put(Message.RecipientType.TO, destination);
+        this.destination = destination;
     }
 
     public String getFrom() {

Modified: 
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java?rev=683862&r1=683861&r2=683862&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
 (original)
+++ 
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
 Thu Aug  7 22:53:51 2008
@@ -75,9 +75,17 @@
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
-                from("pop3://[EMAIL PROTECTED]").to("direct:a");
-                from("direct:a").to("smtp://[EMAIL PROTECTED]", "smtp://[EMAIL 
PROTECTED]");
-                from("pop3://[EMAIL 
PROTECTED]").convertBodyTo(String.class).to("mock:result");
+                from("pop3://[EMAIL PROTECTED]")
+                    .to("direct:a");
+
+                // must use fixed to option to send the mail to the given 
reciever, as we have polled
+                // a mail from a mailbox where it already has the 'old' To as 
header value
+                from("direct:a")
+                    .to("smtp://[EMAIL PROTECTED]",
+                          "smtp://[EMAIL PROTECTED]");
+
+                from("pop3://[EMAIL PROTECTED]")
+                    .convertBodyTo(String.class).to("mock:result");
             }
         };
     }


Reply via email to