Almost everything that you do with LDAP concerns the retrieval and manipulation of attributes. Due to the extensible nature of attributes finding out their types and content can be quite involved. Here is a simple sample code snip to the very basic of attribute look up including type information.
/**
* printAttributes
*
* Prints the attributes listed in ats to the stream defined by s. If ats
* is null all attributes will be listed. If ats is empty none will be
* listed.
*
* example:
* String obj = "uid=admin,ou=system";
* String[] ats = {"uid","displayName"};
* printAttributes(ctx,obj,ats,System.out);
*
*
* @param ctx initial directory context
* @param obj object in directory to retrieve attributes for
* @param ats attribute list of attribute names to retrieve, null for all
* @param s stream to write the attributes to
* @throws javax.naming.NamingException
*/
public static void printAttributes(InitialDirContext ctx, String obj,
String[] ats, PrintStream s) throws NamingException {
Attributes a = ctx.getAttributes(obj, ats);
NamingEnumeration e = a.getAll();
while (e.hasMore()) {
Attribute t = (Attribute)e.next();
s.println("<attribute name=\"" + t.getID() + "\">");
s.println(" <syntax>" + t.getAttributeSyntaxDefinition().getAttributes("") + "</syntax>");
s.println(" <schema>" + t.getAttributeDefinition().getAttributes("") + "</schema>");
NamingEnumeration f = t.getAll();
while (f.hasMore()){
Object z = f.next();
s.println(" <value>" + z + "</value>");
}
s.println("</attribute>");
}
}
This produces output that looks something like this:
<attribute name="cn">
<syntax>{x-schema=X-SCHEMA: system, x-is-human-readable=X-IS-HUMAN-READABLE: true, numericoid=NUMERICOID: 1.3.6.1.4.1.1466.115.121.1.15}</syntax>
<schema>{name=NAME: cn, commonName, substr=SUBSTR: caseIgnoreSubstringsMatch, x-schema=X-SCHEMA: system, syntax=SYNTAX: 1.3.6.1.4.1.1466.115.121.1.15, numericoid=NUMERICOID: 2.5.4.3, sup=SUP: name, desc=DESC: RFC2256: common name(s) for which the entity is known by, usage=USAGE: userApplications, equality=EQUALITY: caseIgnoreMatch}</schema>
<value>developer</value>
</attribute>