You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ba...@apache.org on 2006/05/13 14:41:40 UTC

svn commit: r406086 - in /james/server/trunk/src: java/org/apache/james/transport/mailets/RemoteDeliverySocketFactory.java test/org/apache/james/smtpserver/SMTPServerTest.java

Author: bago
Date: Sat May 13 05:41:39 2006
New Revision: 406086

URL: http://svn.apache.org/viewcvs?rev=406086&view=rev
Log:
Fix regression introduced with Javamail 1.4: using bind in remoteDelivery resulted in failing all connections (JAMES-490)
Introduced a functional test as proof: I added it to the SMTPServerTest because it was faster, but this should be considered a RemoteDelivery test.

Modified:
    james/server/trunk/src/java/org/apache/james/transport/mailets/RemoteDeliverySocketFactory.java
    james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java

Modified: james/server/trunk/src/java/org/apache/james/transport/mailets/RemoteDeliverySocketFactory.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/java/org/apache/james/transport/mailets/RemoteDeliverySocketFactory.java?rev=406086&r1=406085&r2=406086&view=diff
==============================================================================
--- james/server/trunk/src/java/org/apache/james/transport/mailets/RemoteDeliverySocketFactory.java (original)
+++ james/server/trunk/src/java/org/apache/james/transport/mailets/RemoteDeliverySocketFactory.java Sat May 13 05:41:39 2006
@@ -17,10 +17,13 @@
 
 package org.apache.james.transport.mailets;
 
+import javax.net.SocketFactory;
+
+import java.io.IOException;
 import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.net.UnknownHostException;
-import java.io.IOException;
 
 /**
  * It is used by RemoteDelivery in order to make possible to bind the client
@@ -31,12 +34,21 @@
  * (current version 1.3) to support a corresonding property, e.g.
  * mail.smtp.bindAdress.
  * 
- * It should be a javax.net.SocketFactory descendant, but 
- * 1. it is not necessary because JavaMail 1.2 uses reflection when accessing
+ * This used to not extend javax.net.SocketFactory descendant, because 
+ * 1. it was not necessary because JavaMail 1.2 uses reflection when accessing
  * this class;
- * 2. it is not desirable because it would require java 1.4.
+ * 2. it was not desirable because it would require java 1.4.
+ * 
+ * But since James 2.3.0a1:
+ * 1. we require Java 1.4 so the dependency on SocketFactory is
+ * not really an issue;
+ * 2. Javamail 1.4 cast the object returned by getDefault to SocketFactory and
+ * fails to create the socket if we don't extend SocketFactory.
+ * 
+ * Note: Javamail 1.4 should correctly support mail.smtp.localaddr so we could
+ * probably get rid of this class and simply add that property to the Session.
  */
-public class RemoteDeliverySocketFactory {
+public class RemoteDeliverySocketFactory extends SocketFactory {
     
     /**
      * @param addr the ip address or host name the delivery socket will bind to
@@ -49,22 +61,25 @@
     /**
      * the same as the similarly named javax.net.SocketFactory operation.
      */
-    public static RemoteDeliverySocketFactory getDefault() {
+    public static SocketFactory getDefault() {
         return new RemoteDeliverySocketFactory();
     }
     
     /**
      * the same as the similarly named javax.net.SocketFactory operation.
      * Just to be safe, it is not used by JavaMail 1.3.
+     * This is the only method used by JavaMail 1.4.
      */
     public Socket createSocket() throws IOException {
-        throw new IOException("Incompatible JavaMail version, " +
-                "cannot bound socket");
+        Socket s = new Socket();
+        s.bind(new InetSocketAddress(bindAddress, 0));
+        return s;
     }
     
     /**
      * the same as the similarly named javax.net.SocketFactory operation.
      * This is the one which is used by JavaMail 1.3.
+     * This is not used by JavaMail 1.4.
      */
     public Socket createSocket(String host, int port)
                             throws IOException, UnknownHostException {
@@ -74,6 +89,7 @@
     /**
      * the same as the similarly named javax.net.SocketFactory operation.
      * Just to be safe, it is not used by JavaMail 1.3.
+     * This is not used by JavaMail 1.4.
      */
     public Socket createSocket(String host,
                                     int port,
@@ -88,6 +104,7 @@
     /**
      * the same as the similarly named javax.net.SocketFactory operation.
      * Just to be safe, it is not used by JavaMail 1.3.
+     * This is not used by JavaMail 1.4.
      */
     public Socket createSocket(InetAddress host, int port) throws IOException {
         return new Socket(host, port, bindAddress, 0);
@@ -96,6 +113,7 @@
     /**
      * the same as the similarly named javax.net.SocketFactory operation.
      * Just to be safe, it is not used by JavaMail 1.3.
+     * This is not used by JavaMail 1.4.
      */
     public Socket createSocket(InetAddress address,
                                     int port,

Modified: james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java
URL: http://svn.apache.org/viewcvs/james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java?rev=406086&r1=406085&r2=406086&view=diff
==============================================================================
--- james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java (original)
+++ james/server/trunk/src/test/org/apache/james/smtpserver/SMTPServerTest.java Sat May 13 05:41:39 2006
@@ -912,6 +912,45 @@
     }
     
 
+    /**
+     * This is useful code to run tests on javamail bugs 
+     * http://issues.apache.org/jira/browse/JAMES-52
+     * 
+     * This 
+     * @throws Exception
+     */
+    public void testDeliveryToSelfWithGatewayAndBind() throws Exception {
+        finishSetUp(m_testConfiguration);
+        outgoingSpool = new InMemorySpoolRepository();
+        ((MockStore) m_serviceManager.lookup(Store.ROLE)).add("outgoing", outgoingSpool);
+        
+        RemoteDelivery rd = new RemoteDelivery();
+        
+        MockMailContext mmc = new MockMailContext();
+        mmc.setAttribute(Constants.AVALON_COMPONENT_MANAGER,m_serviceManager);
+        mmc.setAttribute(Constants.HELLO_NAME,"localhost");
+        MockMailetConfig mci = new MockMailetConfig("Test",mmc,getStandardParameters());
+        mci.setProperty("bind", "127.0.0.1");
+        mci.setProperty("gateway","127.0.0.1");
+        mci.setProperty("gatewayPort",""+m_smtpListenerPort);
+        rd.init(mci);
+        String sources = "Content-Type: text/plain;\r\nSubject: test\r\n\r\nBody";
+        String sender = "test@localhost";
+        String recipient = "test@localhost";
+        MimeMessage mm = new MimeMessage(Session.getDefaultInstance(new Properties()),new ByteArrayInputStream(sources.getBytes()));
+        MailImpl mail = new MailImpl("name",new MailAddress(sender),Arrays.asList(new MailAddress[] {new MailAddress(recipient)}),mm);
+        
+        rd.service(mail);
+        
+        while (outgoingSpool.size() > 0) {
+            Thread.sleep(1000);
+        }
+
+        verifyLastMail(sender, recipient, null);
+        
+        assertEquals(((String) mm.getContent()).trim(),((String) ((MimeMessage) m_mailServer.getLastMail()[2]).getContent()).trim());
+    }
+
     
     /**
      * This is useful code to run tests on javamail bugs 



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org