activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r537937 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/ camel-mail/src/main/java/org/apache/camel/component/mail/ camel-mail/src/test/java/org/apache/camel/component/mail/
Date Mon, 14 May 2007 18:25:33 GMT
Author: chirino
Date: Mon May 14 11:25:32 2007
New Revision: 537937

URL: http://svn.apache.org/viewvc?view=rev&rev=537937
Log:
Updaed the mail component so that mail headers are correctly copied over from message to message.
 This highlighted the fact
that th TO: should always be what is specified on the outbound endpoint instead of the To:
in the message.
Added a test the validates headers are preserved.


Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelTemplate.java
    activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
    activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
    activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MessageHeaderNamesAccessException.java
    activemq/camel/trunk/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelTemplate.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelTemplate.java?view=diff&rev=537937&r1=537936&r2=537937
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelTemplate.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelTemplate.java Mon
May 14 11:25:32 2007
@@ -146,6 +146,26 @@
         });
         return extractResultBody(result);
     }
+    
+    /**
+     * Sends the body to an endpoint with the specified headers and header values
+     *
+     * @param endpointUri the endpoint URI to send to
+     * @param body        the payload send
+     * @return the result
+     */
+    public Object sendBody(String endpointUri, final Object body, final Map<String, Object>
headers) {
+        E result = send(endpointUri, new Processor() {
+            public void process(Exchange exchange) {
+                Message in = exchange.getIn();
+                for (Map.Entry<String, Object> header : headers.entrySet()) {
+                    in.setHeader(header.getKey(), header.getValue());
+				}
+                in.setBody(body);
+            }
+        });
+        return extractResultBody(result);
+    }
 
     // Methods using the default endpoint
     //-----------------------------------------------------------------------

Modified: activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java?view=diff&rev=537937&r1=537936&r2=537937
==============================================================================
--- activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
(original)
+++ activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
Mon May 14 11:25:32 2007
@@ -17,15 +17,16 @@
  */
 package org.apache.camel.component.mail;
 
-import org.apache.camel.Exchange;
+import java.util.Map;
+import java.util.Set;
 
+import javax.mail.Address;
 import javax.mail.Message;
 import javax.mail.MessagingException;
-import javax.mail.Address;
-import javax.mail.internet.MimeMessage;
 import javax.mail.internet.InternetAddress;
