Class Ldap

  • All Implemented Interfaces:
    BaseLdap, java.io.Serializable

    public class Ldap
    extends AbstractLdap<LdapConfig>
    implements java.io.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-24 00:10:53 +0200 (Mon, 24 May 2010) $
    Author:
    Middleware Services
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Ldap.AttributeModification
      Enum to define the type of attribute modification.
    • Constructor Summary

      Constructors 
      Constructor Description
      Ldap()
      Default constructor.
      Ldap​(LdapConfig ldapConfig)
      This will create a new Ldap with the supplied LdapConfig.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean compare​(SearchFilter filter)
      This will perform an LDAP compare operation with the supplied filter.
      boolean compare​(java.lang.String dn, SearchFilter filter)
      This will perform an LDAP compare operation with the supplied filter and dn.
      void create​(java.lang.String dn, javax.naming.directory.Attributes attrs)
      This will create the supplied dn in the LDAP namespace with the supplied attributes.
      void delete​(java.lang.String dn)
      This will delete the supplied dn from the LDAP namespace.
      javax.naming.directory.Attributes getAttributes​(java.lang.String dn)
      This will return all the attributes associated with the supplied dn.
      javax.naming.directory.Attributes getAttributes​(java.lang.String dn, java.lang.String[] retAttrs)
      This will return the matching attributes associated with the supplied dn.
      javax.naming.directory.Attributes getAttributes​(java.lang.String dn, java.lang.String[] retAttrs, AttributeHandler... handler)
      This will return the matching attributes associated with the supplied dn.
      LdapConfig getLdapConfig()
      This returns the LdapConfig of the Ldap.
      java.lang.String[] getSaslMechanisms()
      This will return a list of SASL mechanisms that this LDAP supports.
      java.util.Iterator<javax.naming.directory.SearchResult> getSchema​(java.lang.String dn)
      This will return the LDAP schema associated with the supplied dn.
      java.lang.String[] getSupportedControls()
      This will return a list of controls that this LDAP supports.
      java.util.Iterator<javax.naming.NameClassPair> list​(java.lang.String dn)
      This will enumerate the names bounds to the specified context, along with the class names of objects bound to them.
      java.util.Iterator<javax.naming.Binding> listBindings​(java.lang.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​(java.io.InputStream is)
      This will set the config parameters of this Ldap using the supplied input stream.
      void modifyAttributes​(java.lang.String dn, Ldap.AttributeModification mod, javax.naming.directory.Attributes attrs)
      This will modify the supplied attributes for the supplied value given by the modification operation.
      void modifyAttributes​(java.lang.String dn, javax.naming.directory.ModificationItem[] mods)
      This will modify the supplied dn using the supplied modifications.
      void rename​(java.lang.String oldDn, java.lang.String newDn)
      This will rename the supplied dn in the LDAP namespace.
      java.util.Iterator<javax.naming.directory.SearchResult> search​(SearchFilter filter)
      This will query the LDAP with the supplied filter.
      java.util.Iterator<javax.naming.directory.SearchResult> search​(SearchFilter filter, java.lang.String[] retAttrs)
      This will query the LDAP with the supplied filter and return attributes.
      java.util.Iterator<javax.naming.directory.SearchResult> search​(SearchFilter filter, javax.naming.directory.SearchControls searchControls)
      This will query the LDAP with the supplied filter and search controls.
      java.util.Iterator<javax.naming.directory.SearchResult> search​(java.lang.String dn, SearchFilter filter)
      This will query the LDAP with the supplied dn and filter.
      java.util.Iterator<javax.naming.directory.SearchResult> search​(java.lang.String dn, SearchFilter filter, java.lang.String[] retAttrs)
      This will query the LDAP with the supplied dn, filter, and return attributes.
      java.util.Iterator<javax.naming.directory.SearchResult> search​(java.lang.String dn, SearchFilter filter, java.lang.String[] retAttrs, SearchResultHandler... handler)
      This will query the LDAP with the supplied dn, filter, return attributes, and search result handler.
      java.util.Iterator<javax.naming.directory.SearchResult> search​(java.lang.String dn, SearchFilter filter, javax.naming.directory.SearchControls searchControls)
      This will query the LDAP with the supplied dn, filter, and search controls.
      java.util.Iterator<javax.naming.directory.SearchResult> search​(java.lang.String dn, SearchFilter filter, javax.naming.directory.SearchControls searchControls, SearchResultHandler... handler)
      This will query the LDAP with the supplied dn, filter, search controls, and search result handler.
      java.util.Iterator<javax.naming.directory.SearchResult> searchAttributes​(java.lang.String dn, javax.naming.directory.Attributes matchAttrs)
      This will query the LDAP for the supplied dn and matching attributes.
      java.util.Iterator<javax.naming.directory.SearchResult> searchAttributes​(java.lang.String dn, javax.naming.directory.Attributes matchAttrs, java.lang.String[] retAttrs)
      This will query the LDAP for the supplied dn, matching attributes and return attributes.
      java.util.Iterator<javax.naming.directory.SearchResult> searchAttributes​(java.lang.String dn, javax.naming.directory.Attributes matchAttrs, java.lang.String[] retAttrs, SearchResultHandler... handler)
      This will query the LDAP for the supplied dn, matching attributes and return attributes.
      java.util.Iterator<javax.naming.directory.SearchResult> searchAttributes​(javax.naming.directory.Attributes matchAttrs)
      This will query the LDAP for the supplied matching attributes.
      java.util.Iterator<javax.naming.directory.SearchResult> searchAttributes​(javax.naming.directory.Attributes matchAttrs, java.lang.String[] retAttrs)
      This will query the LDAP for the supplied matching attributes and return attributes.
      void setLdapConfig​(LdapConfig ldapConfig)
      This will set the config parameters of this Ldap.
      • 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

      • 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​(java.io.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 javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • compare

        public boolean compare​(java.lang.String dn,
                               SearchFilter filter)
                        throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • search

        public java.util.Iterator<javax.naming.directory.SearchResult> search​(SearchFilter filter)
                                                                       throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • search

        public java.util.Iterator<javax.naming.directory.SearchResult> search​(SearchFilter filter,
                                                                              java.lang.String[] retAttrs)
                                                                       throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • search

        public java.util.Iterator<javax.naming.directory.SearchResult> search​(SearchFilter filter,
                                                                              javax.naming.directory.SearchControls searchControls)
                                                                       throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • search

        public java.util.Iterator<javax.naming.directory.SearchResult> search​(java.lang.String dn,
                                                                              SearchFilter filter)
                                                                       throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • search

        public java.util.Iterator<javax.naming.directory.SearchResult> search​(java.lang.String dn,
                                                                              SearchFilter filter,
                                                                              java.lang.String[] retAttrs)
                                                                       throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • search

        public java.util.Iterator<javax.naming.directory.SearchResult> search​(java.lang.String dn,
                                                                              SearchFilter filter,
                                                                              javax.naming.directory.SearchControls searchControls)
                                                                       throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • search

        public java.util.Iterator<javax.naming.directory.SearchResult> search​(java.lang.String dn,
                                                                              SearchFilter filter,
                                                                              java.lang.String[] retAttrs,
                                                                              SearchResultHandler... handler)
                                                                       throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • search

        public java.util.Iterator<javax.naming.directory.SearchResult> search​(java.lang.String dn,
                                                                              SearchFilter filter,
                                                                              javax.naming.directory.SearchControls searchControls,
                                                                              SearchResultHandler... handler)
                                                                       throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • searchAttributes

        public java.util.Iterator<javax.naming.directory.SearchResult> searchAttributes​(javax.naming.directory.Attributes matchAttrs)
                                                                                 throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • searchAttributes

        public java.util.Iterator<javax.naming.directory.SearchResult> searchAttributes​(javax.naming.directory.Attributes matchAttrs,
                                                                                        java.lang.String[] retAttrs)
                                                                                 throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • searchAttributes

        public java.util.Iterator<javax.naming.directory.SearchResult> searchAttributes​(java.lang.String dn,
                                                                                        javax.naming.directory.Attributes matchAttrs)
                                                                                 throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • searchAttributes

        public java.util.Iterator<javax.naming.directory.SearchResult> searchAttributes​(java.lang.String dn,
                                                                                        javax.naming.directory.Attributes matchAttrs,
                                                                                        java.lang.String[] retAttrs)
                                                                                 throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • searchAttributes

        public java.util.Iterator<javax.naming.directory.SearchResult> searchAttributes​(java.lang.String dn,
                                                                                        javax.naming.directory.Attributes matchAttrs,
                                                                                        java.lang.String[] retAttrs,
                                                                                        SearchResultHandler... handler)
                                                                                 throws javax.naming.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 javax.naming.DirContext#search(String, Attributes, String[]).
        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:
        javax.naming.NamingException - if the LDAP returns an error
      • list

        public java.util.Iterator<javax.naming.NameClassPair> list​(java.lang.String dn)
                                                            throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • listBindings

        public java.util.Iterator<javax.naming.Binding> listBindings​(java.lang.String dn)
                                                              throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • getAttributes

        public javax.naming.directory.Attributes getAttributes​(java.lang.String dn)
                                                        throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • getAttributes

        public javax.naming.directory.Attributes getAttributes​(java.lang.String dn,
                                                               java.lang.String[] retAttrs)
                                                        throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • getAttributes

        public javax.naming.directory.Attributes getAttributes​(java.lang.String dn,
                                                               java.lang.String[] retAttrs,
                                                               AttributeHandler... handler)
                                                        throws javax.naming.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 javax.naming.DirContext#getAttributes(String, String[]).
        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:
        javax.naming.NamingException - if the LDAP returns an error
      • getSchema

        public java.util.Iterator<javax.naming.directory.SearchResult> getSchema​(java.lang.String dn)
                                                                          throws javax.naming.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 javax.naming.DirContext#getSchema(String).
        Overrides:
        getSchema in class AbstractLdap<LdapConfig>
        Parameters:
        dn - String named object in the LDAP
        Returns:
        Iterator - LDAP search result
        Throws:
        javax.naming.NamingException - if the LDAP returns an error
      • modifyAttributes

        public void modifyAttributes​(java.lang.String dn,
                                     Ldap.AttributeModification mod,
                                     javax.naming.directory.Attributes attrs)
                              throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • modifyAttributes

        public void modifyAttributes​(java.lang.String dn,
                                     javax.naming.directory.ModificationItem[] mods)
                              throws javax.naming.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 javax.naming.DirContext#modifyAttributes(String, ModificationItem[]).
        Overrides:
        modifyAttributes in class AbstractLdap<LdapConfig>
        Parameters:
        dn - String named object in the LDAP
        mods - ModificationItem[] modifications
        Throws:
        javax.naming.NamingException - if the LDAP returns an error
      • create

        public void create​(java.lang.String dn,
                           javax.naming.directory.Attributes attrs)
                    throws javax.naming.NamingException
        This will create the supplied dn in the LDAP namespace with the supplied attributes. See javax.naming.DirContext#createSubcontext(String, Attributes). 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:
        javax.naming.NamingException - if the LDAP returns an error
      • rename

        public void rename​(java.lang.String oldDn,
                           java.lang.String newDn)
                    throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • delete

        public void delete​(java.lang.String dn)
                    throws javax.naming.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:
        javax.naming.NamingException - if the LDAP returns an error
      • getSaslMechanisms

        public java.lang.String[] getSaslMechanisms()
                                             throws javax.naming.NamingException
        This will return a list of SASL mechanisms that this LDAP supports.
        Returns:
        String[] - supported SASL mechanisms
        Throws:
        javax.naming.NamingException - if the LDAP returns an error
      • getSupportedControls

        public java.lang.String[] getSupportedControls()
                                                throws javax.naming.NamingException
        This will return a list of controls that this LDAP supports.
        Returns:
        String[] - supported controls
        Throws:
        javax.naming.NamingException - if the LDAP returns an error