Browse Source

SEC-258: Removed use of URI class

Luke Taylor 19 years ago
parent
commit
91f5fc30be

+ 34 - 23
core/src/main/java/org/acegisecurity/ldap/LdapUtils.java

@@ -22,8 +22,6 @@ import org.springframework.util.Assert;
 import javax.naming.Context;
 import javax.naming.NamingException;
 import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.net.URISyntaxException;
 
 /**
  * LDAP Utility methods.
@@ -48,24 +46,6 @@ public class LdapUtils {
         }
     }
 
-    /**
-     * Parses the supplied LDAP URL.
-     * @param url the URL (e.g. <tt>ldap://monkeymachine:11389/dc=acegisecurity,dc=org</tt>).
-     * @return the URI object created from the URL
-     * @throws IllegalArgumentException if the URL is null, empty or the URI syntax is invalid.
-     */
-    public static URI parseLdapUrl(String url) {
-        Assert.hasLength(url);
-
-        try {
-            return new URI(url);
-        } catch (URISyntaxException e) {
-            IllegalArgumentException iae = new IllegalArgumentException("Unable to parse url: " + url);
-            iae.initCause(e);
-            throw iae;
-        }
-    }
-
     public static byte[] getUtf8Bytes(String s) {
         try {
             return s.getBytes("UTF-8");
@@ -127,14 +107,24 @@ public class LdapUtils {
     public static String parseRootDnFromUrl(String url) {
         Assert.hasLength(url);
 
-        String urlRootDn = null;
+        String urlRootDn = "";
 
         if (url.startsWith("ldap:") || url.startsWith("ldaps:")) {
 
-            URI uri = parseLdapUrl(url);
+//            URI uri = parseLdapUrl(url);
 
-            urlRootDn = uri.getPath();
+//            urlRootDn = uri.getPath();
+            // skip past the "://"
+            int colon = url.indexOf(':');
 
+            url = url.substring(colon + 3);
+
+            // Match the slash at the end of the address (if there)
+            int slash = url.indexOf('/');
+
+            if(slash >= 0) {
+                urlRootDn = url.substring(slash);
+            }
         } else {
             // Assume it's an embedded server
             urlRootDn = url;
@@ -146,4 +136,25 @@ public class LdapUtils {
 
         return urlRootDn;
     }
+
+    // removed for 1.3 compatibility
+
+    /**
+     * Parses the supplied LDAP URL.
+     * @param url the URL (e.g. <tt>ldap://monkeymachine:11389/dc=acegisecurity,dc=org</tt>).
+     * @return the URI object created from the URL
+     * @throws IllegalArgumentException if the URL is null, empty or the URI syntax is invalid.
+     */
+//    private static URI parseLdapUrl(String url) {
+//        Assert.hasLength(url);
+//
+//        try {
+//            return new URI(url);
+//        } catch (URISyntaxException e) {
+//            IllegalArgumentException iae = new IllegalArgumentException("Unable to parse url: " + url);
+//            iae.initCause(e);
+//            throw iae;
+//        }
+//    }
+
 }

+ 36 - 0
core/src/test/java/org/acegisecurity/ldap/LdapUtilsTests.java

@@ -0,0 +1,36 @@
+/* Copyright 2004, 2005 Acegi Technology Pty Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.acegisecurity.ldap;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests {@link LdapUtils}
+ *
+ * @author Luke Taylor
+ * @version $Id$
+ */
+public class LdapUtilsTests extends TestCase {
+
+    public void testRootDnsAreParsedFromUrlsCorrectly() {
+        assertEquals("", LdapUtils.parseRootDnFromUrl("ldap://monkeymachine"));
+        assertEquals("", LdapUtils.parseRootDnFromUrl("ldap://monkeymachine/"));
+        assertEquals("", LdapUtils.parseRootDnFromUrl("ldap://monkeymachine.co.uk/"));
+        assertEquals("dc=acegisecurity,dc=org", LdapUtils.parseRootDnFromUrl("ldaps://monkeymachine.co.uk/dc=acegisecurity,dc=org"));
+        assertEquals("dc=acegisecurity,dc=org", LdapUtils.parseRootDnFromUrl("ldap:///dc=acegisecurity,dc=org"));
+        assertEquals("dc=acegisecurity,dc=org", LdapUtils.parseRootDnFromUrl("ldap://monkeymachine/dc=acegisecurity,dc=org"));
+        assertEquals("dc=acegisecurity,dc=org/ou=blah", LdapUtils.parseRootDnFromUrl("ldap://monkeymachine.co.uk/dc=acegisecurity,dc=org/ou=blah"));
+    }
+}