You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2007/05/21 16:33:34 UTC

svn commit: r540154 - in /activemq/camel/trunk/components/camel-mail: ./ src/main/java/org/apache/camel/component/mail/ src/test/java/org/apache/camel/component/mail/

Author: jstrachan
Date: Mon May 21 07:33:33 2007
New Revision: 540154

URL: http://svn.apache.org/viewvc?view=rev&rev=540154
Log:
fixed a minor glitch when a mail provider returns multiple headers; tidied the pom, added a new test case and a minor refactoring

Added:
    activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MultipleDestinationConsumeTest.java
      - copied, changed from r539381, activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java
Modified:
    activemq/camel/trunk/components/camel-mail/pom.xml
    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/MailMessage.java

Modified: activemq/camel/trunk/components/camel-mail/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/pom.xml?view=diff&rev=540154&r1=540153&r2=540154
==============================================================================
--- activemq/camel/trunk/components/camel-mail/pom.xml (original)
+++ activemq/camel/trunk/components/camel-mail/pom.xml Mon May 21 07:33:33 2007
@@ -47,24 +47,14 @@
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-core</artifactId>
     </dependency>
-
-    <dependency>
-      <groupId>org.apache.camel</groupId>
-      <artifactId>camel-core</artifactId>
-      <type>test-jar</type>
-    </dependency>
-
     <dependency>
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-spring</artifactId>
-      <optional>true</optional>
     </dependency>
-
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring</artifactId>
     </dependency>
-
     <dependency>
       <groupId>javax.mail</groupId>
       <artifactId>mail</artifactId>
@@ -72,6 +62,13 @@
 
 
     <!-- testing -->
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-core</artifactId>
+      <type>test-jar</type>
+      <optional>true</optional>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.jvnet.mock-javamail</groupId>
       <artifactId>mock-javamail</artifactId>

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?view=diff&rev=540154&r1=540153&r2=540154
==============================================================================
--- 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 Mon May 21 07:33:33 2007
@@ -17,16 +17,17 @@
  */
 package org.apache.camel.component.mail;
 
-import java.util.Map;
-import java.util.Set;
+import org.apache.camel.Exchange;
+import org.apache.camel.converter.ObjectConverter;
 
 import javax.mail.Address;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
