directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r160795 - in directory/naming/trunk: naming-config/src/test/org/apache/naming/config/XmlConfiguratorTest.java naming-factory/src/java/org/apache/naming/factory/MailSessionFactory.java xdocs/changes.xml
Date Sun, 10 Apr 2005 18:47:19 GMT
Author: psteitz
Date: Sun Apr 10 11:47:18 2005
New Revision: 160795

URL: http://svn.apache.org/viewcvs?view=rev&rev=160795
Log:
Ported tomcat patch from BZ 31288, adding support for authenticated mail
sessions.

Added sample config and (deactivated) test cases for MailSession,
SendMail factories.

Modified:
    directory/naming/trunk/naming-config/src/test/org/apache/naming/config/XmlConfiguratorTest.java
    directory/naming/trunk/naming-factory/src/java/org/apache/naming/factory/MailSessionFactory.java
    directory/naming/trunk/xdocs/changes.xml

Modified: directory/naming/trunk/naming-config/src/test/org/apache/naming/config/XmlConfiguratorTest.java
URL: http://svn.apache.org/viewcvs/directory/naming/trunk/naming-config/src/test/org/apache/naming/config/XmlConfiguratorTest.java?view=diff&r1=160794&r2=160795
==============================================================================
--- directory/naming/trunk/naming-config/src/test/org/apache/naming/config/XmlConfiguratorTest.java
(original)
+++ directory/naming/trunk/naming-config/src/test/org/apache/naming/config/XmlConfiguratorTest.java
Sun Apr 10 11:47:18 2005
@@ -24,7 +24,15 @@
 import java.util.Hashtable;
 
 import javax.naming.Context;
-import javax.naming.InitialContext;
+import javax.naming.InitialContext;
+
+import javax.mail.Session;
+import javax.mail.Message;
+import javax.mail.internet.MimePartDataSource;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import javax.mail.Transport;
+
 import javax.sql.DataSource;
 
 import junit.framework.TestCase;
@@ -222,6 +230,85 @@
                 con.close(); 
             }
         }      
+    }
+    
+    /**
+     * Mail factory configuration tests.  Currently disabled, as the tests
+     * and associated configs require Javamail libraries and access to outbound
+     * smpt servers and send email messages. 
+     * 
+     * For these test cases to work, both javamail.jar and activation.jar must be
+     * available and "touser@todomain.com" needs to be a real email address.
+     * NB: These test cases will send actual email messages to the to address.
+     * 
+     * To activate these tests, change the names from "tstXxx" to "testXxx".
+     */
+
+    /**
+     * Test for correctly configured and operational mail 
+     * session factory.  Does not use authorization config.
+     * 
+     * @throws Exception as tests do
+     */
+    public void tstMailSession() throws Exception {
+        XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/mail.xml"));
+        Context ctx = new InitialContext();
+        Context env = (Context) ctx.lookup("java:comp/env");
+        Session session = (Session) env.lookup("mail/smtp");
+        Message msg = new MimeMessage(session);
+        msg.setFrom(new InternetAddress("fromuser@fromdomain.com"));
+        InternetAddress to[] = new InternetAddress[1];
+        to[0] = new InternetAddress("touser@todomain.com");
+        msg.setRecipients(Message.RecipientType.TO, to);
+        msg.setSubject("mail session test");
+        msg.setContent("this is a test", "text/plain");
+        Transport.send(msg);
+    }        
+    
+    /**
+     * Test for correctly configured and operational send mail 
+     * resource factory. 
+     * 
+     * @throws Exception as tests do
+     */
+    public void tstSendMail() throws Exception {
+        XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/mail.xml"));
+        Context ctx = new InitialContext();
+        Context env = (Context) ctx.lookup("java:comp/env");
+        MimePartDataSource ds = (MimePartDataSource) env.lookup("mail/send");
+        Message msg = ds.getMessageContext().getMessage();
+        msg.setFrom(new InternetAddress("fromuser@fromdomain.com"));
+        InternetAddress to[] = new InternetAddress[1];
+        to[0] = new InternetAddress("touser@todomain.com");
+        msg.setRecipients(Message.RecipientType.TO, to);
+        msg.setSubject("sendmail test");
+        msg.setContent("this is a test", "text/plain");
+        Transport.send(msg);
+    }
+    
+    /**
+     * Test for correctly configured and operational send mail 
+     * resource factory.  Uses auth config, passing mail.smpt.user
+     * and password from config to mail session factory.
+     * 
+     * @throws Exception as tests do
+     */
+    public void tstSendMailAuth() throws Exception {
+        XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/mail.xml"));
+        Context ctx = new InitialContext();
+        Context env = (Context) ctx.lookup("java:comp/env");
+        Session session = (Session) env.lookup("mail/smtp-auth");
+        Message msg = new MimeMessage(session);
+        /* fromuser@todomain.com must be allowed to send authenticated
+            mail from the configured smpt host.  Generally, that means that
+            mail.smpt.user = fromuser. */
+        msg.setFrom(new InternetAddress("fromuser@todomain.com"));
+        InternetAddress to[] = new InternetAddress[1];
+        to[0] = new InternetAddress("touser@todomain.com");
+        msg.setRecipients(Message.RecipientType.TO, to);
+        msg.setSubject("authenticated mail session test");
+        msg.setContent("this is a test", "text/plain");
+        Transport.send(msg);
     }
 }
 

