edu.vt.middleware.ldap
Class Ldap

java.lang.Object
  extended by edu.vt.middleware.ldap.AbstractLdap<LdapConfig>
      extended by edu.vt.middleware.ldap.Ldap
All Implemented Interfaces:
BaseLdap, Serializable

public class Ldap
extends AbstractLdap<LdapConfig>
implements Serializable

Ldap contains functions for basic interaction with an LDAP. Methods are provided for connecting, binding, querying and updating.

Version:
$Revision: 1330 $ $Date: 2010-05-23 18:10:53 -0400 (Sun, 23 May 2010) $
Author:
Middleware Services
See Also:
Serialized Form

Nested Class Summary
static class Ldap.AttributeModification
          Enum to define the type of attribute modification.
 
Field Summary
 
Fields inherited from class edu.vt.middleware.ldap.AbstractLdap
BINDING_COPY_RESULT_HANDLER, config, connectionHandler, COPY_RESULT_HANDLER, logger, NCP_COPY_RESULT_HANDLER, SR_COPY_RESULT_HANDLER
 
Constructor Summary
Ldap()
          Default constructor.
Ldap(LdapConfig ldapConfig)
          This will create a new Ldap with the supplied LdapConfig.
 
Method Summary
 boolean compare(SearchFilter filter)
          This will perform an LDAP compare operation with the supplied filter.
 boolean compare(String dn, SearchFilter filter)
          This will perform an LDAP compare operation with the supplied filter and dn.
 void create(String dn, Attributes attrs)
          This will create the supplied dn in the LDAP namespace with the supplied attributes.
 void delete(String dn)
          This will delete the supplied dn from the LDAP namespace.
 Attributes getAttributes(String dn)
          This will return all the attributes associated with the supplied dn.
 Attributes getAttributes(String dn, String[] retAttrs)
          This will return the matching attributes associated with the supplied dn.
 Attributes getAttributes(String dn, String[] retAttrs, AttributeHandler... handler)
          This will return the matching attributes associated with the supplied dn.
 LdapConfig getLdapConfig()
          This returns the LdapConfig of the Ldap.
 String[] getSaslMechanisms()
          This will return a list of SASL mechanisms that this LDAP supports.
 Iterator<SearchResult> getSchema(String dn)
          This will return the LDAP schema associated with the supplied dn.
 String[] getSupportedControls()
          This will return a list of controls that this LDAP supports.
 Iterator<NameClassPair> list(String dn)
          This will enumerate the names bounds to the specified context, along with the class names of objects bound to them.
 Iterator<Binding> listBindings(String dn)
          This will enumerate the names bounds to the specified context, along with the objects bound to them.
 void loadFromProperties()
          This will set the config parameters of this Ldap using the default properties file, which must be located in your classpath.
 void loadFromProperties(InputStream is)
          This will set the config parameters of this Ldap using the supplied input stream.
 void modifyAttributes(String dn, Ldap.AttributeModification mod, Attributes attrs)
          This will modify the supplied attributes for the supplied value given by the modification operation.
 void modifyAttributes(String dn, ModificationItem[] mods)
          This will modify the supplied dn using the supplied modifications.
 void rename(String oldDn, String newDn)
          This will rename the supplied dn in the LDAP namespace.
 Iterator<SearchResult> search(SearchFilter filter)
          This will query the LDAP with the supplied filter.
 Iterator<SearchResult> search(SearchFilter filter, SearchControls searchControls)
          This will query the LDAP with the supplied filter and search controls.
 Iterator<SearchResult> search(SearchFilter filter, String[] retAttrs)
          This will query the LDAP with the supplied filter and return attributes.
 Iterator<SearchResult> search(String dn, SearchFilter filter)
          This will query the LDAP with the supplied dn and filter.
 Iterator<SearchResult> search(String dn, SearchFilter filter, SearchControls searchControls)
          This will query the LDAP with the supplied dn, filter, and search controls.
 Iterator<SearchResult> search(String dn, SearchFilter filter, SearchControls searchControls, SearchResultHandler... handler)
          This will query the LDAP with the supplied dn, filter, search controls, and search result handler.
 Iterator<SearchResult> search(String dn, SearchFilter filter, String[] retAttrs)
          This will query the LDAP with the supplied dn, filter, and return attributes.
 Iterator<SearchResult> search(String dn, SearchFilter filter, String[] retAttrs, SearchResultHandler... handler)
          This will query the LDAP with the supplied dn, filter, return attributes, and search result handler.
 Iterator<SearchResult> searchAttributes(Attributes matchAttrs)
          This will query the LDAP for the supplied matching attributes.
 Iterator<SearchResult> searchAttributes(Attributes matchAttrs, String[] retAttrs)
          This will query the LDAP for the supplied matching attributes and return attributes.
 Iterator<SearchResult> searchAttributes(String dn, Attributes matchAttrs)
          This will query the LDAP for the supplied dn and matching attributes.
 Iterator<SearchResult> searchAttributes(String dn, Attributes matchAttrs, String[] retAttrs)
          This will query the LDAP for the supplied dn, matching attributes and return attributes.
 Iterator<SearchResult> searchAttributes(String dn, Attributes matchAttrs, String[] retAttrs, SearchResultHandler... handler)
          This will query the LDAP for the supplied dn, matching attributes and return attributes.
 void setLdapConfig(LdapConfig ldapConfig)
          This will set the config parameters of this Ldap.
 
