You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2008/04/27 12:23:37 UTC

svn commit: r651913 - in /activemq/camel/trunk/components/camel-mail/src: main/java/org/apache/camel/component/mail/ main/resources/META-INF/services/org/apache/camel/component/ test/java/org/apache/camel/component/mail/

Author: davsclaus
Date: Sun Apr 27 03:23:31 2008
New Revision: 651913

URL: http://svn.apache.org/viewvc?rev=651913&view=rev
Log:
CAMEL-335
- Default port number for protocols
- Username can be set using URI parameter
- Use of URI fragment removed (no other components use it at all, and it was not documented, and had mixed purpose)
- imaps protocol added

Added:
    activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java   (with props)
    activemq/camel/trunk/components/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/imaps
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/main/java/org/apache/camel/component/mail/MailEndpoint.java
    activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.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=651913&r1=651912&r2=651913&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 Sun Apr 27 03:23:31 2008
@@ -41,6 +41,7 @@
  * @version $Revision$
  */
 public class MailBinding {
+    
     public void populateMailMessage(MailEndpoint endpoint, MimeMessage mimeMessage, Exchange exchange) {
         try {
             appendHeadersFromCamel(mimeMessage, exchange, exchange.getIn());
@@ -62,9 +63,8 @@
                 mimeMessage.setText(exchange.getIn().getBody(String.class));
             }
         } catch (Exception e) {
-            throw new RuntimeMailException(
-                                           "Failed to populate body due to: " + e + ". Exchange: " + exchange,
-                                           e);
+            throw new RuntimeMailException("Failed to populate body due to: " + e.getMessage() +
+                ". Exchange: " + exchange, e);
         }
     }
 
@@ -74,15 +74,13 @@
 
     /**
      * Extracts the body from the Mail message
-     * 
-     * @param exchange
-     * @param message
      */
     public Object extractBodyFromMail(MailExchange exchange, Message message) {
         try {
             return message.getContent();
         } catch (Exception e) {
-            throw new RuntimeMailException("Failed to extract body due to: " + e + ". Message: " + message, e);
+            throw new RuntimeMailException("Failed to extract body due to: " + e.getMessage() +
+                ". Exchange: " + exchange + ". Message: " + message, e);
         }
     }
 
@@ -129,7 +127,7 @@
         textBodyPart.setContent(exchange.getIn().getBody(String.class), "text/plain");
         multipart.addBodyPart(textBodyPart);
 
-        BodyPart messageBodyPart = null;
+        BodyPart messageBodyPart;
 
         Set<Map.Entry<String, DataHandler>> entries = camelMessage.getAttachments().entrySet();
         for (Map.Entry<String, DataHandler> entry : entries) {
@@ -170,8 +168,7 @@
     }
 
     /**
-     * Strategy to allow filtering of attachments which are put on the Mail
-     * message
+     * Strategy to allow filtering of attachments which are put on the Mail message
      */
     protected boolean shouldOutputAttachment(org.apache.camel.Message camelMessage, String headerName,
                                              DataHandler headerValue) {

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=651913&r1=651912&r2=651913&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 Sun Apr 27 03:23:31 2008
@@ -29,19 +29,23 @@
  * @version $Revision$
  */
 public class MailConfiguration implements Cloneable {
-    private String defaultEncoding;
-    private String host;
+
+    public static final String DEFAULT_FOLDER_NAME = "INBOX";
+    public static final String DEFAULT_FROM = "camel@localhost";
+
     private Properties javaMailProperties;
-    private String password;
     private String protocol;
-    private Session session;
-    private String username;
+    private String host;
     private int port = -1;
+    private String username;
+    private String password;
+    private Session session;
+    private String defaultEncoding;
+    private String from = DEFAULT_FROM;
     private String destination;
-    private String from = "camel@localhost";
+    private String folderName = DEFAULT_FOLDER_NAME;
     private boolean deleteProcessedMessages = true;
-    private String folderName = "INBOX";
-    private boolean ignoreUriScheme;
+    private boolean ignoreUriScheme = false;
 
     public MailConfiguration() {
     }
@@ -69,27 +73,23 @@
                 setProtocol(scheme);
             }
         }
+
         String userInfo = uri.getUserInfo();
         if (userInfo != null) {
             setUsername(userInfo);
+
+            // set default destination to userInfo@host for backwards compatibility
+            // can be overridden by URI parameters
+            setDestination(userInfo + "@" + host);
         }
