Spring LDAP

org.springframework.ldap.core.simple
Class SimpleLdapTemplate

java.lang.Object
  extended by org.springframework.ldap.core.simple.SimpleLdapTemplate
All Implemented Interfaces:
SimpleLdapOperations

public class SimpleLdapTemplate
extends Object
implements SimpleLdapOperations

Java-5-based convenience wrapper for the classic LdapTemplate, adding some convenient shortcuts and taking advantage of Java 5 Generics. Use the getLdapOperations() method if you need to invoke less commonly used template methods.

Author:
Mattias Hellborg Arthursson

Constructor Summary
SimpleLdapTemplate(ContextSource contextSource)
          Constructs a new SimpleLdapTemplate instance, automatically creating a wrapped LdapTemplate instance to work with.
SimpleLdapTemplate(LdapOperations ldapOperations)
          Constructs a new SimpleLdapTemplate instance wrapping the supplied LdapOperations instance.
 
Method Summary
 boolean authenticate(Name base, String filter, String password)
          Utility method to perform a simple LDAP 'bind' authentication.
 boolean authenticate(String base, String filter, String password)
          Utility method to perform a simple LDAP 'bind' authentication.
 void bind(DirContextOperations ctx)
          Bind the data in the supplied context in the tree.
 void bind(Name dn, Object obj, Attributes attributes)
          Create an entry in the LDAP tree.
 void bind(String dn, Object obj, Attributes attributes)
          Create an entry in the LDAP tree.
 LdapOperations getLdapOperations()
          Get the wrapped LdapOperations instance.
<T> T
lookup(Name dn, ParameterizedContextMapper<T> mapper)
          Perform a lookup of the specified DN and map the result using the mapper.
<T> T
lookup(String dn, ParameterizedContextMapper<T> mapper)
          Perform a lookup of the specified DN and map the result using the mapper.
 DirContextOperations lookupContext(Name dn)
          Look up the specified DN, and automatically cast it to a DirContextOperations instance.
 DirContextOperations lookupContext(String dn)
          Look up the specified DN, and automatically cast it to a DirContextOperations instance.
 void modifyAttributes(DirContextOperations ctx)
          Modify the Attributes of the entry corresponding to the supplied DirContextOperations instance.
<T> List<T>
search(Name base, String filter, ParameterizedContextMapper<T> mapper)
          Search for a List of type T using the supplied filter and link ParameterizedContextMapper.
<T> List<T>
search(Name base, String filter, SearchControls controls, ParameterizedContextMapper<T> mapper, DirContextProcessor processor)
          Search for a List of type T using the supplied filter, SearchControls, DirContextProcessor and ParameterizedContextMapper.
<T> List<T>
search(String base, String filter, ParameterizedContextMapper<T> mapper)
          Search for a List of type T using the supplied filter and link ParameterizedContextMapper.
<T> List<T>
search(String base, String filter, SearchControls controls, ParameterizedContextMapper<T> mapper, DirContextProcessor processor)
          Search for a List of type T using the supplied filter, SearchControls, DirContextProcessor and ParameterizedContextMapper.
<T> T
searchForObject(Name base, String filter, ParameterizedContextMapper<T> mapper)
          Perform a search for a unique entry matching the specified search criteria and return the found object.
<T> T
searchForObject(String base, String filter, ParameterizedContextMapper<T> mapper)
          Perform a search for a unique entry matching the specified search criteria and return the found object.
 void unbind(Name dn)
          Remove an entry from the LDAP tree.
 void unbind(String dn)
          Remove an entry from the LDAP tree.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleLdapTemplate

public SimpleLdapTemplate(LdapOperations ldapOperations)
Constructs a new SimpleLdapTemplate instance wrapping the supplied LdapOperations instance.

Parameters:
ldapOperations - the LdapOperations instance to wrap.

SimpleLdapTemplate

public SimpleLdapTemplate(ContextSource contextSource)
Constructs a new SimpleLdapTemplate instance, automatically creating a wrapped LdapTemplate instance to work with.

Parameters:
contextSource -
Method Detail

getLdapOperations

public LdapOperations getLdapOperations()
Description copied from interface: SimpleLdapOperations
Get the wrapped LdapOperations instance.

Specified by:
getLdapOperations in interface SimpleLdapOperations
Returns:
the wrapped LdapOperations instance.

lookup

public <T> T lookup(String dn,
                    ParameterizedContextMapper<T> mapper)
Description copied from interface: SimpleLdapOperations
Perform a lookup of the specified DN and map the result using the mapper.

Specified by:
lookup in interface SimpleLdapOperations
Parameters:
dn - the Distinguished Name to look up.
mapper - the mapper to use.
Returns:
the mapped object, as received by the ParameterizedContextMapper.

