diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/DistinguishedName.java b/spring-ldap/src/main/java/org/springframework/ldap/core/DistinguishedName.java index 0374c34f..eeabaf4f 100644 --- a/spring-ldap/src/main/java/org/springframework/ldap/core/DistinguishedName.java +++ b/spring-ldap/src/main/java/org/springframework/ldap/core/DistinguishedName.java @@ -36,22 +36,31 @@ import org.springframework.ldap.support.ListComparator; * DistinguishedName implementation is included in JDK1.5 (LdapName), but not in * prior releases. * - * An DistinguishedName is particularly useful when building or modifying an - * Ldap path dynamically, as escaping will be taken care of. + * A DistinguishedName is particularly useful when building or modifying an LDAP + * path dynamically, as escaping will be taken care of. * - * A path is split into several names. The Name interface specifies that the - * most significant part be in position 0, i.e. - * - * The path: uid=adam.skogman, ou=People, ou=EU Name[0]: ou=EU Name[1]: - * ou=People Name[2]: uid=adam.skogman + * A path is split into several names. The {@link Name} interface specifies that + * the most significant part be in position 0. *
- * Useful for parsing and building LDAP paths. + * Example: + * + *
* DistinguishedName path = new DistinguishedName();
- * path.addLast("cn", entry.getUid());
- * path.addLast("ou", "users");
- * path.append(new DistinguishedName(helpdesk.getSomeSuffix()));
+ * path.addLast("uid", person.getUid());
+ * path.addLast("ou", "People");
+ * path.append(new DistinguishedName("dc=jayway,dc=se"));
* String dn = path.toString();
*
*
diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java b/spring-ldap/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java
index 41a94cfb..263e5748 100644
--- a/spring-ldap/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java
+++ b/spring-ldap/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java
@@ -71,7 +71,7 @@ public abstract class AbstractContextSource implements ContextSource,
private Class contextFactory = DEFAULT_CONTEXT_FACTORY;
- private DistinguishedName base;
+ private DistinguishedName base = DistinguishedName.EMPTY_PATH;
protected String userDn = "";
@@ -153,12 +153,12 @@ public abstract class AbstractContextSource implements ContextSource,
StringBuffer providerUrlBuffer = new StringBuffer(1024);
for (int i = 0; i < ldapUrls.length; i++) {
providerUrlBuffer.append(ldapUrls[i]);
- if (base != null) {
+ if (base != DistinguishedName.EMPTY_PATH) {
if (!ldapUrls[i].endsWith("/")) {
providerUrlBuffer.append("/");
}
- providerUrlBuffer.append(base.toUrl());
}
+ providerUrlBuffer.append(base.toUrl());
providerUrlBuffer.append(' ');
}
return providerUrlBuffer.toString().trim();
@@ -176,6 +176,17 @@ public abstract class AbstractContextSource implements ContextSource,
this.base = new DistinguishedName(base);
}
+ /**
+ * Get the base suffix from which all operations should originate. If a base
+ * suffix is set, you will not have to (and, indeed, should not) specify the
+ * full distinguished names in the operations performed.
+ *
+ * @return the base suffix
+ */
+ protected DistinguishedName getBase() {
+ return base;
+ }
+
/**
* Create a DirContext using the supplied environment.
*
@@ -240,7 +251,8 @@ public abstract class AbstractContextSource implements ContextSource,
"At least one server url must be set");
}
- if (base != null && getJdkVersion().compareTo(JDK_142) < 0) {
+ if (base != DistinguishedName.EMPTY_PATH
+ && getJdkVersion().compareTo(JDK_142) < 0) {
throw new IllegalArgumentException(
"Base path is not supported for JDK versions < 1.4.2");
}
@@ -281,7 +293,7 @@ public abstract class AbstractContextSource implements ContextSource,
env.put(Context.OBJECT_FACTORIES, dirObjectFactory.getName());
}
- if (base != null) {
+ if (base != DistinguishedName.EMPTY_PATH) {
// Save the base path for use in the DefaultDirObjectFactory.
env.put(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY, base);
}
@@ -302,7 +314,8 @@ public abstract class AbstractContextSource implements ContextSource,
}
/**
- * Set the user distinguished name (principal) to use for getting authenticated contexts.
+ * Set the user distinguished name (principal) to use for getting
+ * authenticated contexts.
*
* @param userDn
* the user distinguished name.
@@ -312,7 +325,8 @@ public abstract class AbstractContextSource implements ContextSource,
}
/**
- * Set the user distinguished name (principal) to use for getting authenticated contexts.
+ * Set the user distinguished name (principal) to use for getting
+ * authenticated contexts.
*
* @param userName
* the user distinguished name.
diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/support/DefaultDirObjectFactory.java b/spring-ldap/src/main/java/org/springframework/ldap/core/support/DefaultDirObjectFactory.java
index 6b789d91..b45038fb 100644
--- a/spring-ldap/src/main/java/org/springframework/ldap/core/support/DefaultDirObjectFactory.java
+++ b/spring-ldap/src/main/java/org/springframework/ldap/core/support/DefaultDirObjectFactory.java
@@ -37,6 +37,8 @@ public class DefaultDirObjectFactory implements DirObjectFactory {
/**
* Key to use in the ContextSource implementation to store the value of the
* base path suffix, if any, in the Ldap Environment.
+ *
+ * @deprecated Use {@link AbstractContextSource#getBase()} instead
*/
public static final String JNDI_ENV_BASE_PATH_KEY = "org.springframework.ldap.base.path";
diff --git a/spring-ldap/src/test/java/org/springframework/ldap/core/support/LdapContextSourceTest.java b/spring-ldap/src/test/java/org/springframework/ldap/core/support/LdapContextSourceTest.java
index 8b6502ef..efcb0fe2 100644
--- a/spring-ldap/src/test/java/org/springframework/ldap/core/support/LdapContextSourceTest.java
+++ b/spring-ldap/src/test/java/org/springframework/ldap/core/support/LdapContextSourceTest.java
@@ -100,6 +100,56 @@ public class LdapContextSourceTest extends TestCase {
assertNull(env.get(Context.SECURITY_CREDENTIALS));
}
+ public void testGetAnonymousEnvWithNoBaseSet() throws Exception {
+ tested.setUrl("ldap://ldap.example.com:389");
+ tested.afterPropertiesSet();
+ Hashtable env = tested.getAnonymousEnv();
+ assertEquals("ldap://ldap.example.com:389", env
+ .get(Context.PROVIDER_URL));
+ }
+
+ public void testOldJdkWithNoBaseSetShouldWork() throws Exception {
+ tested = new LdapContextSource() {
+ String getJdkVersion() {
+ return "1.3";
+ }
+ };
+ tested.setUrl("ldap://ldap.example.com:389");
+ tested.afterPropertiesSet();
+ }
+
+ public void testOldJdkWithBaseSetShouldNotWork() throws Exception {
+ tested = new LdapContextSource() {
+ String getJdkVersion() {
+ return "1.3";
+ }
+ };
+ tested.setUrl("ldap://ldap.example.com:389");
+ tested.setBase("dc=example,dc=com");
+ try {
+ tested.afterPropertiesSet();
+ fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException expected) {
+ assertTrue(true);
+ }
+ }
+
+ public void testOldJdkWithBaseSetToEmptyPathShouldNotWork() throws Exception {
+ tested = new LdapContextSource() {
+ String getJdkVersion() {
+ return "1.3";
+ }
+ };
+ tested.setUrl("ldap://ldap.example.com:389");
+ tested.setBase(null);
+ try {
+ tested.afterPropertiesSet();
+ fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException expected) {
+ assertTrue(true);
+ }
+ }
+
public void testGetAuthenticatedEnv() throws Exception {
tested.setBase("dc=example,dc=se");
tested.setUrl("ldap://ldap.example.com:389");