-
-import org.apache.camel.Exchange;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * A Strategy used to convert between a Camel {@Exchange} and {@Message} to and from a
@@ -37,13 +38,13 @@
 public class MailBinding {
     public void populateMailMessage(MailEndpoint endpoint, MimeMessage mimeMessage, Exchange exchange) {
         try {
-            appendMailHeaders(mimeMessage, exchange.getIn());
+            appendHeadersFromCamel(mimeMessage, exchange, exchange.getIn());
 
             String destination = endpoint.getConfiguration().getDestination();
-            if (destination != null ) {
+            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();
@@ -78,33 +79,35 @@
     /**
      * Appends the Mail headers from the Camel {@link MailMessage}
      */
-    protected void appendMailHeaders(MimeMessage mimeMessage, org.apache.camel.Message camelMessage) throws MessagingException {
+    protected void appendHeadersFromCamel(MimeMessage mimeMessage, Exchange exchange, org.apache.camel.Message camelMessage) throws MessagingException {
         Set<Map.Entry<String, Object>> entries = camelMessage.getHeaders().entrySet();
         for (Map.Entry<String, Object> entry : entries) {
             String headerName = entry.getKey();
             Object headerValue = entry.getValue();
             if (headerValue != null) {
                 if (shouldOutputHeader(camelMessage, headerName, headerValue)) {
-                	
-            		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());
-                	}
+
+                    // Mail messages can repeat the same header...
+                    if (ObjectConverter.isCollection(headerValue)) {
+                        Iterator iter = ObjectConverter.iterator(headerValue);
+                        while (iter.hasNext()) {
+                            Object value = iter.next();
+                            mimeMessage.addHeader(headerName, asString(exchange, value));
+                        }
+                    }
+                    else {
+                        mimeMessage.setHeader(headerName, asString(exchange, headerValue));
+                    }
                 }
             }
         }
+    }
+
+    /**
+     * Converts the given object value to a String
+     */
+    protected String asString(Exchange exchange, Object value) {
+        return exchange.getContext().getTypeConverter().convertTo(String.class, value);
     }
 
     /**

Modified: activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java?view=diff&rev=540154&r1=540153&r2=540154
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java (original)
+++ activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java Mon May 21 07:33:33 2007
@@ -18,6 +18,7 @@
 package org.apache.camel.component.mail;
 
 import org.apache.camel.impl.DefaultMessage;
+import org.apache.camel.util.CollectionHelper;
 
 import javax.mail.Header;
 import javax.mail.Message;
@@ -46,7 +47,8 @@
             return "MailMessage: " + mailMessage;
         }
         else {
-            return "MailMessage: " + getBody();}
+            return "MailMessage: " + getBody();
+        }
     }
 
     @Override
@@ -80,10 +82,10 @@
         if (answer == null) {
             return super.getHeader(name);
         }
-        if( answer.length > 0 ) {
-        	return answer[0];
+        if (answer.length == 1) {
+            return answer[0];
         }
-        return null;
+        return answer;
     }
 
     @Override
@@ -109,16 +111,16 @@
             catch (MessagingException e) {
                 throw new MessageHeaderNamesAccessException(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);                
+                while (names.hasMoreElements()) {
+                    Header header = (Header) names.nextElement();
+                    String value = header.getValue();
+                    String name = header.getName();
+                    CollectionHelper.appendValue(map, name, value);
+                }
+            }
+            catch (Throwable e) {
+                throw new MessageHeaderNamesAccessException(e);
             }
         }
     }

Copied: activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MultipleDestinationConsumeTest.java (from r539381, activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MultipleDestinationConsumeTest.java?view=diff&rev=540154&p1=activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java&r1=539381&p2=activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MultipleDestinationConsumeTest.java&r2=540154
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java (original)
+++ activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MultipleDestinationConsumeTest.java Mon May 21 07:33:33 2007
@@ -19,87 +19,88 @@
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.util.ObjectHelper;
-import org.jvnet.mock_javamail.Mailbox;
 
+import javax.mail.Address;
 import javax.mail.Message;
-import javax.mail.MessagingException;
-import javax.mail.Multipart;
 import javax.mail.Session;
 import javax.mail.Transport;
-import javax.mail.internet.MimeBodyPart;
+import javax.mail.Header;
+import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
-import javax.mail.internet.MimeMultipart;
-import java.io.IOException;
 import java.util.Properties;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Enumeration;
 
 /**
  * @version $Revision: 1.1 $
  */
-public class MimeMessageConsumeTest extends ContextTestSupport {
+public class MultipleDestinationConsumeTest extends ContextTestSupport {
     protected MockEndpoint resultEndpoint;
     protected String body = "hello world!";
+    protected Session mailSession;
 
     public void testSendAndReceiveMails() throws Exception {
         resultEndpoint = (MockEndpoint) resolveMandatoryEndpoint("mock:result");
         resultEndpoint.expectedMinimumMessageCount(1);
 
-        Properties properties = new Properties();
-        properties.put("mail.smtp.host", "localhost");
-        Session session = Session.getDefaultInstance(properties, null);
+        MimeMessage message = new MimeMessage(mailSession);
+        message.setText(body);
 
-        MimeMessage message = new MimeMessage(session);
-        populateMimeMessageBody(message);
-        message.setRecipients(Message.RecipientType.TO, "james@localhost");
+        message.setRecipients(Message.RecipientType.TO, new Address[]{
+                new InternetAddress("james@localhost"), new InternetAddress("bar@localhost")
+        });
 
         Transport.send(message);
 
-
-
         // lets test the receive worked
         resultEndpoint.assertIsSatisfied();
 
         Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
 
-        log.info("Received: " + exchange.getIn().getBody());
+        org.apache.camel.Message in = exchange.getIn();
+        log.debug("Received: " + in.getBody());
+
+        String text = in.getBody(String.class);
+        log.debug("Has headers: " + in.getHeaders());
+
+        MailExchange mailExchange = (MailExchange) exchange;
+        Message inMessage = mailExchange.getIn().getMessage();
+        Enumeration iter = inMessage.getAllHeaders();
+        while (iter.hasMoreElements()) {
+            Header header = (Header) iter.nextElement();
+            String[] value = message.getHeader(header.getName());
+            log.debug("Header: " + header.getName() + " has value: " + ObjectHelper.asString(value));
+        }
 
-        String text = exchange.getIn().getBody(String.class);
         assertEquals("body", body, text);
+        Object value = in.getHeader("TO");
+        assertEquals("TO Header", "james@localhost, bar@localhost", value);
+/*
+        List list = assertIsInstanceOf(List.class, value);
+        assertEquals("to list", 2, list.size());
+*/
     }
 
-    /**
-     * Lets encode a multipart mime message
-     */
-    protected void populateMimeMessageBody(MimeMessage message) throws MessagingException {
-        MimeBodyPart plainPart = new MimeBodyPart();
-        plainPart.setText(body);
-
-        MimeBodyPart htmlPart = new MimeBodyPart();
-        htmlPart.setText("<html><body>" + body + "</body></html>");
-
-        Multipart alt = new MimeMultipart("alternative");
-        alt.addBodyPart(plainPart);
-        alt.addBodyPart(htmlPart);
-
-        Multipart mixed = new MimeMultipart("mixed");
-        MimeBodyPart wrap = new MimeBodyPart();
-        wrap.setContent(alt);
-        mixed.addBodyPart(wrap);
-
-        mixed.addBodyPart(plainPart);
-        mixed.addBodyPart(htmlPart);
+    @Override
+    protected void setUp() throws Exception {
+        Properties properties = new Properties();
+        properties.put("mail.smtp.host", "localhost");
+        mailSession = Session.getInstance(properties, null);
 
-        message.setContent(mixed);
+        super.setUp();
     }
 
     @Override
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
-                from("smtp://james@localhost").convertBodyTo(String.class).to("mock:result");
+                from("smtp://james@localhost?password=foo").convertBodyTo(String.class).to("mock:result");
             }
         };
     }
-}
+}
\ No newline at end of file