Reading an Object's Attributes from the Directory
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class Main {
public static void main(String[] argv) throws Exception {
String url = "ldap://localhost/o=JNDITutorial";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
DirContext ctx = new InitialDirContext(env);
String[] attrIDs = { "sn", "number", "value", "mail" };
Attributes answer = ctx.getAttributes("cn=yourName, ou=People", attrIDs);
NamingEnumeration e = answer.getAll();
while (e.hasMore()) {
Attribute attr = (Attribute) e.next();
System.out.println(attr.getID());
}
}
}
Related examples in the same category