Using javax.naming.spi.DirObjectFactory to find objects from LDAP
I have implemented a factory object to look up LDAP objects, but the context provided does not return the DN (via nameCtx.getNameInNamespace ()) from LDAP. Am I doing something wrong?
public class LdapPersonFactory implements DirObjectFactory {
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable<?, ?> environment, Attributes attrs) throws Exception {
if (attrs == null)
return null;
Attribute oc = attrs.get("objectclass");
if (oc != null && oc.contains("inetOrgPerson")) {
String surname = (String) attrs.get("sn").get();
String givenName = (String) attrs.get("givenname").get();
String dn = nameCtx.getNameInNamespace();
return new LdapPerson(dn, givenName, surname);
}
return null;
}
}
nameCtx.getNameInNamespace () only returns an empty string.
+1
source to share
3 answers
Perhaps your context points to the "root node" or what ever it is called. That is, a node that has top level name names as its children.
I also assume that the context is not associated with you when you call getNameInNamespace, although I would expect it to throw an exception.
I am using spring-ldap for this kind of thing and am not experiencing a similar error with its DirContextAdapter and LdapTemplate classes. But again, I always bind them to a specific namespace.
0
source to share