fetching data from ldap server using java - java

Please find my code snippet below:
String[] attrIDs = {"title", "Depatrment", "DivisionDescription" };
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> answer = ctx.search(ldapServerSearchBase, FILTER, ctls);
SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
String str_title = attrs.get("title").toString();
String str_dept = null;
String str_desc = null;
if(str_title.equals("Professor"))
{
str_dept = attrs.get("Depatrment").toString();
str_desc = attrs.get("DivisionDescription").toString();
}
System.out.println(str_title);
System.out.println(str_dept);
System.out.println(str_desc);
When I run this code, it always displays null when i output str_dept and str_desc. But it does display str_title correctly as "Professor".
Please help me understand what could be going wrong here.
Thanks!

Are you sure that the attributes you're fetching even exists?
str_dept = attrs.get("Depatrment").toString();
str_desc = attrs.get("DivisionDescription").toString();
Firstly, the Depatrment is spelled wrong, and the DivisionDescription most likely does not exists (at least not in the default schematics for most directory services). Might be you need the following attributes?:
DivisionDescription => division
Depatrment => department
However, check that your directory service actually even posess the requested attributes (and that users actually have some values within these attributes).

Although I am sure, if more information were provided, there is a better method to do this, the example below should get you on your way:
String[] attrIDs = {"title", "Depatrment", "DivisionDescription" };
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> answer = ctx.search(ldapServerSearchBase, filter, ctls);
SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
String str_title=null;
String str_dept = null;
String str_desc = null;
if( attrs.get("title")!=null)
{
str_title = attrs.get("title").toString();
System.out.println(str_title);
if(str_title.equals("Professor"))
{
str_dept = attrs.get("Depatrment").toString();
str_desc = attrs.get("DivisionDescription").toString();
System.out.println(str_dept);
System.out.println(str_desc);
}
}
-jim

Related

How to get all users from specific ou in active directory using java?

Here is the method which i have used to fetch but nothing is being populated.
public void doSearch() throws NamingException {
String searchFilter = "(&(ou=Example,ou=Examples_ou)(objectClass=person))";
String domain = "DC=mydom,DC=com";
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> answer = ctx.search(domain, searchFilter, searchControls);
int ttl = 0;
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult) answer.next();
ttl++;
System.out.println(">>>" + sr.getName());
Attributes attrs = sr.getAttributes();
System.out.println(">>>>>>" + attrs.get("samAccountName"));
}
System.out.println("Total results: " + ttl);
}
Your filter is invalid, thus returns no data. In
ctx.search(domain, searchFilter, searchControls);
domain is being passed as the base DN for the search. If you want to restrict your search to users within ou=Example,ou=Examples_ou of dc=example,dc=com, then your search base DN should be "ou=Example,ou=Examples_ou,dc=example,dc=com" and searchFilter would be simply "(&(objectClass=person))"

Java: LDAP Search returning 1 row

I am trying to get all users from my active directory however my code is returning just one row. I have tried the below which is currently only outputting one user.
private void getUserBasicAttributes(String username, LdapContext ctx) {
try {
List<String> usersList = new ArrayList<String>();
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
//First input parameter is search bas, it can be "CN=Users,DC=YourDomain,DC=com"
//Second Attribute can be uid=username
NamingEnumeration<SearchResult> answer = ctx.search("DC=domain,DC=com", "(&(objectCategory=user))"
, constraints);
if (answer.hasMoreElements()) {
Person person = new Person();
SearchResult attrs = ((SearchResult) answer.next());
String names[] = attrs.getName().split(",");
String name[] = names[0].split("=");
usersList.add(name[1]);
}else{
throw new Exception("Invalid User");
}
System.out.println(usersList.size());
} catch (Exception ex) {
ex.printStackTrace();
}
}
You are not looping over all the results, add a while loop inside the if
if (answer.hasMoreElements()) {
while(answer.hasMoreElements()) {
Person person = new Person();
SearchResult attrs = ((SearchResult) answer.next());
String names[] = attrs.getName().split(",");
String name[] = names[0].split("=");
usersList.add(name[1]);
}
}else{
throw new Exception("Invalid User");
}
You need while instead of if:
while (answer.hasMoreElements()) {
Person person = new Person();
SearchResult attrs = ((SearchResult) answer.next());
String names[] = attrs.getName().split(",");
String name[] = names[0].split("=");
usersList.add(name[1]);
}
if (usersList.size() == 0) {
throw new Exception("Invalid User");
}
You can simplify the name-element handling as well. No need to parse the DN. Just specify the attribute(s) you want returned up front and retrieve them directly.
You are making this too hard. No reason to perform any "splitting" pf values.
// Specify the ids of the attributes to return
String[] attrIDs = { "uid" };
// Get ONLY the attributes desired
Attributes answer = ctx.getAttributes("CN=Users,DC=YourDomain,DC=com", attrIDs);
for (NamingEnumeration ae = answer.getAll(); ae.hasMore();) {
Attribute attr = (Attribute)ae.next();
System.out.println("attribute: " + attr.getID());
/* Print each value */
for (NamingEnumeration e = attr.getAll(); e.hasMore();
System.out.println(e.next()))
;
}
Let me know how I can help.

