Reputation: 574
I'm trying to query an ldap based on a list of userIds acquired from an Oracle database. If the account exists in the LDAP, I'm wanting to change two values for the given object. The problem I'm having is that result is never null for some reason, so my code always tries to change the value, even if the user doesn't exist on the LDAP. You'll see that I printed the value of "result" for testing purposes. The output for result is:
com.sun.jndi.ldap.LdapSearchEnumeration@3a747fa2
with everything after the "@" being a random value for each query. Is there something I'm doing wrong?
Here's my code:
public void queryLdap(String netId, LdapContext context)
{
String uId = "(uid=" + netId + ")";
String userDn="uid=" + netId + "," + domainRoot;
try{
SearchResult searchResult = null;
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs = {"pdsAccountStatus", "inetUserStatus"};
//controls.setReturningAttributes(attrIDs);
NamingEnumeration<SearchResult> result = context.search(domainRoot, uId, controls);
System.out.println(result);
if(result != null){
changeLdap(userDn);
}
}
catch(Exception ex){
System.out.println("NamingEnumeration Error in queryLdap: " + ex);
}
}
Upvotes: 0
Views: 1753
Reputation: 2942
The return type is a NamingEnumeration<SearchResult>
which is never null.
You need to change your result check to use methods that the NamingEnumeration has available:
if ( result.hasMore() )
{
changeLdap( userDn );
}
But it would be even better to actually use the results using a while
loop, however that depends on your use-case:
while ( result.hasMore() )
{
try {
SearchResult res = result.next();
String objDn = res.getNameInNamespace();
// Use the Dn of the returned object
} catch (NamingException e) {
// Whatever
}
}
Upvotes: 1