+
         int port = uri.getPort();
         if (port >= 0) {
             setPort(port);
-        }
-
-        // we can either be invoked with
-        // mailto:address
-        // or
-        // smtp:user@host:port/name@address
-
-        String fragment = uri.getFragment();
-        if (fragment == null || fragment.length() == 0) {
-            fragment = userInfo + "@" + host;
         } else {
-            setFolderName(fragment);
+            // resolve default port if no port number was provided
+            setPort(MailUtils.getDefaultPortForProtocol(uri.getScheme()));
         }
-        setDestination(fragment);
     }
 
     public JavaMailConnection createJavaMailConnection(MailEndpoint mailEndpoint) {
@@ -97,7 +97,6 @@
         if (defaultEncoding != null) {
             answer.setDefaultEncoding(defaultEncoding);
         }
-        // answer.setDefaultFileTypeMap(fileTypeMap);
         if (host != null) {
             answer.setHost(host);
         }

Modified: activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java?rev=651913&r1=651912&r2=651913&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java (original)
+++ activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java Sun Apr 27 03:23:31 2008
@@ -53,6 +53,7 @@
     public Consumer<MailExchange> createConsumer(Processor processor) throws Exception {
         JavaMailConnection connection = configuration.createJavaMailConnection(this);
         String protocol = getConfiguration().getProtocol();
+        // TODO: Why do we change protocol from smtp to pop3?
         if (protocol.equals("smtp")) {
             protocol = "pop3";
         }

Added: activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java?rev=651913&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java (added)
+++ activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java Sun Apr 27 03:23:31 2008
@@ -0,0 +1,81 @@
+/**
+ * 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;
+
+/**
+ * Mail utility class.
+ * <p>
+ * Parts of the code copied from Apache ServiceMix.
+ *
+ * @version $Revision$
+ */
+public class MailUtils {
+
+    public static final int DEFAULT_PORT_SMTP = 25;
+    public static final int DEFAULT_PORT_SMTPS = 465;
+    public static final int DEFAULT_PORT_POP3 = 110;
+    public static final int DEFAULT_PORT_POP3S = 995;
+    public static final int DEFAULT_PORT_NNTP = 119;
+    public static final int DEFAULT_PORT_IMAP = 143;
+    public static final int DEFAULT_PORT_IMAPS = 993;
+
+    public static final String PROTOCOL_SMTP = "smtp";
+    public static final String PROTOCOL_SMTPS = "smtps";
+    public static final String PROTOCOL_POP3 = "pop3";
+    public static final String PROTOCOL_POP3S = "pop3s";
+    public static final String PROTOCOL_NNTP = "nntp";
+    public static final String PROTOCOL_IMAP = "imap";
+    public static final String PROTOCOL_IMAPS = "imaps";
+
+    private MailUtils() {
+    }
+
+    /**
+     * Returns the default port for a given protocol.
+     * <p>
+     * If a protocol could not successfully be determined the default port number for SMTP protocol is returned.
+     *
+     * @param protocol the protocol
+     * @return the default port
+     */
+    public static int getDefaultPortForProtocol(final String protocol) {
+        int port = DEFAULT_PORT_SMTP;
+
+        if (protocol != null) {
+            if (protocol.equalsIgnoreCase(PROTOCOL_IMAP)) {
+                port = DEFAULT_PORT_IMAP;
+            } else if (protocol.equalsIgnoreCase(PROTOCOL_IMAPS)) {
+                port = DEFAULT_PORT_IMAPS;
+            } else if (protocol.equalsIgnoreCase(PROTOCOL_NNTP)) {
+                port = DEFAULT_PORT_NNTP;
+            } else if (protocol.equalsIgnoreCase(PROTOCOL_POP3)) {
+                port = DEFAULT_PORT_POP3;
+            } else if (protocol.equalsIgnoreCase(PROTOCOL_POP3S)) {
+                port = DEFAULT_PORT_POP3S;
+            } else if (protocol.equalsIgnoreCase(PROTOCOL_SMTP)) {
+                port = DEFAULT_PORT_SMTP;
+            } else if (protocol.equalsIgnoreCase(PROTOCOL_SMTPS)) {
+                port = DEFAULT_PORT_SMTPS;
+            } else {
+                port = DEFAULT_PORT_SMTP;
+            }
+        }
+
+        return port;
+    }
+    
+}

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

Propchange: activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: activemq/camel/trunk/components/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/imaps
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/imaps?rev=651913&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/imaps (added)
+++ activemq/camel/trunk/components/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/imaps Sun Apr 27 03:23:31 2008
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+class=org.apache.camel.component.mail.MailComponent

Modified: activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java?rev=651913&r1=651912&r2=651913&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java (original)
+++ activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java Sun Apr 27 03:23:31 2008
@@ -25,44 +25,76 @@
 public class MailComponentTest extends ContextTestSupport {
 
     public void testMailEndpointsAreConfiguredProperlyWhenUsingSmtp() throws Exception {
-        MailEndpoint endpoint = resolveMandatoryEndpoint("smtp://james@myhost:30/subject");
+        MailEndpoint endpoint = resolveMandatoryEndpoint("smtp://james@myhost:25/subject");
         MailConfiguration config = endpoint.getConfiguration();
         assertEquals("getProtocol()", "smtp", config.getProtocol());
         assertEquals("getHost()", "myhost", config.getHost());
-        assertEquals("getPort()", 30, config.getPort());
+        assertEquals("getPort()", 25, config.getPort());
         assertEquals("getUsername()", "james", config.getUsername());
         assertEquals("getDestination()", "james@myhost", config.getDestination());
         assertEquals("folder", "INBOX", config.getFolderName());
     }
 
     public void testMailEndpointsAreConfiguredProperlyWhenUsingImap() throws Exception {
-        MailEndpoint endpoint = resolveMandatoryEndpoint("imap://james@myhost:30/subject");
+        MailEndpoint endpoint = resolveMandatoryEndpoint("imap://james@myhost:143/subject");
         MailConfiguration config = endpoint.getConfiguration();
         assertEquals("getProtocol()", "imap", config.getProtocol());
         assertEquals("getHost()", "myhost", config.getHost());
-        assertEquals("getPort()", 30, config.getPort());
+        assertEquals("getPort()", 143, config.getPort());
         assertEquals("getUsername()", "james", config.getUsername());
         assertEquals("getDestination()", "james@myhost", config.getDestination());
         assertEquals("folder", "INBOX", config.getFolderName());
     }
 
     public void testMailEndpointsAreConfiguredProperlyWhenUsingPop() throws Exception {
-        MailEndpoint endpoint = resolveMandatoryEndpoint("pop3://james@myhost:30/subject");
+        MailEndpoint endpoint = resolveMandatoryEndpoint("pop3://james@myhost:110/subject");
         MailConfiguration config = endpoint.getConfiguration();
         assertEquals("getProtocol()", "pop3", config.getProtocol());
         assertEquals("getHost()", "myhost", config.getHost());
-        assertEquals("getPort()", 30, config.getPort());
+        assertEquals("getPort()", 110, config.getPort());
         assertEquals("getUsername()", "james", config.getUsername());
         assertEquals("getDestination()", "james@myhost", config.getDestination());
         assertEquals("folder", "INBOX", config.getFolderName());
     }
 
-    public void testDefaultConfiguration() throws Exception {
+    public void testDefaultSMTPConfiguration() throws Exception {
         MailEndpoint endpoint = resolveMandatoryEndpoint("smtp://james@myhost?password=secret");
         MailConfiguration config = endpoint.getConfiguration();
         assertEquals("getProtocol()", "smtp", config.getProtocol());
         assertEquals("getHost()", "myhost", config.getHost());
-        assertEquals("getPort()", -1, config.getPort());
+        assertEquals("getPort()", MailUtils.DEFAULT_PORT_SMTP, config.getPort());
+        assertEquals("getUsername()", "james", config.getUsername());
+        assertEquals("getDestination()", "james@myhost", config.getDestination());
+        assertEquals("folder", "INBOX", config.getFolderName());
+        assertEquals("encoding", null, config.getDefaultEncoding());
+        assertEquals("from", "camel@localhost", config.getFrom());
+        assertEquals("password", "secret", config.getPassword());
+        assertEquals(true, config.isDeleteProcessedMessages());
+        assertEquals(false, config.isIgnoreUriScheme());
+    }
+
+    public void testDefaultPOP3Configuration() throws Exception {
+        MailEndpoint endpoint = resolveMandatoryEndpoint("pop3://james@myhost?password=secret");
+        MailConfiguration config = endpoint.getConfiguration();
+        assertEquals("getProtocol()", "pop3", config.getProtocol());
+        assertEquals("getHost()", "myhost", config.getHost());
+        assertEquals("getPort()", MailUtils.DEFAULT_PORT_POP3, config.getPort());
+        assertEquals("getUsername()", "james", config.getUsername());
+        assertEquals("getDestination()", "james@myhost", config.getDestination());
+        assertEquals("folder", "INBOX", config.getFolderName());
+        assertEquals("encoding", null, config.getDefaultEncoding());
+        assertEquals("from", "camel@localhost", config.getFrom());
+        assertEquals("password", "secret", config.getPassword());
+        assertEquals(true, config.isDeleteProcessedMessages());
+        assertEquals(false, config.isIgnoreUriScheme());
+    }
+
+    public void testDefaultIMAPConfiguration() throws Exception {
+        MailEndpoint endpoint = resolveMandatoryEndpoint("imap://james@myhost?password=secret");
+        MailConfiguration config = endpoint.getConfiguration();
+        assertEquals("getProtocol()", "imap", config.getProtocol());
+        assertEquals("getHost()", "myhost", config.getHost());
+        assertEquals("getPort()", MailUtils.DEFAULT_PORT_IMAP, config.getPort());
         assertEquals("getUsername()", "james", config.getUsername());
         assertEquals("getDestination()", "james@myhost", config.getDestination());
         assertEquals("folder", "INBOX", config.getFolderName());
@@ -74,8 +106,8 @@
     }
 
     public void testManyConfigurations() throws Exception {
-        MailEndpoint endpoint = resolveMandatoryEndpoint(
-            "smtp://james@myhost:30/subject?password=secret&from=me@camelriders.org&DeleteProcessedMessages=false&defaultEncoding=iso-8859-1&folderName=riders");
+        MailEndpoint endpoint = resolveMandatoryEndpoint("smtp://james@myhost:30/subject?password=secret"
+            + "&from=me@camelriders.org&DeleteProcessedMessages=false&defaultEncoding=iso-8859-1&folderName=riders");
         MailConfiguration config = endpoint.getConfiguration();
         assertEquals("getProtocol()", "smtp", config.getProtocol());
         assertEquals("getHost()", "myhost", config.getHost());
@@ -87,6 +119,38 @@
         assertEquals("from", "me@camelriders.org", config.getFrom());
         assertEquals("password", "secret", config.getPassword());
         assertEquals(false, config.isDeleteProcessedMessages());
+        assertEquals(false, config.isIgnoreUriScheme());
+    }
+
+    public void testDestination() {
+        MailEndpoint endpoint = resolveMandatoryEndpoint("smtp://james@myhost:25/?password=secret&destination=someone@outthere.com&folderName=XXX");
+        MailConfiguration config = endpoint.getConfiguration();
+        assertEquals("getProtocol()", "smtp", config.getProtocol());
+        assertEquals("getHost()", "myhost", config.getHost());
+        assertEquals("getPort()", 25, config.getPort());
+        assertEquals("getUsername()", "james", config.getUsername());
+        assertEquals("getDestination()", "someone@outthere.com", config.getDestination());
+        assertEquals("folder", "XXX", config.getFolderName());
+        assertEquals("encoding", null, config.getDefaultEncoding());
+        assertEquals("from", "camel@localhost", config.getFrom());
+        assertEquals("password", "secret", config.getPassword());
+        assertEquals(true, config.isDeleteProcessedMessages());
+        assertEquals(false, config.isIgnoreUriScheme());
+    }
+
+    public void testNoUserInfoButUsername() {
+        MailEndpoint endpoint = resolveMandatoryEndpoint("smtp://myhost:25/?password=secret&username=james");
+        MailConfiguration config = endpoint.getConfiguration();
+        assertEquals("getProtocol()", "smtp", config.getProtocol());
+        assertEquals("getHost()", "myhost", config.getHost());
+        assertEquals("getPort()", 25, config.getPort());
+        assertEquals("getUsername()", "james", config.getUsername());
+        assertEquals("getDestination()", null, config.getDestination());
+        assertEquals("folder", "INBOX", config.getFolderName());
+        assertEquals("encoding", null, config.getDefaultEncoding());
+        assertEquals("from", "camel@localhost", config.getFrom());
+        assertEquals("password", "secret", config.getPassword());
+        assertEquals(true, config.isDeleteProcessedMessages());
         assertEquals(false, config.isIgnoreUriScheme());
     }