JNDI: How to use Logical not query to search LDAP

I am facing problem while applying logical not query.
For example,
NamingEnumeration<SearchResult> results =
context.search("ou=people,dc=example,dc=com", "sn=Kumaran", searcCon);
Above statement is working absolutely fine, it is returning all the person entries whose sn is Kumaran.
Suppose that I change the statement to get all the person entries whose sn is not Kumaran.
NamingEnumeration<SearchResult> results =
context.search("ou=people,dc=example,dc=com", "(!sn=Kumaran)", searcCon);
I am getting following error:
Exception in thread "main" javax.naming.directory.InvalidSearchFilterException: Unbalanced parenthesis; remaining name 'ou=people,dc=example,dc=com'
at com.sun.jndi.ldap.Filter.findRightParen(Filter.java:694)
at com.sun.jndi.ldap.Filter.encodeFilterList(Filter.java:733)
at com.sun.jndi.ldap.Filter.encodeComplexFilter(Filter.java:657)
at com.sun.jndi.ldap.Filter.encodeFilter(Filter.java:118)
at com.sun.jndi.ldap.Filter.encodeFilterString(Filter.java:74)
at com.sun.jndi.ldap.LdapClient.search(LdapClient.java:548)
at com.sun.jndi.ldap.LdapCtx.doSearch(LdapCtx.java:1985)
at com.sun.jndi.ldap.LdapCtx.searchAux(LdapCtx.java:1844)
at com.sun.jndi.ldap.LdapCtx.c_search(LdapCtx.java:1769)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_search(ComponentDirContext.java:392)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(PartialCompositeDirContext.java:358)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(PartialCompositeDirContext.java:341)
at javax.naming.directory.InitialDirContext.search(InitialDirContext.java:267)
at jndi_tutorial.Test.getPeopleSnIsKumaran(Test.java:34)
at jndi_tutorial.Test.main(Test.java:62)
Following is my code
public static void printResults(NamingEnumeration<SearchResult> results)
throws NamingException {
while (results.hasMoreElements()) {
SearchResult res = results.next();
Attributes atbs = res.getAttributes();
Attribute nameAttr = atbs.get("cn");
String name = (String) nameAttr.get();
Attribute descriptionAtr = atbs.get("description");
String description = (String) descriptionAtr.get();
System.out.println(description);
System.out.println("Name is :" + name + ",description is :"
+ description);
}
}
/* Get all people whose sn is not equal to Kumaran */
public static void getAllExcludeKumaran() throws NamingException {
SearchControls searcCon = new SearchControls();
searcCon.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = context.search(
"ou=people,dc=example,dc=com", "(!(sn=Kumaran))", searcCon);
printResults(results);
}
Reference
https://technet.microsoft.com/en-us/library/aa996205(v=exchg.65).aspx
Try to add parenthesis (!(sn=Kumaran))
UPDATE:
Try this
...
SearchControls searcCon = new SearchControls();
String[] attributeFilter = { "cn", "description" };
searcCon.setReturningAttributes(attributeFilter);
searcCon.setSearchScope(SearchControls.SUBTREE_SCOPE);
...
See RFC 2254:
filter ::= "(" filtercomp ")"
not ::= "!" filter
Your code should be like
context.search("ou=people,dc=example,dc=com", "((!sn=Kumaran))", searcCon);
It should work for you.

LDAP Query Accessing a Slow Response Attribute from Java (employeeID)