-import java.util.Map;
-import java.util.Set;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.camel.Exchange;
 
 /**
  * A Strategy used to convert between a Camel {@Exchange} and {@Message} to and from a
@@ -38,11 +39,11 @@
         try {
             appendMailHeaders(mimeMessage, exchange.getIn());
 
-            if (empty(mimeMessage.getAllRecipients())) {
-                // lets default the address to the endpoint destination
-                String destination = endpoint.getConfiguration().getDestination();
+            String destination = endpoint.getConfiguration().getDestination();
+            if (destination != null ) {
                 mimeMessage.setRecipients(Message.RecipientType.TO, destination);
             }
+                        
             if (empty(mimeMessage.getFrom())) {
                 // lets default the address to the endpoint destination
                 String from = endpoint.getConfiguration().getFrom();
@@ -84,7 +85,23 @@
             Object headerValue = entry.getValue();
             if (headerValue != null) {
                 if (shouldOutputHeader(camelMessage, headerName, headerValue)) {
-                    mimeMessage.setHeader(headerName, headerValue.toString());
+                	
+            		String[] values = new String[]{};
+            		Class stringArrayClazz = values.getClass();
+            		
+            		// Mail messages can repeat the same header...
+            		if( headerValue.getClass() == stringArrayClazz ) {
+            			mimeMessage.removeHeader(headerName);
+                		values = (String[]) headerValue;
+                		for (int i = 0; i < values.length; i++) {
+                            mimeMessage.addHeader(headerName, values[i]);
+						}
+            		} else if( headerValue.getClass() == String.class ) {
+                        mimeMessage.setHeader(headerName, (String) headerValue);
+            		} else {
+                		// Unknown type? then use toString()
+                        mimeMessage.setHeader(headerName, headerValue.toString());
+                	}
                 }
             }
         }

Modified: activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java?view=diff&rev=537937&r1=537936&r2=537937
==============================================================================
--- activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
(original)
+++ activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
Mon May 14 11:25:32 2007
@@ -19,6 +19,7 @@
 
 import org.apache.camel.impl.DefaultMessage;
 
+import javax.mail.Header;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import java.util.Enumeration;
@@ -67,7 +68,7 @@
     }
 
     public Object getHeader(String name) {
-        Object answer = null;
+        String[] answer = null;
         if (mailMessage != null) {
             try {
                 answer = mailMessage.getHeader(name);
@@ -77,9 +78,12 @@
             }
         }
         if (answer == null) {
-            answer = super.getHeader(name);
+            return super.getHeader(name);
         }
-        return answer;
+        if( answer.length > 0 ) {
+        	return answer[0];
+        }
+        return null;
     }
 
     @Override
@@ -105,15 +109,16 @@
             catch (MessagingException e) {
                 throw new MessageHeaderNamesAccessException(e);
             }
-            while (names.hasMoreElements()) {
-                String name = names.nextElement().toString();
-                try {
-                    Object value = mailMessage.getHeader(name);
-                    map.put(name, value);
-                }
-                catch (MessagingException e) {
-                    throw new MessageHeaderAccessException(name, e);
-                }
+            
+            System.out.println("Copying....");
+            try {
+	            while (names.hasMoreElements()) {
+	                Header header = (Header) names.nextElement();
+	                map.put(header.getName(), header.getValue());
+	                System.out.println("Set: "+header.getName()+"="+header.getValue());
+	            }
+            }catch (Throwable e) {
+                throw new MessageHeaderNamesAccessException(e);                
             }
         }
     }

Modified: activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MessageHeaderNamesAccessException.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MessageHeaderNamesAccessException.java?view=diff&rev=537937&r1=537936&r2=537937
==============================================================================
--- activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MessageHeaderNamesAccessException.java
(original)
+++ activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MessageHeaderNamesAccessException.java
Mon May 14 11:25:32 2007
@@ -17,7 +17,6 @@
  */
 package org.apache.camel.component.mail;
 
-import javax.mail.MessagingException;
 
 /**
  * @version $Revision:520964 $
@@ -25,7 +24,7 @@
 public class MessageHeaderNamesAccessException extends RuntimeMailException {
     private static final long serialVersionUID = -6744171518099741324L;
 
-    public MessageHeaderNamesAccessException(MessagingException e) {
+    public MessageHeaderNamesAccessException(Throwable e) {
         super("Failed to acess the Mail message property names", e);
     }
 }

Modified: activemq/camel/trunk/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java?view=diff&rev=537937&r1=537936&r2=537937
==============================================================================
--- activemq/camel/trunk/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
(original)
+++ activemq/camel/trunk/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
Mon May 14 11:25:32 2007
@@ -18,6 +18,7 @@
 package org.apache.camel.component.mail;
 
 import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.builder.RouteBuilder;
 import static org.apache.camel.util.ObjectHelper.asString;
@@ -27,6 +28,7 @@
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import java.io.IOException;
+import java.util.HashMap;
 
 /**
  * @version $Revision: 1.1 $
@@ -38,7 +40,9 @@
         resultEndpoint = (MockEndpoint) resolveMandatoryEndpoint("mock:result");
         resultEndpoint.expectedBodiesReceived("hello world!");
 
-        template.sendBody("smtp://james@localhost", "hello world!");
+        HashMap<String, Object> headers = new HashMap<String, Object>();
+        headers.put("reply-to", "reply1@localhost");
+        template.sendBody("smtp://james@localhost", "hello world!", headers);
 
         // lets test the first sent worked
         assertMailboxReceivedMessages("james@localhost");
@@ -48,7 +52,12 @@
 
         // lets test the receive worked
         resultEndpoint.assertIsSatisfied();
-
+        
+        // Validate that the headers were preserved.
+        Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
+        String replyTo = (String) exchange.getIn().getHeader("reply-to");
+        assertEquals( "reply1@localhost", replyTo);
+        
         assertMailboxReceivedMessages("copy@localhost");
     }
 



Mime
View raw message