If your system is migrated from LDAP server to AD server and you want to access the respective attributes from AD server, you can use this program to achieve the goal.
import java.util.Hashtable;
import java.util.Enumeration;
import javax.naming.*;
import javax.naming.directory.*;
class test {
public static void main(String[] args) {
// Identify service provider to use
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://dsx.test.com:389");
//env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "testnet.gen");
env.put(Context.SECURITY_CREDENTIALS, "supportsn");
try {
// Create the initial directory context
DirContext ctx = new InitialDirContext(env);
System.out.println("Fetching data from ldap server");
// Specify the ids of the attributes to return
String[] attrIDs = {"telephoneNumber", "sn","mail","company","l","st","co"};
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
// Specify the search scope
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Specify the search filter to match
String filter = "(&(uid=nbkid)(objectclass=user)(objectCategory=person))";
// String filter = "(&(uid=chambers))";
// Specify the base dn
String baseDN = "OU=ccoentities,O=cco.test.com";
// Search the subtree for objects by using the filter
NamingEnumeration answer = ctx.search(baseDN, filter, ctls);
while (answer.hasMore()) {
SearchResult sr = (SearchResult)answer.next();
System.out.println(">>>" + sr.getName());
Attributes attrs = sr.getAttributes();
System.out.println(attrs.get("telephoneNumber").get());
System.out.println(attrs.get("sn").get());
System.out.println(attrs.get("mail").get());
System.out.println(attrs.get("co").get());
System.out.println(attrs.get("company").get());
System.out.println(attrs.get("l").get());
System.out.println(attrs.get("st").get());
}
answer.close();
ctx.close();
} catch (Exception e) {
System.err.println("Problem getting attribute: " + e);
// Do your exception handling
}
}
}