When I do a lookup for a DN with Springs ldapTemplate, I have to omit the configured base DN.
For example when the base DN is configured as dc=company,dc=com
, a working lookup would be like
ldapTemplate.lookup("ou=whatever,ou=groups")
When I pass the fully qualified DN
ldapTemplate.lookup("ou=whatever,ou=groups,dc=company,dc=com")
the lookup fails and complains that the DN ou=whatever,ou=groups,dc=company,dc=com,dc=company,dc=com
(note the doubled base at the end) does not exist.
As a consequence, when I do lookups based on returned DNs from previous lookups, I have to remove the base from the end of the DN.
This is quite annoying and I am almost sure that there is a better way to do this.
So, how can I do a lookup for a fully qualified DN with a configured base DN?
I know there is a query method that takes a base argument, but this seems to be made for more real queries, not simple lookups.
LdapName fqdn = ... //fully qualified DN with base DN that is returned from lookup
LdapName baseDn = ((LdapContextSource) ldapTemplate.getContextSource()).getBaseLdapName();
LdapName lookupDn = LdapUtils.removeFirst(fqdn, baseDn);
According to the JavaDoc, this is what LdapUtils#removeFirst
is for:
/**
* Remove the supplied path from the beginning the specified
* <code>Name</code> if the name instance starts with
* <code>path</code>. Useful for stripping base path suffix from a
* <code>Name</code>.
**/
Alternatively, you can configure your ContextSource
with no base. Then, all your queries use a fully-qualified name. Or, you can use more than one ContextSource
; one that has a base configured and another that does not.