Search code examples
ldapspring-ldapldap-query

Getting Parent DN in LdapTemplate call


Using Spring LdapTemplate I am able to perform a paginated full LDAP search for a subset of users using the top level DN as the base and filters. I prefer this method to using a list of parent DNs because it doesn't require knowing the parent DNs before getting all the required users. Using a debugger I do not see the parent DN being returned from the server.

Is there a way to derive or include the parent DN in the record retrieved from LDAP?

The second best would be a query that would return all the second level DNs one level below the top level DN. Sorry if I'm not using the right terminology, I'm quite new to working with LDAP. Is there a way to do this?

Thanks!


Solution

  • AFAIK there is no JNDI method to retrieve the parent DN of a given object. The only way I know for sure how to do this, is to use two methods on a SearchResult that return the object name and the full object DN. Something like this:

    // Your own method to retrieve the next LDAP object
    SearchResult res = getNextSearchResult(); 
    String objectName = res.getName();
    // Typical markup: CN=<objectName>,<parentDN>
    String fullDn = res.getNameInNamespace(); 
    String parentDn = fullDn.substring(fullDn.indexOf(objectName)+objectName.length()+1);