I am currently maintaining some library scanning software at a company, and I am at a coop for (I am in high school). I have to be able to pass an employeeID value from the scanner to the LDAP lookup to return a "CN" value.
Unfortunately, I am not getting any results returned in the Java program. I am able to search using the Active Directory program in Windows, but it takes 6 to 10 seconds to display any results from employeeID. I attempted to solve this issue by using a very large timeout limit on the query, but I think I must be doing something wrong.
Anyone with database experience have any ideas?
try
{
System.out.println("Début du test Active Directory");
Hashtable<String, String> env = new Hashtable<String, String>(11);
env.put(INSERT CREDENTIALS HERE);
env.put("com.sun.jndi.ldap.timeout", "80000");
env.put(Context.SECURITY_PROTOCOL, "ssl");
env.put(Context.SECURITY_PROTOCOL, "simple");
ldapContext = new InitialDirContext(env);
// Create the search controls
SearchControls searchCtls = new SearchControls();
//Specify the attributes to return
String returnedAtts[]={"cn","givenName", "samAccountName"};
searchCtls.setReturningAttributes(returnedAtts);
//Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//specify the LDAP search filter
id = "********";
String searchFilter = "(&(employeeID="+id+"))";
//Specify the Base for the search
String searchBase = "dc=ericsson,dc=se";
//initialize counter to total the results
SearchResult sr = null;
int totalResults = 0;
NamingEnumeration<SearchResult> answer = ldapContext.search(searchBase, searchFilter, searchCtls);
// Search for objects using the filter
while (totalResults == 0){
answer = ldapContext.search(searchBase, searchFilter, searchCtls);
System.out.println("Total results: " + totalResults);
while (answer.hasMoreElements())
{
sr = answer.next();
System.out.println(sr);
totalResults++;
System.out.println(">>>" + sr.getName());
Attributes attrs = sr.getAttributes();
cn = (">>>>>>>>>" + attrs.get("cn"));
signum = cn.substring(13,20);
System.out.println("Total results: " + totalResults);
}
}
//Loop through the search results
You need to ensure that the employeeID attribute is indexed.
You should also qualify your filter further. I would add at least an objectClass filter, set to whatever object class you're using for people.

getting Manager's email ID from LDAP?

I am trying to get a person's Email ID as well as his/her manager's email ID. And the following is the code I tried.
DirContext ctx = new InitialDirContext(LDAPDetails());
String[] attrIDs = {"sAMAccountName", "cn", "title", "mailnickname", "mail", "manager", "department", "telephoneNumber"};
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String filter = "(CN=285263)";
NamingEnumeration<SearchResult> answer = ctx.search("OU=users,DC=cts,DC=com", filter , ctls);
answer = ctx.search("OU=xyz,DC=cts,DC=com", filter , ctls);
while (answer.hasMore()) {
SearchResult sr = (SearchResult) retEnum.next();
Attribute mailAttribute=sr.getAttributes().get("mail");
System.out.println("Team Member's eMail: "+mailAttribute.get()); //Here I am able to get the person's email.
Attribute managerAttribute=sr.getAttributes().get("manager"); // this is just getting the manager's CN value. Not the email ID.
}
Can someone help me get the manager's email ID please? Thanks in advance.
You'll have to lookup the manager(s) to get his (their) email address(es):
DirContext ctx = new InitialDirContext(LDAPDetails());
String[] attrIDs = { "sAMAccountName", "cn", "title", "mailnickname", "mail", "manager", "department", "telephoneNumber" };
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String filter = "(CN=285263)";
NamingEnumeration<SearchResult> answer = ctx.search("OU=users,DC=cts,DC=com", filter, ctls);
while (answer.hasMore()) {
SearchResult sr = (SearchResult) retEnum.next();
Attribute mailAttribute = sr.getAttributes().get("mail");
System.out.println("Team Member's eMail: " + mailAttribute.get()); // Here I am able to get the person's email.
Attribute managerAttribute = sr.getAttributes().get("manager"); // this is just getting the manager's CN value. Not the email ID.
// now lookup the manger
NamingEnumeration<SearchResult> managerAnswer = ctx.search(managerAttribute.get(), "", ctls);
while (answer.hasMore()) {
SearchResult managerSr = (SearchResult) retEnum.next();
Attribute mailAttribute = sr.getAttributes().get("mail");
System.out.println("Managers eMail: " + mailAttribute.get());
}
}
Here is my approach...
1..Connecto to LDAP
2..Search the user and Get manager name
3.. Search the LDAP with the manager name and object class as user in filter condition.
NamingEnumeration answermanager = context.search(managerAttribute.get().toString(),"(objectClass=user)",searchCtls);

Categories