You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jl...@apache.org on 2006/03/02 15:18:26 UTC

svn commit: r382387 - in /geronimo/specs/trunk/geronimo-spec-javamail/src: main/java/javax/mail/internet/InternetAddress.java test/java/javax/mail/internet/InternetAddressTest.java

Author: jlaskowski
Date: Thu Mar  2 06:18:24 2006
New Revision: 382387

URL: http://svn.apache.org/viewcvs?rev=382387&view=rev
Log:
GERONIMO-1671: InternetAddress.getLocalAddress() does not properly implement the local address resolution path.

Thanks Rick for the patch (and taking care of my local issues)!


Modified:
    geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/javax/mail/internet/InternetAddress.java
    geronimo/specs/trunk/geronimo-spec-javamail/src/test/java/javax/mail/internet/InternetAddressTest.java

Modified: geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/javax/mail/internet/InternetAddress.java
URL: http://svn.apache.org/viewcvs/geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/javax/mail/internet/InternetAddress.java?rev=382387&r1=382386&r2=382387&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/javax/mail/internet/InternetAddress.java (original)
+++ geronimo/specs/trunk/geronimo-spec-javamail/src/main/java/javax/mail/internet/InternetAddress.java Thu Mar  2 06:18:24 2006
@@ -27,6 +27,8 @@
 import javax.mail.Address;
 import javax.mail.Session;
 
+import org.apache.geronimo.mail.util.SessionUtil;
+
 /**
  * A representation of an Internet email address as specified by RFC822 in
  * conjunction with a human-readable personal name that can be encoded as
@@ -378,36 +380,51 @@
      * @return an InternetAddress for the current user, or null if it cannot be determined
      */
     public static InternetAddress getLocalAddress(Session session) {
-        String address = null;
+        String host = null;
+        String user = null;
+
+        // ok, we have several steps for resolving this.  To start with, we could have a from address
+        // configured already, which will be a full InternetAddress string.  If we don't have that, then
+        // we need to resolve a user and host to compose an address from.
         if (session != null) {
-            address = session.getProperty("mail.from");
-            if (address == null) {
-                String user = session.getProperty("mail.user");
-                String host = session.getProperty("mail.host");
-                if (user != null && host != null) {
-                    address = user + '@' + host;
+            String address = session.getProperty("mail.from");
+            // if we got this, we can skip out now
+            if (address != null) {
+                try {
+                    return new InternetAddress(address);
+                } catch (AddressException e) {
+                    // invalid address on the from...treat this as an error and return null.
+                    return null;
                 }
             }
+
+            // now try for user and host information.  We have both session and system properties to check here.
+            // we'll just handle the session ones here, and check the system ones below if we're missing information.
+            user = session.getProperty("mail.user");
+            host = session.getProperty("mail.host");
         }
-        if (address == null) {
-            try {
-                String user = System.getProperty("user.name");
-                String host = InetAddress.getLocalHost().getHostName();
-                if (user != null && host != null) {
-                    address = user + '@' + host;
-                }
-            } catch (UnknownHostException e) {
-                // ignore
-            } catch (SecurityException e) {
-                // ignore
+
+        try {
+
+            // if either user or host is null, then we check non-session sources for the information.
+            if (user == null) {
+                user = System.getProperty("user.name");
             }
-        }
-        if (address != null) {
-            try {
-                return new InternetAddress(address);
-            } catch (AddressException e) {
-                // ignore
+
+            if (host == null) {
+                host = InetAddress.getLocalHost().getHostName();
+            }
+
+            if (user != null && host != null) {
+                // if we have both a user and host, we can create a local address
+                return new InternetAddress(user + '@' + host);
             }
+        } catch (AddressException e) {
+            // ignore
+        } catch (UnknownHostException e) {
+            // ignore
+        } catch (SecurityException e) {
+            // ignore
         }
         return null;
     }

Modified: geronimo/specs/trunk/geronimo-spec-javamail/src/test/java/javax/mail/internet/InternetAddressTest.java
URL: http://svn.apache.org/viewcvs/geronimo/specs/trunk/geronimo-spec-javamail/src/test/java/javax/mail/internet/InternetAddressTest.java?rev=382387&r1=382386&r2=382387&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-spec-javamail/src/test/java/javax/mail/internet/InternetAddressTest.java (original)
+++ geronimo/specs/trunk/geronimo-spec-javamail/src/test/java/javax/mail/internet/InternetAddressTest.java Thu Mar  2 06:18:24 2006
@@ -17,6 +17,12 @@
 
 package javax.mail.internet;
 import junit.framework.TestCase;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Properties;
+
+import javax.mail.Session;
 /**
  * @version $Rev$ $Date$
  */
@@ -378,6 +384,58 @@
 
         addresses = getGroup("Foo:;", false);
         assertTrue("Expecting 0 addresses", addresses.length == 0);
+    }
+
+
+    public void testLocalAddress() throws Exception {
+        System.getProperties().remove("user.name");
+
+        assertNull(InternetAddress.getLocalAddress(null));
+        System.setProperty("user.name", "dev");
+
+        InternetAddress localHost = null;
+        String user = null;
+        String host = null;
+        try {
+            user = System.getProperty("user.name");
+            host = InetAddress.getLocalHost().getHostName();
+
+            localHost = new InternetAddress(user + "@" + host);
+        } catch (AddressException e) {
+            // ignore
+        } catch (UnknownHostException e) {
+            // ignore
+        } catch (SecurityException e) {
+            // ignore
+        }
+
+        assertEquals(InternetAddress.getLocalAddress(null), localHost);
+
+        Properties props = new Properties();
+        Session session = Session.getInstance(props, null);
+
+        assertEquals(InternetAddress.getLocalAddress(session), localHost);
+
+        props.put("mail.host", "apache.org");
+        session = Session.getInstance(props, null);
+
+        assertEquals(InternetAddress.getLocalAddress(session), new InternetAddress(user + "@apache.org"));
+
+        props.put("mail.user", "user");
+        props.remove("mail.host");
+
+        session = Session.getInstance(props, null);
+        assertEquals(InternetAddress.getLocalAddress(session), new InternetAddress("user@" + host));
+
+        props.put("mail.host", "apache.org");
+        session = Session.getInstance(props, null);
+
+        assertEquals(InternetAddress.getLocalAddress(session), new InternetAddress("user@apache.org"));
+
+        props.put("mail.from", "tester@incubator.apache.org");
+        session = Session.getInstance(props, null);
+
+        assertEquals(InternetAddress.getLocalAddress(session), new InternetAddress("tester@incubator.apache.org"));
     }
 
     private InternetAddress[] getGroup(String address, boolean strict) throws AddressException