adamj
Sep 15th, 2006, 03:22 PM
I need to get the full DN of results returned by the ldapTemplate.search()
The easiest way I found of doing this was to use a NameClassPairCallbackHandler with code like this:
public void handleNameClassPair(NameClassPair nameClassPair) {
SearchResult result = (SearchResult)nameClassPair;
String fullDn = result.getNameInNamespace();
...
}
But getNameInNamespace() was not added to the API until Java 5, and I am constrained to use Java 1.4.
So I have resorted to using a ContextMapper like this:
public Object mapFromContext(Object object) {
DirContextAdapter searchResultContext = (DirContextAdapter)object;
String dn = searchResultContext.getNameInNamespace();
// but this is relative to the base DN, so:
if(baseDN.length() > 0) {
dn += "," + baseDN;
}
...
}
I'm not sure if it was the intention for DirContextAdapter's getNameInNamespace() to return DNs relative to the base, but this is inconsistent with Java 5's getNameInNamespace() behavior. Maybe this is an implementation error in the Spring LDAP API?
What's bothersome about my ContextMapper solution is I can't get the baseDN from either the ldapTemplate or even the ldapContextSource. So I have to expose a baseDN property on any beans that do LDAP searches, just so I can construct the full DN properly.
Maybe I'm just missing something in the Spring LDAP API? Is there a better way to get full DN? If not, can this be made easier in a future release?
The easiest way I found of doing this was to use a NameClassPairCallbackHandler with code like this:
public void handleNameClassPair(NameClassPair nameClassPair) {
SearchResult result = (SearchResult)nameClassPair;
String fullDn = result.getNameInNamespace();
...
}
But getNameInNamespace() was not added to the API until Java 5, and I am constrained to use Java 1.4.
So I have resorted to using a ContextMapper like this:
public Object mapFromContext(Object object) {
DirContextAdapter searchResultContext = (DirContextAdapter)object;
String dn = searchResultContext.getNameInNamespace();
// but this is relative to the base DN, so:
if(baseDN.length() > 0) {
dn += "," + baseDN;
}
...
}
I'm not sure if it was the intention for DirContextAdapter's getNameInNamespace() to return DNs relative to the base, but this is inconsistent with Java 5's getNameInNamespace() behavior. Maybe this is an implementation error in the Spring LDAP API?
What's bothersome about my ContextMapper solution is I can't get the baseDN from either the ldapTemplate or even the ldapContextSource. So I have to expose a baseDN property on any beans that do LDAP searches, just so I can construct the full DN properly.
Maybe I'm just missing something in the Spring LDAP API? Is there a better way to get full DN? If not, can this be made easier in a future release?