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/17 19:08:44 UTC

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

Author: jstrachan
Date: Thu May 17 10:08:44 2007
New Revision: 539023

URL: http://svn.apache.org/viewvc?view=rev&rev=539023
Log:
added another converter and a test case to demonstrate consuming mime messages using the text payload

Added:
    activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java   (with props)
Modified:
    activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java

Modified: activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java?view=diff&rev=539023&r1=539022&r2=539023
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java (original)
+++ activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java Thu May 17 10:08:44 2007
@@ -19,9 +19,10 @@
 
 import org.apache.camel.Converter;
 
+import javax.mail.BodyPart;
 import javax.mail.Message;
 import javax.mail.MessagingException;
-import javax.mail.BodyPart;
+import javax.mail.Multipart;
 import javax.mail.internet.MimeMultipart;
 import java.io.IOException;
 
@@ -50,6 +51,17 @@
         }
         if (content != null) {
             return content.toString();
+        }
+        return null;
+    }
+
+    @Converter
+    public static String toString(Multipart multipart) throws MessagingException, IOException {
+        for (int i = 0, size = multipart.getCount(); i < size; i++) {
+            BodyPart part = multipart.getBodyPart(i);
+            if (part.getContentType().startsWith("text")) {
+                return part.getContent().toString();
+            }
         }
         return null;
     }

Added: 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/MimeMessageConsumeTest.java?view=auto&rev=539023
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java (added)
+++ activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java Thu May 17 10:08:44 2007
@@ -0,0 +1,105 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mail;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+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.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.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
+import java.io.IOException;
+import java.util.Properties;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class MimeMessageConsumeTest extends ContextTestSupport {
+    protected MockEndpoint resultEndpoint;
+    protected String body = "hello world!";
+
+    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(session);
+        populateMimeMessageBody(message);
+        message.setRecipients(Message.RecipientType.TO, "james@localhost");
+
+        Transport.send(message);
+
+
+
+        // lets test the receive worked
+        resultEndpoint.assertIsSatisfied();
+
+        Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
+
+        log.info("Received: " + exchange.getIn().getBody());
+
+        String text = exchange.getIn().getBody(String.class);
+        assertEquals("body", body, text);
+    }
+
+    /**
+     * 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);
+
+        message.setContent(mixed);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("smtp://james@localhost").convertBodyTo(String.class).to("mock:result");
+            }
+        };
+    }
+}

Propchange: activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native