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


String dn = (String) attrs.get("dn").get();

      



it throws away only NamingException

.
I don't think the distinguished name (DN) is an attribute of an LDAP object, it looks more like an identification key in the LDAP world.

+1


source


May be,?

String dn = (String) attrs.get("dn").get();

      



Should it be an attribute like any other?

0


source


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







All Articles