Modified: directory/naming/trunk/naming-factory/src/java/org/apache/naming/factory/MailSessionFactory.java
URL: http://svn.apache.org/viewcvs/directory/naming/trunk/naming-factory/src/java/org/apache/naming/factory/MailSessionFactory.java?view=diff&r1=160794&r2=160795
==============================================================================
--- directory/naming/trunk/naming-factory/src/java/org/apache/naming/factory/MailSessionFactory.java
(original)
+++ directory/naming/trunk/naming-factory/src/java/org/apache/naming/factory/MailSessionFactory.java
Sun Apr 10 11:47:18 2005
@@ -21,6 +21,9 @@
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Properties;
+
+import javax.mail.Authenticator;
+import javax.mail.PasswordAuthentication;
 import javax.mail.Session;
 import javax.naming.Name;
 import javax.naming.Context;
@@ -92,7 +95,8 @@
         // can read its default properties without throwing Security
         // exceptions
         return AccessController.doPrivileged( new PrivilegedAction() {
-		public Object run() {
+        public Object run() {
+                    String password = null;
 
                     // Create the JavaMail properties we will use
                     Properties props = new Properties();
@@ -103,15 +107,37 @@
                         RefAddr attr = (RefAddr) attrs.nextElement();
                         if ("factory".equals(attr.getType()))
                             continue;
+                        if ("password".equals(attr.getType())) {
+                            password = (String)attr.getContent();
+                            continue;
+                        }
                         props.put(attr.getType(), (String) attr.getContent());
                     }
-
+                    
+                    Authenticator auth = null;
+                    
+                    // Check for authentication
+                    if (password != null) {
+                        String user = props.getProperty("mail.smtp.user");
+                        if (user == null)
+                            user = props.getProperty("mail.user");
+                        if (user != null) {
+                            final PasswordAuthentication pa = new PasswordAuthentication(user,
password);
+                            
+                            auth = new Authenticator(){
+                                protected PasswordAuthentication getPasswordAuthentication()
+                                {
+                                    return pa;
+                                }
+                            };
+                        }
+                    }
                     // Create and return the new Session object
-                    Session session = Session.getInstance(props, null);
+                    Session session = Session.getInstance(props, auth);
                     return (session);
 
-		}
-	    } );
+        }
+        } );
 
     }
 

Modified: directory/naming/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewcvs/directory/naming/trunk/xdocs/changes.xml?view=diff&r1=160794&r2=160795
==============================================================================
--- directory/naming/trunk/xdocs/changes.xml (original)
+++ directory/naming/trunk/xdocs/changes.xml Sun Apr 10 11:47:18 2005
@@ -39,7 +39,7 @@
   <body>
     <release version="0.9" date="TBD"  
        description="Apache Directory Naming 0.9 - General Availability Release">
-       <action dev="psteitz" type="update" issue="DIRNAMING-11">
+      <action dev="psteitz" type="update" issue="DIRNAMING-11">
           Modified NamingContextFactory to persist contexts in ContextBindings.
           When using NamingContextFactory, a NAME property may be specified to 
           retrieve or create a named context. 
@@ -57,6 +57,10 @@
           jndiURLContextFactory to handle jndi URL scheme and 
           SelectorJNDIContext to manage jndi: namespace, stripping headers and
           delegating JNDI operation to named contexts.
+      </action>
+      <action dev="psteitz" type="update">
+          Ported tomcat patch from BZ 31288, adding support for authenticated
+          mail sessions.
       </action>
     </release>     
     <release version="0.8" date="2005-01-15" 



Mime
View raw message