search

public <T> List<T> search(String base,
                          String filter,
                          ParameterizedContextMapper<T> mapper)
Description copied from interface: SimpleLdapOperations
Search for a List of type T using the supplied filter and link ParameterizedContextMapper.

Specified by:
search in interface SimpleLdapOperations
Parameters:
base - Base DN relative to the base of the ContextSource - where to start the search.
filter - Search filter.
mapper - the Mapper to supply all results to.
Returns:
a List of type T containing objects for all entries found, as mapped by the ParameterizedContextMapper.

search

public <T> List<T> search(String base,
                          String filter,
                          SearchControls controls,
                          ParameterizedContextMapper<T> mapper,
                          DirContextProcessor processor)
Description copied from interface: SimpleLdapOperations
Search for a List of type T using the supplied filter, SearchControls, DirContextProcessor and ParameterizedContextMapper.

Specified by:
search in interface SimpleLdapOperations
Parameters:
base - Base DN relative to the base of the ContextSource - where to start the search.
filter - Search filter.
controls - the SearchControls. Make sure that the returningObjFlag is set to true.
mapper - the Mapper to supply all results to.
processor - the DirContextProcessor to be used for applying pre/post processing on the DirContext instance.
Returns:
a List of type T containing objects for all entries found, as mapped by the ParameterizedContextMapper.

lookupContext

public DirContextOperations lookupContext(String dn)
Description copied from interface: SimpleLdapOperations
Look up the specified DN, and automatically cast it to a DirContextOperations instance.

Specified by:
lookupContext in interface SimpleLdapOperations
Parameters:
dn - The Distinguished Name of the entry to look up.
Returns:
A DirContextOperations instance constructed from the found entry.

modifyAttributes

public void modifyAttributes(DirContextOperations ctx)
Description copied from interface: SimpleLdapOperations
Modify the Attributes of the entry corresponding to the supplied DirContextOperations instance. The instance should have been received from the SimpleLdapOperations.lookupContext(String) operation, and then modified to match the current state of the matching domain object, e.g.:
 public void update(Person person) {
        DirContextOperations ctx = simpleLdapOperations.lookup(person.getDn());
 
        ctx.setAttributeValue("description", person.getDescription());
        ctx.setAttributeValue("telephoneNumber", person.getPhone());
        // More modifications here
 
        simpleLdapOperations.modifyAttributes(ctx);
 }
 

Specified by:
modifyAttributes in interface SimpleLdapOperations
Parameters:
ctx - the entry to update in the LDAP tree.

bind

public void bind(String dn,
                 Object obj,
                 Attributes attributes)
Description copied from interface: SimpleLdapOperations
Create an entry in the LDAP tree. The attributes used to create the entry are either retrieved from the obj parameter or the attributes parameter (or both). One of these parameters may be null but not both.

Specified by:
bind in interface SimpleLdapOperations
Parameters:
dn - The distinguished name to bind the object and attributes to.
obj - The object to bind, may be null. Typically a DirContext implementation.
attributes - The attributes to bind, may be null.

unbind

public void unbind(String dn)
Description copied from interface: SimpleLdapOperations
Remove an entry from the LDAP tree. The entry must not have any children.

Specified by:
unbind in interface SimpleLdapOperations
Parameters:
dn - The distinguished name to unbind.

bind

public void bind(Name dn,
                 Object obj,
                 Attributes attributes)
Description copied from interface: SimpleLdapOperations
Create an entry in the LDAP tree. The attributes used to create the entry are either retrieved from the obj parameter or the attributes parameter (or both). One of these parameters may be null but not both.

Specified by:
bind in interface SimpleLdapOperations
Parameters:
dn - The distinguished name to bind the object and attributes to.
obj - The object to bind, may be null. Typically a DirContext implementation.
attributes - The attributes to bind, may be null.

lookup

public <T> T lookup(Name dn,
                    ParameterizedContextMapper<T> mapper)
Description copied from interface: SimpleLdapOperations
Perform a lookup of the specified DN and map the result using the mapper.

Specified by:
lookup in interface SimpleLdapOperations
Parameters:
dn - the Distinguished Name to look up.
mapper - the mapper to use.
Returns:
the mapped object, as received by the ParameterizedContextMapper.

lookupContext

public DirContextOperations lookupContext(Name dn)
Description copied from interface: SimpleLdapOperations
Look up the specified DN, and automatically cast it to a DirContextOperations instance.

Specified by:
lookupContext in interface SimpleLdapOperations
Parameters:
dn - The Distinguished Name of the entry to look up.
Returns:
A DirContextOperations instance constructed from the found entry.

search