Methods inherited from class edu.vt.middleware.ldap.AbstractLdap
close, compare, connect, finalize, getContext, modifyAttributes, operationRetry, pagedSearch, reconnect, search, toString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Ldap

public Ldap()
Default constructor.


Ldap

public Ldap(LdapConfig ldapConfig)
This will create a new Ldap with the supplied LdapConfig.

Parameters:
ldapConfig - LdapConfig
Method Detail

setLdapConfig

public void setLdapConfig(LdapConfig ldapConfig)
This will set the config parameters of this Ldap.

Overrides:
setLdapConfig in class AbstractLdap<LdapConfig>
Parameters:
ldapConfig - LdapConfig

getLdapConfig

public LdapConfig getLdapConfig()
This returns the LdapConfig of the Ldap.

Returns:
LdapConfig

loadFromProperties

public void loadFromProperties()
This will set the config parameters of this Ldap using the default properties file, which must be located in your classpath.


loadFromProperties

public void loadFromProperties(InputStream is)
This will set the config parameters of this Ldap using the supplied input stream.

Parameters:
is - InputStream

compare

public boolean compare(SearchFilter filter)
                throws NamingException
This will perform an LDAP compare operation with the supplied filter. LdapConfig.getBaseDn() is used as the dn to compare. See compare(String, SearchFilter).

Parameters:
filter - SearchFilter expression to use for compare
Returns:
boolean - result of compare operation
Throws:
NamingException - if the LDAP returns an error

compare

public boolean compare(String dn,
                       SearchFilter filter)
                throws NamingException
This will perform an LDAP compare operation with the supplied filter and dn.

Parameters:
dn - String name to compare
filter - SearchFilter expression to use for compare
Returns:
boolean - result of compare operation
Throws:
NamingException - if the LDAP returns an error

search

public Iterator<SearchResult> search(SearchFilter filter)
                              throws NamingException
This will query the LDAP with the supplied filter. All attributes will be returned. LdapConfig.getBaseDn() is used as the start point for searching. Search controls will be created from LdapConfig.getSearchControls(String[]). See search(String,SearchFilter,String[]).

Parameters:
filter - SearchFilter expression to use for the search
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

search

public Iterator<SearchResult> search(SearchFilter filter,
                                     String[] retAttrs)
                              throws NamingException
This will query the LDAP with the supplied filter and return attributes. LdapConfig.getBaseDn() is used as the start point for searching. Search controls will be created from LdapConfig.getSearchControls(String[]). See search(String,SearchFilter,String[]).

Parameters:
filter - SearchFilter expression to use for the search
retAttrs - String[] attributes to return
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

search

public Iterator<SearchResult> search(SearchFilter filter,
                                     SearchControls searchControls)
                              throws NamingException
This will query the LDAP with the supplied filter and search controls. LdapConfig.getBaseDn() is used as the start point for searching. See search(String,SearchFilter,SearchControls).

Parameters:
filter - SearchFilter expression to use for the search
searchControls - SearchControls to search with
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

search

public Iterator<SearchResult> search(String dn,
                                     SearchFilter filter)
                              throws NamingException
This will query the LDAP with the supplied dn and filter. All attributes will be returned. Search controls will be created from LdapConfig.getSearchControls(String[]). See search(String,SearchFilter,String[]).

Parameters:
dn - String name to begin search at
filter - SearchFilter expression to use for the search
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

search

public Iterator<SearchResult> search(String dn,
                                     SearchFilter filter,
                                     String[] retAttrs)
                              throws NamingException
This will query the LDAP with the supplied dn, filter, and return attributes. Search controls will be created from LdapConfig.getSearchControls(String[]). See search(String,SearchFilter,SearchControls,SearchResultHandler[]).

Parameters:
dn - String name to begin search at
filter - SearchFilter expression to use for the search
retAttrs - String[] attributes to return
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

search

