Add functionality to LdapTemplate for checking that an entry exists, and for retrieving an entry as an object, mapped from its attributes.

This commit is contained in:
Luke Taylor 2006-05-01 00:28:27 +00:00
parent 3f0f45706c
commit a468f03cae
2 changed files with 74 additions and 5 deletions

View File

@ -0,0 +1,31 @@
/* 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 javax.naming.directory.Attributes;
import javax.naming.NamingException;
/**
* A mapper for use with {@link LdapTemplate}. Creates a customized object from
* a set of attributes retrieved from a directory entry.
*
* @author Luke Taylor
* @version $Id$
*/
public interface AttributesMapper {
public Object mapAttributes(Attributes attributes) throws NamingException;
}

View File

@ -17,6 +17,7 @@ package org.acegisecurity.ldap;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.NameNotFoundException;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
@ -44,7 +45,7 @@ public class LdapTemplate {
public static final String[] NO_ATTRS = new String[0];
private InitialDirContextFactory dirContextFactory;
private String userDn = null;
private String managerDn = null;
private String password = null;
/** Default search scope */
private int searchScope = SearchControls.SUBTREE_SCOPE;
@ -57,10 +58,10 @@ public class LdapTemplate {
public LdapTemplate(InitialDirContextFactory dirContextFactory, String userDn, String password) {
this(dirContextFactory);
Assert.hasLength(userDn, "userDn must not be null or empty");
Assert.hasLength(userDn, "managerDn must not be null or empty");
Assert.notNull(password, "password cannot be null");
this.userDn = userDn;
this.managerDn = userDn;
this.password = password;
}
@ -72,9 +73,9 @@ public class LdapTemplate {
DirContext ctx = null;
try {
ctx = (userDn == null) ?
ctx = (managerDn == null) ?
dirContextFactory.newInitialDirContext() :
dirContextFactory.newInitialDirContext(userDn, password);
dirContextFactory.newInitialDirContext(managerDn, password);
return callback.execute(ctx);
@ -162,4 +163,41 @@ public class LdapTemplate {
return (Set)execute(new SingleAttributeSearchCallback());
}
public boolean nameExists(final String dn) {
Boolean exists = (Boolean) execute( new LdapCallback() {
public Object execute(DirContext ctx) throws NamingException {
try {
ctx.lookup( LdapUtils.getRelativeName(dn, ctx) );
} catch(NameNotFoundException nnfe) {
return Boolean.FALSE;
}
return Boolean.TRUE;
}
}
);
return exists.booleanValue();
}
/**
* Composes an object from the attributes of the given DN.
*
* @param dn the directory entry which will be read
* @param mapper maps the attributes to the required object
* @param attributesToRetrieve the named attributes which will be retrieved from the directory entry.
* @return the object created by the mapper
*/
public Object retrieveEntry(final String dn, final AttributesMapper mapper, final String[] attributesToRetrieve) {
return execute ( new LdapCallback() {
public Object execute(DirContext ctx) throws NamingException {
return mapper.mapAttributes( ctx.getAttributes(LdapUtils.getRelativeName(dn, ctx), attributesToRetrieve) );
}
} );
}
}