From 91f5fc30be5412d41ffe786e7dbe799dd8ab5bf2 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Sun, 30 Apr 2006 19:45:37 +0000 Subject: [PATCH] SEC-258: Removed use of URI class --- .../org/acegisecurity/ldap/LdapUtils.java | 57 +++++++++++-------- .../acegisecurity/ldap/LdapUtilsTests.java | 36 ++++++++++++ 2 files changed, 70 insertions(+), 23 deletions(-) create mode 100644 core/src/test/java/org/acegisecurity/ldap/LdapUtilsTests.java diff --git a/core/src/main/java/org/acegisecurity/ldap/LdapUtils.java b/core/src/main/java/org/acegisecurity/ldap/LdapUtils.java index 419b14ce5a..a6c2a7853f 100644 --- a/core/src/main/java/org/acegisecurity/ldap/LdapUtils.java +++ b/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. ldap://monkeymachine:11389/dc=acegisecurity,dc=org). - * @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. ldap://monkeymachine:11389/dc=acegisecurity,dc=org). + * @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; +// } +// } + } diff --git a/core/src/test/java/org/acegisecurity/ldap/LdapUtilsTests.java b/core/src/test/java/org/acegisecurity/ldap/LdapUtilsTests.java new file mode 100644 index 0000000000..31cda011ea --- /dev/null +++ b/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")); + } +}