public Iterator<SearchResult> search(String dn,
                                     SearchFilter filter,
                                     SearchControls searchControls)
                              throws NamingException
This will query the LDAP with the supplied dn, filter, and search controls. See search(String,SearchFilter,SearchControls,SearchResultHandler[]).

Parameters:
dn - String name to begin search at
filter - SearchFilter expression to use for the search
searchControls - SearchControls to search with
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

search

public Iterator<SearchResult> search(String dn,
                                     SearchFilter filter,
                                     String[] retAttrs,
                                     SearchResultHandler... handler)
                              throws NamingException
This will query the LDAP with the supplied dn, filter, return attributes, and search result handler. Search controls will be created from LdapConfig.getSearchControls(String[]). See search( String,SearchFilter,SearchControls,SearchResultHandler...).

Parameters:
dn - String name to begin search at
filter - SearchFilter expression to use for the search
retAttrs - String[] attributes to return
handler - SearchResultHandler[] of handlers to execute
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

search

public Iterator<SearchResult> search(String dn,
                                     SearchFilter filter,
                                     SearchControls searchControls,
                                     SearchResultHandler... handler)
                              throws NamingException
This will query the LDAP with the supplied dn, filter, search controls, and search result handler. If LdapConfig.getPagedResultsSize() is greater than 0, the PagedResultsControl will be invoked. See #search(String,String,Object[],SearchControls,SearchResultHandler[]).

Parameters:
dn - String name to begin search at
filter - SearchFilter expression to use for the search
searchControls - SearchControls to search with
handler - SearchResultHandler[] of handlers to execute
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

searchAttributes

public Iterator<SearchResult> searchAttributes(Attributes matchAttrs)
                                        throws NamingException
This will query the LDAP for the supplied matching attributes. All attributes will be returned. LdapConfig.getBaseDn() is used as the name to search. See searchAttributes(String, Attributes, String[]).

Parameters:
matchAttrs - Attributes attributes to match
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

searchAttributes

public Iterator<SearchResult> searchAttributes(Attributes matchAttrs,
                                               String[] retAttrs)
                                        throws NamingException
This will query the LDAP for the supplied matching attributes and return attributes. LdapConfig.getBaseDn() is used as the name to search. See searchAttributes(String, Attributes, String[]).

Parameters:
matchAttrs - Attributes attributes to match
retAttrs - String[] attributes to return
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

searchAttributes

public Iterator<SearchResult> searchAttributes(String dn,
                                               Attributes matchAttrs)
                                        throws NamingException
This will query the LDAP for the supplied dn and matching attributes. All attributes will be returned. See searchAttributes(String, Attributes, String[]).

Parameters:
dn - String name to search in
matchAttrs - Attributes attributes to match
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

searchAttributes

public Iterator<SearchResult> searchAttributes(String dn,
                                               Attributes matchAttrs,
                                               String[] retAttrs)
                                        throws NamingException
This will query the LDAP for the supplied dn, matching attributes and return attributes. See searchAttributes( String, Attributes, String[], SearchResultHandler[]). This method converts relative DNs to fully qualified DNs, no post processing is required

Parameters:
dn - String name to search in
matchAttrs - Attributes attributes to match
retAttrs - String[] attributes to return
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

searchAttributes

public Iterator<SearchResult> searchAttributes(String dn,
                                               Attributes matchAttrs,
                                               String[] retAttrs,
                                               SearchResultHandler... handler)
                                        throws NamingException
This will query the LDAP for the supplied dn, matching attributes and return attributes. This method will always perform a one level search. The resulting Iterator is a deep copy of the original search results. If matchAttrs is empty or null then all objects in the target context are returned. If retAttrs is null then all attributes will be returned. If retAttrs is an empty array then no attributes will be returned. See DirContext.

Overrides:
searchAttributes in class AbstractLdap<LdapConfig>
Parameters:
dn - String name to search in
matchAttrs - Attributes attributes to match
retAttrs - String[] attributes to return
handler - SearchResultHandler[] to post process results
Returns:
Iterator - of LDAP search results
Throws:
NamingException - if the LDAP returns an error

list

public Iterator<NameClassPair> list(String dn)
                             throws NamingException
This will enumerate the names bounds to the specified context, along with the class names of objects bound to them. The resulting Iterator is a deep copy of the original search results. See Context.list(String).

Overrides:
list in class AbstractLdap<LdapConfig>
Parameters:
dn - String LDAP context to list
Returns:
Iterator - LDAP search result
Throws:
NamingException - if the LDAP returns an error

listBindings

public Iterator<Binding> listBindings(String dn)
                               throws NamingException
This will enumerate the names bounds to the specified context, along with the objects bound to them. The resulting Iterator is a deep copy of the original search results. See Context.listBindings(String).

