edu.internet2.middleware.ldappc.util
Class LdapUtil

java.lang.Object
  extended by edu.internet2.middleware.ldappc.util.LdapUtil

public final class LdapUtil
extends Object

This provides utility methods for interacting with a LDAP directory.


Field Summary
static String EMPTY_NAME
          Empty name string for name parser.
static String MULTIVALUED_RDN_DELIMITER
          Delimiter for multivalued RDNs.
static String OBJECT_CLASS_ATTRIBUTE
          Object class attribute name.
 
Method Summary
static String canonicalizeDn(String dn)
          Normalize LDAP DN using LdapDN.
static String convertParameterToAsterisk(String filter, int parameterIndex)
          Converts a "{i}" ldap query filter parameter to "*" where i >= 0.
static void delete(Ldappc ldappc, Name dn)
          This deletes the object identified by the given dn and any child objects.
static String escapeForwardSlash(String dn)
          Escape all forward slashes "/" with "\/".
static List<String> getChildDNs(String dn, edu.vt.middleware.ldap.Ldap ldap)
          Return a list of child DNs under the given DN, in (reverse) order suitable for deletion.
static List<String> getChildDNs(String dn, LdapContext ldap)
          Return a list of child DNs under the given DN, in (reverse) order suitable for deletion.
static String getLdif(Attribute attribute)
          Create an LDIF representation.
static String getLdif(Attributes attributes)
          Create an LDIF representation.
static String getLdifAdd(org.apache.directory.shared.ldap.name.LdapDN dn, Attributes attributes)
           
static String getLdifDelete(org.apache.directory.shared.ldap.name.LdapDN dn)
           
static String getLdifModify(org.apache.directory.shared.ldap.name.LdapDN dn, ModificationItem[] modificationItems)
           
static Name getName(NameParser parser, SearchResult searchResult)
          Return the Name of a SearchResult via getName().
static String getParentDn(String dn)
           
static String makeLdapFilterValueSafe(String value)
          This method converts '*','(',')' and the "null" character (i.e., 0x00) to be a forward slash (i.e., \) and the two hex character value as defined in RFC2254.
static String makeLdapNameSafe(String value)
          This method escapes all LDAP special characters in the value.
static BufferedReader openReader(File file)
          Open file for reading.
static BufferedWriter openWriter(File file)
          Open the file for writing.
static Attributes searchAttributes(edu.vt.middleware.ldap.Ldap ldap, String dn)
          Returns all attributes.
static Attributes searchAttributes(edu.vt.middleware.ldap.Ldap ldap, String dn, String[] retAttrs)
          Returns the attributes of a given LDAP dn.
static Iterator<SearchResult> searchEntryDn(edu.vt.middleware.ldap.Ldap ldap, String dn, String[] retAttrs)
          Perform a search for a given LDAP entry.
static String unescapeForwardSlash(String dn)
          Remove the escape character "\" from all escaped forward slashes "\/", returning "/".
static void writeLdif(BufferedWriter writer, String ldif)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OBJECT_CLASS_ATTRIBUTE

public static final String OBJECT_CLASS_ATTRIBUTE
Object class attribute name.

See Also:
Constant Field Values

EMPTY_NAME

public static final String EMPTY_NAME
Empty name string for name parser.

See Also:
Constant Field Values

MULTIVALUED_RDN_DELIMITER

public static final String MULTIVALUED_RDN_DELIMITER
Delimiter for multivalued RDNs.

See Also:
Constant Field Values
Method Detail

delete

public static void delete(Ldappc ldappc,
                          Name dn)
                   throws NamingException
This deletes the object identified by the given dn and any child objects.

Parameters:
ldappc -
dn - the DN to delete
Throws:
NamingException

makeLdapFilterValueSafe

public static String makeLdapFilterValueSafe(String value)
This method converts '*','(',')' and the "null" character (i.e., 0x00) to be a forward slash (i.e., \) and the two hex character value as defined in RFC2254. For example, the string "abc * efg" is converted to "abc \2a efg".

Parameters:
value - String to make safe
Returns:
Ldap filter value safe string

makeLdapNameSafe

public static String makeLdapNameSafe(String value)
This method escapes all LDAP special characters in the value. This way it is safe to use as part of an LDAP DN.

Parameters:
value - String to make safe
Returns:
LDAP name safe string

convertParameterToAsterisk

public static String convertParameterToAsterisk(String filter,
                                                int parameterIndex)
Converts a "{i}" ldap query filter parameter to "*" where i >= 0.

Parameters:
filter - Ldap query filter
parameterIndex - Index of the parameter to alter
Returns:
A new ldap query filter with "{i}" replaced with "*", or an empty string if the filter is null.

getName

public static Name getName(NameParser parser,
                           SearchResult searchResult)
                    throws NamingException