public <T> List<T> search(Name base,
                          String filter,
                          ParameterizedContextMapper<T> mapper)
Description copied from interface: SimpleLdapOperations
Search for a List of type T using the supplied filter and link ParameterizedContextMapper.

Specified by:
search in interface SimpleLdapOperations
Parameters:
base - Base DN relative to the base of the ContextSource - where to start the search.
filter - Search filter.
mapper - the Mapper to supply all results to.
Returns:
a List of type T containing objects for all entries found, as mapped by the ParameterizedContextMapper.

search

public <T> List<T> search(Name base,
                          String filter,
                          SearchControls controls,
                          ParameterizedContextMapper<T> mapper,
                          DirContextProcessor processor)
Description copied from interface: SimpleLdapOperations
Search for a List of type T using the supplied filter, SearchControls, DirContextProcessor and ParameterizedContextMapper.

Specified by:
search in interface SimpleLdapOperations
Parameters:
base - Base DN relative to the base of the ContextSource - where to start the search.
filter - Search filter.
controls - the SearchControls. Make sure that the returningObjFlag is set to true.
mapper - the Mapper to supply all results to.
processor - the DirContextProcessor to be used for applying pre/post processing on the DirContext instance.
Returns:
a List of type T containing objects for all entries found, as mapped by the ParameterizedContextMapper.

unbind

public void unbind(Name dn)
Description copied from interface: SimpleLdapOperations
Remove an entry from the LDAP tree. The entry must not have any children.

Specified by:
unbind in interface SimpleLdapOperations
Parameters:
dn - The distinguished name to unbind.

bind

public void bind(DirContextOperations ctx)
Description copied from interface: SimpleLdapOperations
Bind the data in the supplied context in the tree. All specified Attributes in will be bound to the DN set on the instance.

Specified by:
bind in interface SimpleLdapOperations
Parameters:
ctx - the context to bind

searchForObject

public <T> T searchForObject(String base,
                             String filter,
                             ParameterizedContextMapper<T> mapper)
Description copied from interface: SimpleLdapOperations
Perform a search for a unique entry matching the specified search criteria and return the found object. If no entry is found or if there are more than one matching entry, an IncorrectResultSizeDataAccessException is thrown.

Specified by:
searchForObject in interface SimpleLdapOperations
Parameters:
base - the DN to use as the base of the search.
filter - the search filter.
mapper - the mapper to use for the search.
Returns:
the single object returned by the mapper that matches the search criteria.

searchForObject

public <T> T searchForObject(Name base,
                             String filter,
                             ParameterizedContextMapper<T> mapper)
Description copied from interface: SimpleLdapOperations
Perform a search for a unique entry matching the specified search criteria and return the found object. If no entry is found or if there are more than one matching entry, an IncorrectResultSizeDataAccessException is thrown.

Specified by:
searchForObject in interface SimpleLdapOperations
Parameters:
base - the DN to use as the base of the search.
filter - the search filter.
mapper - the mapper to use for the search.
Returns:
the single object returned by the mapper that matches the search criteria.

authenticate

public boolean authenticate(String base,
                            String filter,
                            String password)
Description copied from interface: SimpleLdapOperations
Utility method to perform a simple LDAP 'bind' authentication. Search for the LDAP entry to authenticate using the supplied base DN and filter; use the DN of the found entry together with the password as input to ContextSource.getContext(String, String), thus authenticating the entry.

Example:

 AndFilter filter = new AndFilter();
 filter.and("objectclass", "person").and("uid", userId);
 boolean authenticated = ldapTemplate.authenticate(DistinguishedName.EMPTY_PATH,
                filter.toString(), password);
 

Specified by:
authenticate in interface SimpleLdapOperations
Parameters:
base - the DN to use as the base of the search.
filter - the search filter - must result in a unique result.
password - the password to use for authentication.
Returns:
true if the authentication was successful, false otherwise.

authenticate

public boolean authenticate(Name base,
                            String filter,
                            String password)
Description copied from interface: SimpleLdapOperations
Utility method to perform a simple LDAP 'bind' authentication. Search for the LDAP entry to authenticate using the supplied base DN and filter; use the DN of the found entry together with the password as input to ContextSource.getContext(String, String), thus authenticating the entry.

Example:

 AndFilter filter = new AndFilter();
 filter.and("objectclass", "person").and("uid", userId);
 boolean authenticated = ldapTemplate.authenticate(DistinguishedName.EMPTY_PATH,
                filter.toString(), password);
 

Specified by:
authenticate in interface SimpleLdapOperations
Parameters:
base - the DN to use as the base of the search.
filter - the search filter - must result in a unique result.
password - the password to use for authentication.
Returns:
true if the authentication was successful, false otherwise.

Spring LDAP