geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickmcgu...@apache.org
Subject svn commit: r593244 - in /geronimo/specs/trunk/geronimo-javamail_1.4_spec/src: main/java/javax/mail/URLName.java test/java/javax/mail/URLNameTest.java
Date Thu, 08 Nov 2007 18:31:49 GMT
Author: rickmcguire
Date: Thu Nov  8 10:31:48 2007
New Revision: 593244

URL: http://svn.apache.org/viewvc?rev=593244&view=rev
Log:
GERONIMO-3591 javamail URLName() class does not HTTP encode URL username elements


Modified:
    geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/main/java/javax/mail/URLName.java
    geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/test/java/javax/mail/URLNameTest.java

Modified: geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/main/java/javax/mail/URLName.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/main/java/javax/mail/URLName.java?rev=593244&r1=593243&r2=593244&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/main/java/javax/mail/URLName.java
(original)
+++ geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/main/java/javax/mail/URLName.java
Thu Nov  8 10:31:48 2007
@@ -19,6 +19,7 @@
 
 package javax.mail;
 
+import java.io.ByteArrayOutputStream;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -28,6 +29,8 @@
  * @version $Rev$ $Date$
  */
 public class URLName {
+    private static final String nonEncodedChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-.*";
+    
     private String file;
     private String host;
     private String password;
@@ -109,6 +112,8 @@
         } else {
             this.password = null;
         }
+        username = encode(username); 
+        password = encode(password); 
         updateFullURL();
     }
 
@@ -151,9 +156,9 @@
             if (host != null) {
                 buf.append("//");
                 if (username != null) {
-                    buf.append(username);
+                    buf.append(encode(username));
                     if (password != null) {
-                        buf.append(':').append(password);
+                        buf.append(':').append(encode(password));
                     }
                     buf.append('@');
                 }
@@ -237,5 +242,69 @@
 
     public String getUsername() {
         return username;
+    }
+    
+    /**
+     * Perform an HTTP encoding to the username and 
+     * password elements of the URLName.  
+     * 
+     * @param v      The input (uncoded) string.
+     * 
+     * @return The HTTP encoded version of the string. 
+     */
+    private static String encode(String v) {
+        // make sure we don't operate on a null string
+        if (v == null) {
+            return null; 
+        }
+        boolean needsEncoding = false; 
+        for (int i = 0; i < v.length(); i++) {
+            // not in the list of things that don't need encoding?
+            if (nonEncodedChars.indexOf(v.charAt(i)) == -1) {
+                // got to do this the hard way
+                needsEncoding = true; 
+                break; 
+            }
+        }
+        // just fine the way it is. 
+        if (!needsEncoding) {
+            return v; 
+        }
+        
+        // we know we're going to be larger, but not sure by how much.  
+        // just give a little extra
+        StringBuffer encoded = new StringBuffer(v.length() + 10);
+            
+        // we get the bytes so that we can have the default encoding applied to 
+        // this string.  This will flag the ones we need to give special processing to. 
+        byte[] data = v.getBytes(); 
+        
+        for (int i = 0; i < data.length; i++) {
+            // pick this up as a one-byte character The 7-bit ascii ones will be fine 
+            // here. 
+            char ch = (char)(data[i] & 0xff); 
+            // blanks get special treatment 
+            if (ch == ' ') {
+                encoded.append('+'); 
+            }
+            // not in the list of things that don't need encoding?
+            else if (nonEncodedChars.indexOf(ch) == -1) {
+                // forDigit() uses the lowercase letters for the radix.  The HTML specifications

+                // require the uppercase letters. 
+                char firstChar = Character.toUpperCase(Character.forDigit((ch >> 4)
& 0xf, 16)); 
+                char secondChar = Character.toUpperCase(Character.forDigit(ch & 0xf,
16)); 
+                
+                // now append the encoded triplet. 
+                encoded.append('%'); 
+                encoded.append(firstChar); 
+                encoded.append(secondChar); 
+            }
+            else {
+                // just add this one to the buffer 
+                encoded.append(ch); 
+            }
+        }
+        // convert to string form. 
+        return encoded.toString(); 
     }
 }

Modified: geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/test/java/javax/mail/URLNameTest.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/test/java/javax/mail/URLNameTest.java?rev=593244&r1=593243&r2=593244&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/test/java/javax/mail/URLNameTest.java
(original)
+++ geronimo/specs/trunk/geronimo-javamail_1.4_spec/src/test/java/javax/mail/URLNameTest.java
Thu Nov  8 10:31:48 2007
@@ -115,6 +115,22 @@
         } catch (MalformedURLException e) {
             fail();
         }
+        
+        s = "http://john%40gmail.com:doe@www.apache.org/file/";
+        name = new URLName(s);
+        assertEquals(s, name.toString());
+        assertEquals("http", name.getProtocol());
+        assertEquals("www.apache.org", name.getHost());
+        assertEquals(-1, name.getPort());
+        assertEquals("/file/", name.getFile());
+        assertNull(name.getRef());
+        assertEquals("john@gmail.com", name.getUsername());
+        assertEquals("doe", name.getPassword());
+        try {
+            assertEquals(new URL(s), name.getURL());
+        } catch (MalformedURLException e) {
+            fail();
+        }
 
         s = "file/file2";
         name = new URLName(s);
@@ -266,6 +282,21 @@
         assertEquals("doe", name.getPassword());
         try {
             assertEquals(new URL("http://john:doe@www.apache.org/file/file2"), name.getURL());
+        } catch (MalformedURLException e) {
+            fail();
+        }
+
+        name = new URLName("http", "www.apache.org", -1, "/file/file2", "john@gmail.com",
"doe");
+        assertEquals("http://john%40gmail.com:doe@www.apache.org/file/file2", name.toString());
+        assertEquals("http", name.getProtocol());
+        assertEquals("www.apache.org", name.getHost());
+        assertEquals(-1, name.getPort());
+        assertEquals("/file/file2", name.getFile());
+        assertNull(name.getRef());
+        assertEquals("john@gmail.com", name.getUsername());
+        assertEquals("doe", name.getPassword());
+        try {
+            assertEquals(new URL("http://john%40gmail.com:doe@www.apache.org/file/file2"),
name.getURL());
         } catch (MalformedURLException e) {
             fail();
         }



Mime
View raw message