You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by lh...@apache.org on 2010/09/10 10:19:51 UTC

svn commit: r995696 - in /servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail: MailSenderEndpoint.java marshaler/AbstractMailMarshaler.java utils/MailConnectionConfiguration.java

Author: lhein
Date: Fri Sep 10 08:19:51 2010
New Revision: 995696

URL: http://svn.apache.org/viewvc?rev=995696&view=rev
Log:
cleaned up code and added further flexible configuration possibilities for the connection attributes
(see SMXCOMP-767)

Modified:
    servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/MailSenderEndpoint.java
    servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/marshaler/AbstractMailMarshaler.java
    servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/utils/MailConnectionConfiguration.java

Modified: servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/MailSenderEndpoint.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/MailSenderEndpoint.java?rev=995696&r1=995695&r2=995696&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/MailSenderEndpoint.java (original)
+++ servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/MailSenderEndpoint.java Fri Sep 10 08:19:51 2010
@@ -58,9 +58,6 @@ public class MailSenderEndpoint extends 
     private Map<String, String> customProperties = new HashMap<String, String>();
     private IgnoreList ignoreMessageProperties = new IgnoreList();
 
-    public static final String MAIL_USERNAME = "org.apache.servicemix.mail.username";
-    public static final String MAIL_PASSWORD = "org.apache.servicemix.mail.password";
-
     /*
      * (non-Javadoc)
      * @see org.apache.servicemix.common.Endpoint#validate()
@@ -142,42 +139,72 @@ public class MailSenderEndpoint extends 
     }
 
     private String getUsername(NormalizedMessage in) {
-        if (in.getProperty(MAIL_USERNAME) != null) {
-            return (String)in.getProperty(MAIL_USERNAME);
+        if (in.getProperty(AbstractMailMarshaler.MSG_TAG_USER) != null) {
+            return (String)in.getProperty(AbstractMailMarshaler.MSG_TAG_USER);
         } else {
             return config.getUsername();
         }
     }
 
     private String getPassword(NormalizedMessage in) {
-        if (in.getProperty(MAIL_PASSWORD) != null) {
-            return (String)in.getProperty(MAIL_PASSWORD);
+        if (in.getProperty(AbstractMailMarshaler.MSG_TAG_PASSWORD) != null) {
+            return (String)in.getProperty(AbstractMailMarshaler.MSG_TAG_PASSWORD);
         } else {
             return config.getPassword();
         }
     }
 
+    private String getProtocol(NormalizedMessage in) {
+        if (in.getProperty(AbstractMailMarshaler.MSG_TAG_PROTOCOL) != null) {
+            return (String)in.getProperty(AbstractMailMarshaler.MSG_TAG_PROTOCOL);
+        } else {
+            return config.getProtocol();
+        }
+    }
+
+    private String getHost(NormalizedMessage in) {
+        if (in.getProperty(AbstractMailMarshaler.MSG_TAG_HOST) != null) {
+            return (String)in.getProperty(AbstractMailMarshaler.MSG_TAG_HOST);
+        } else {
+            return config.getHost();
+        }
+    }
+
+    private int getPort(NormalizedMessage in) {
+        if (in.getProperty(AbstractMailMarshaler.MSG_TAG_PORT) != null) {
+            return (Integer)in.getProperty(AbstractMailMarshaler.MSG_TAG_PORT);
+        } else {
+            return config.getPort();
+        }
+    }
+
     private void sendMail(MessageExchange exchange, NormalizedMessage in) throws Exception {
         Session session;
         Properties props = MailUtils.getPropertiesForProtocol(this.config, this.customTrustManagers);
         props.put("mail.debug", isDebugMode() ? "true" : "false");
 
+        String user = getUsername(in);
+        String passwd = getPassword(in);
+        String protocol = getProtocol(in);
+        String host = getHost(in);
+        int port = getPort(in);
+
         // apply the custom properties
         applyCustomProperties(props);
 
         // Get session
-        session = Session.getInstance(props, config.getAuthenticator());
+        session = Session.getInstance(props, config.getAuthenticator(user, passwd));
 
         // debug the session
         session.setDebug(this.debugMode);
 
         // get the transport from session
-        Transport transport = session.getTransport(config.getProtocol());
+        Transport transport = session.getTransport(protocol);
 
         // Connect only once here
         // Transport.send() disconnects after each send
         // Usually, no username and password is required for SMTP
-        transport.connect(config.getHost(), config.getPort(), getUsername(in), getPassword(in));
+        transport.connect(host, port, user, passwd);
 
         // Define message
         MimeMessage msg = new MimeMessage(session);

Modified: servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/marshaler/AbstractMailMarshaler.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/marshaler/AbstractMailMarshaler.java?rev=995696&r1=995695&r2=995696&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/marshaler/AbstractMailMarshaler.java (original)
+++ servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/marshaler/AbstractMailMarshaler.java Fri Sep 10 08:19:51 2010
@@ -158,6 +158,31 @@ public abstract class AbstractMailMarsha
     public static final String DEFAULT_SENDER = "no-reply@localhost";
 
     /**
+     * this tag is used in normalized messages to represent the user which owns the mail account
+     */
+    public static final String MSG_TAG_USER = "org.apache.servicemix.mail.username";
+
+    /**
+     * this tag is used in normalized messages to represent the password of the mail account
+     */
+    public static final String MSG_TAG_PASSWORD = "org.apache.servicemix.mail.password";
+
+    /**
+     * this tag is used in normalized messages to represent the mail server hostname
+     */
+    public static final String MSG_TAG_HOST = "org.apache.servicemix.mail.host";
+
+    /**
+     * this tag is used in normalized messages to represent the used protocol
+     */
+    public static final String MSG_TAG_PROTOCOL = "org.apache.servicemix.mail.protocol";
+
+    /**
+     * this tag is used in normalized messages to represent the port number used for mail server
+     */
+    public static final String MSG_TAG_PORT = "org.apache.servicemix.mail.port";
+
+    /**
      * the dummy subject - set if no subject was provided
      */
     public static final String DUMMY_SUBJECT = "no subject";

Modified: servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/utils/MailConnectionConfiguration.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/utils/MailConnectionConfiguration.java?rev=995696&r1=995695&r2=995696&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/utils/MailConnectionConfiguration.java (original)
+++ servicemix/components/bindings/servicemix-mail/trunk/src/main/java/org/apache/servicemix/mail/utils/MailConnectionConfiguration.java Fri Sep 10 08:19:51 2010
@@ -127,12 +127,23 @@ public class MailConnectionConfiguration
                || this.protocol.equalsIgnoreCase("imaps");
     }
 
-    /**
+     /**
      * returns an authenticator object for use in sessions
-     * 
+     *
      * @return  the authenticator object
      */
     public Authenticator getAuthenticator() {
+        return getAuthenticator(getUsername(), getPassword());
+    }
+
+    /**
+     * returns an authenticator object for use in sessions
+     *
+     * @param user  the user name
+     * @param password  the password
+     * @return  the authenticator object
+     */
+    public Authenticator getAuthenticator(final String user, final String password) {
         return new Authenticator() {
             /*
              * (non-Javadoc)
@@ -141,7 +152,7 @@ public class MailConnectionConfiguration
              */
             @Override
             protected PasswordAuthentication getPasswordAuthentication() {
-                return new PasswordAuthentication(getUsername(), getPassword());
+                return new PasswordAuthentication(user, password);
             }
         };
     }