[mashup-dev] svn commit r9747 - trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/email

svn at wso2.org svn at wso2.org
Tue Nov 13 23:07:07 PST 2007


Author: keith
Date: Tue Nov 13 23:06:58 2007
New Revision: 9747

Modified:
   trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/email/Email.java
Log:
Enabling to give the login credentials at runtime



Modified: trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/email/Email.java
==============================================================================
--- trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/email/Email.java	(original)
+++ trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/email/Email.java	Tue Nov 13 23:06:58 2007
@@ -57,29 +57,59 @@
         Email email = new Email();
         Properties props = new Properties();
         email.properties = props;
-        SMTPAuthenticator smtpAuthenticator = new SMTPAuthenticator();
-        if (smtpAuthenticator.getPasswordAuthentication() != null) {
-            email.setProperty("mail.smtp.auth", "true");
-        }
-        Session session = Session.getDefaultInstance(props, smtpAuthenticator);
-        email.message = new MimeMessage(session);
-
-        email.setProperty("mail.smtp.starttls.enable", "true");
 
+        String host, port, username, password;
         ServerConfiguration serverConfig = ServerConfiguration.getInstance();
 
-        String host = serverConfig.getFirstProperty(MashupConstants.HOST);
+        int length = args.length;
+        if (length == 0) {
+            host = serverConfig.getFirstProperty(MashupConstants.HOST);
+            port = serverConfig.getFirstProperty(MashupConstants.PORT);
+            username = serverConfig.getFirstProperty(MashupConstants.EMAIL_USERNAME);
+            password = serverConfig.getFirstProperty(MashupConstants.EMAIL_PASSWORD);
+        } else if (length == 2) {
+            // We assume that the two parameters are host and port
+            host = (String) args[0];
+            port = (String) args[1];
+            username = serverConfig.getFirstProperty(MashupConstants.EMAIL_USERNAME);
+            password = serverConfig.getFirstProperty(MashupConstants.EMAIL_PASSWORD);
+        } else if (length == 3) {
+            // We assume that the three parameters are host, username and password
+            host = (String) args[0];
+            username = (String) args[1];
+            password = (String) args[2];
+            port = serverConfig.getFirstProperty(MashupConstants.PORT);
+        } else if (length == 4) {
+            //We assume that the parameters are host, port, username and password
+            host = (String) args[0];
+            port = (String) args[1];
+            username = (String) args[2];
+            password = (String) args[3];
+        } else {
+            throw new MashupFault("Incorrect number of arguments. If you want to use login " +
+                    "details specified in the server.xml call this method with 0 parameters.");
+        }
+
         if (host == null) {
             throw new MashupFault("The details needed to send emails shuld be given in the " +
                     "server.xml. PLease check your server.xml found in the conf dir.");
         }
         email.setProperty("mail.smtp.host", host);
 
-        String port = serverConfig.getFirstProperty(MashupConstants.PORT);
         if (port != null) {
             email.setProperty("mail.smtp.port", port);
         }
 
+        SMTPAuthenticator smtpAuthenticator = null;
+        if (username != null){
+            smtpAuthenticator = new SMTPAuthenticator(username, password);
+            email.setProperty("mail.smtp.auth", "true");
+        }
+        Session session = Session.getDefaultInstance(props, smtpAuthenticator);
+        email.message = new MimeMessage(session);
+
+        email.setProperty("mail.smtp.starttls.enable", "true");
+
         return email;
     }
 
@@ -201,16 +231,14 @@
 
     private static class SMTPAuthenticator extends javax.mail.Authenticator {
 
-        public PasswordAuthentication getPasswordAuthentication() {
+        private String username, password;
 
-            String username, password;
-            ServerConfiguration serverConfig = ServerConfiguration.getInstance();
+        private SMTPAuthenticator(String username, String password) {
+            this.username = username;
+            this.password = password;
+        }
 
-            username = serverConfig.getFirstProperty(MashupConstants.EMAIL_USERNAME);
-            if (username == null) {
-                return null;
-            }
-            password = serverConfig.getFirstProperty(MashupConstants.EMAIL_PASSWORD);
+        public PasswordAuthentication getPasswordAuthentication() {
             return new PasswordAuthentication(username, password);
         }
     }




More information about the Mashup-dev mailing list