Overrides:
listBindings in class AbstractLdap<LdapConfig>
Parameters:
dn - String LDAP context to list
Returns:
Iterator - LDAP search result
Throws:
NamingException - if the LDAP returns an error

getAttributes

public Attributes getAttributes(String dn)
                         throws NamingException
This will return all the attributes associated with the supplied dn. See getAttributes(String, String[]).

Parameters:
dn - String named object in the LDAP
Returns:
Attributes
Throws:
NamingException - if the LDAP returns an error

getAttributes

public Attributes getAttributes(String dn,
                                String[] retAttrs)
                         throws NamingException
This will return the matching attributes associated with the supplied dn. If retAttrs is null then all attributes will be returned. If retAttrs is an empty array then no attributes will be returned. See getAttributes(String, String[], AttributeHandler[]).

Parameters:
dn - String named object in the LDAP
retAttrs - String[] attributes to return
Returns:
Attributes
Throws:
NamingException - if the LDAP returns an error

getAttributes

public Attributes getAttributes(String dn,
                                String[] retAttrs,
                                AttributeHandler... handler)
                         throws NamingException
This will return the matching attributes associated with the supplied dn. If retAttrs is null then all attributes will be returned. If retAttrs is an empty array then no attributes will be returned. See DirContext.

Overrides:
getAttributes in class AbstractLdap<LdapConfig>
Parameters:
dn - String named object in the LDAP
retAttrs - String[] attributes to return
handler - AttributeHandler[] to post process results
Returns:
Attributes
Throws:
NamingException - if the LDAP returns an error

getSchema

public Iterator<SearchResult> getSchema(String dn)
                                 throws NamingException
This will return the LDAP schema associated with the supplied dn. The resulting Iterator is a deep copy of the original search results. See DirContext.

Overrides:
getSchema in class AbstractLdap<LdapConfig>
Parameters:
dn - String named object in the LDAP
Returns:
Iterator - LDAP search result
Throws:
NamingException - if the LDAP returns an error

modifyAttributes

public void modifyAttributes(String dn,
                             Ldap.AttributeModification mod,
                             Attributes attrs)
                      throws NamingException
This will modify the supplied attributes for the supplied value given by the modification operation. See AbstractLdap.modifyAttributes(String, int, Attributes).

Parameters:
dn - String named object in the LDAP
mod - AttributeModification modification operation
attrs - Attributes attributes to be used for the operation, may be null
Throws:
NamingException - if the LDAP returns an error

modifyAttributes

public void modifyAttributes(String dn,
                             ModificationItem[] mods)
                      throws NamingException
This will modify the supplied dn using the supplied modifications. The modifications are performed in the order specified. Each modification specifies a modification operation code and an attribute on which to operate. Where possible, the modifications are performed atomically. See DirContext.

Overrides:
modifyAttributes in class AbstractLdap<LdapConfig>
Parameters:
dn - String named object in the LDAP
mods - ModificationItem[] modifications
Throws:
NamingException - if the LDAP returns an error

create

public void create(String dn,
                   Attributes attrs)
            throws NamingException
This will create the supplied dn in the LDAP namespace with the supplied attributes. See DirContext. Note that the context created by this operation is immediately closed.

Overrides:
create in class AbstractLdap<LdapConfig>
Parameters:
dn - String named object in the LDAP
attrs - Attributes attributes to be added to this entry
Throws:
NamingException - if the LDAP returns an error

rename

public void rename(String oldDn,
                   String newDn)
            throws NamingException
This will rename the supplied dn in the LDAP namespace. See Context.rename(String, String).

Overrides:
rename in class AbstractLdap<LdapConfig>
Parameters:
oldDn - String object to rename
newDn - String new name
Throws:
NamingException - if the LDAP returns an error

delete

public void delete(String dn)
            throws NamingException
This will delete the supplied dn from the LDAP namespace. Note that this method does not throw NameNotFoundException if the supplied dn does not exist. See Context.destroySubcontext(String).

Overrides:
delete in class AbstractLdap<LdapConfig>
Parameters:
dn - String named object in the LDAP
Throws:
NamingException - if the LDAP returns an error

getSaslMechanisms

public String[] getSaslMechanisms()
                           throws NamingException
This will return a list of SASL mechanisms that this LDAP supports.

Returns:
String[] - supported SASL mechanisms
Throws:
NamingException - if the LDAP returns an error

getSupportedControls

public String[] getSupportedControls()
                              throws NamingException
This will return a list of controls that this LDAP supports.

Returns:
String[] - supported controls
Throws:
NamingException - if the LDAP returns an error


Copyright © 2003-2010 Virginia Tech. All Rights Reserved.