Return the Name of a SearchResult via getName().

Parameters:
parser - the Context's NameParser
searchResult - the SearchResult
Returns:
the Name
Throws:
NamingException

getLdif

public static String getLdif(Attribute attribute)
                      throws NamingException
Create an LDIF representation.

Parameters:
attribute -
Returns:
the LDIF representation
Throws:
NamingException

getLdif

public static String getLdif(Attributes attributes)
                      throws NamingException
Create an LDIF representation.

Parameters:
attributes -
Returns:
the LDIF representation
Throws:
NamingException

getLdifModify

public static String getLdifModify(org.apache.directory.shared.ldap.name.LdapDN dn,
                                   ModificationItem[] modificationItems)
                            throws NamingException
Throws:
NamingException

getLdifAdd

public static String getLdifAdd(org.apache.directory.shared.ldap.name.LdapDN dn,
                                Attributes attributes)
                         throws NamingException
Throws:
NamingException

getLdifDelete

public static String getLdifDelete(org.apache.directory.shared.ldap.name.LdapDN dn)
                            throws NamingException
Throws:
NamingException

writeLdif

public static void writeLdif(BufferedWriter writer,
                             String ldif)

openWriter

public static BufferedWriter openWriter(File file)
                                 throws LdappcException
Open the file for writing.

Parameters:
file - File to write to.
Returns:
BufferedWriter for the file.
Throws:
LdappcException - thrown if the file cannot be opened.

openReader

public static BufferedReader openReader(File file)
                                 throws LdappcException
Open file for reading.

Parameters:
file - File to read from.
Returns:
BufferedReader for the file.
Throws:
LdappcException - thrown if the file cannot be opened.

getChildDNs

public static List<String> getChildDNs(String dn,
                                       edu.vt.middleware.ldap.Ldap ldap)
                                throws NamingException
Return a list of child DNs under the given DN, in (reverse) order suitable for deletion. This method requires the use of the FqdnSearchResultHandler.

Parameters:
dn - the dn to delete, as well as all children
ldap - the ldap connection
Returns:
Throws:
NamingException

getChildDNs

public static List<String> getChildDNs(String dn,
                                       LdapContext ldap)
                                throws NamingException
Return a list of child DNs under the given DN, in (reverse) order suitable for deletion.

Parameters:
dn - the dn to delete, as well as all children
ldap - the ldap connection
Returns:
Throws:
NamingException

searchAttributes

public static Attributes searchAttributes(edu.vt.middleware.ldap.Ldap ldap,
                                          String dn)
                                   throws NamingException,
                                          LdappcException
Returns all attributes. See searchAttributes(Ldap, String, String[])

Throws:
NamingException
LdappcException

searchAttributes

public static Attributes searchAttributes(edu.vt.middleware.ldap.Ldap ldap,
                                          String dn,
                                          String[] retAttrs)
                                   throws NamingException,
                                          LdappcException
Returns the attributes of a given LDAP dn. The underlying LdapContext method used is search(), rather than getAttributes(), so that SearchResultHandlers are used. The filter is (objectclass=*), the base is the given dn, and the scope is OBJECT.

Parameters:
ldap - the vt-ldap connection
dn - the dn to search for
retAttrs - the attr names to return
Returns:
Throws:
NamingException
LdappcException - if one and only one matching dn is not found

searchEntryDn

public static Iterator<SearchResult> searchEntryDn(edu.vt.middleware.ldap.Ldap ldap,
                                                   String dn,
                                                   String[] retAttrs)
                                            throws NamingException
Perform a search for a given LDAP entry. The underlying LdapContext method used is search(), rather than getAttributes(), so that SearchResultHandlers are used. The filter is (objectclass=*), the base is the given dn, and the scope is OBJECT.

Parameters:
ldap -
dn -
retAttrs -
Returns:
Throws:
NamingException

escapeForwardSlash

public static String escapeForwardSlash(String dn)
Escape all forward slashes "/" with "\/".

Parameters:
dn -
Returns:
the resultant string with / replaced with \/

unescapeForwardSlash

public static String unescapeForwardSlash(String dn)
Remove the escape character "\" from all escaped forward slashes "\/", returning "/".

Parameters:
dn -
Returns:
the resultant string

canonicalizeDn

public static String canonicalizeDn(String dn)
                             throws InvalidNameException
Normalize LDAP DN using LdapDN. This will convert RDN attributeTypes to lowercase, which is of interest since Active Directory usually (?) returns attributeTypes uppercased.

Parameters:
dn -
Returns:
the lowercased and normalized dn
Throws:
InvalidNameException

getParentDn

public static String getParentDn(String dn)
                          throws InvalidNameException
Throws:
InvalidNameException


Copyright © 2011 Internet2. All Rights Reserved.