LDAP-55: Remove unused constant JNDI_ENV_BASE_PATH_KEY

http://opensource.atlassian.com/projects/spring/browse/LDAP-55
This commit is contained in:
Ulrik Sandberg
2007-04-26 10:56:37 +00:00
parent 3b2e00e204
commit b7f9dcdfae
4 changed files with 93 additions and 18 deletions

View File

@@ -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.
* <p>
* Useful for parsing and building LDAP paths.
* Example:
*
* <dl>
* <dt>The path</dt>
* <dd>uid=adam.skogman, ou=People, ou=EU</dd>
* <dt>Name[0]</dt>
* <dd>ou=EU</dd>
* <dt>Name[1]</dt>
* <dd>ou=People</dd>
* <dt>Name[2]</dt>
* <dd>uid=adam.skogman</dd>
* </dl>
*
* Example:
* <pre>
* DistinguishedName path = new DistinguishedName();
* path.addLast(&quot;cn&quot;, entry.getUid());
* path.addLast(&quot;ou&quot;, &quot;users&quot;);
* path.append(new DistinguishedName(helpdesk.getSomeSuffix()));
* path.addLast(&quot;uid&quot;, person.getUid());
* path.addLast(&quot;ou&quot;, &quot;People&quot;);
* path.append(new DistinguishedName(&quot;dc=jayway,dc=se&quot;));
* String dn = path.toString();
* </pre>
*

View File

@@ -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.

View File

@@ -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";

View File

@@ -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");