tags:

views:

133

answers:

3

I've implemented an object factory to lookup LDAP objects, but the supplied context does not return the DN (via nameCtx.getNameInNamespace()) from the LDAP. Am i doing it wrong in some way?

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.

A: 

Maybe?

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

It should be an attribute like any other?

cagcowboy
A: 
String dn = (String) attrs.get("dn").get();

this throws a NamingException only.
I don't think that the distinguished name (DN) is an attribute of the LDAP object, it's more like an identity key in the LDAP-world.

Oliver Michels
A: 

It could be that your context points to the "root node" or what ever it is called. That is, the node that has the top-level namespaces as its children.

I suppose it could also be that the context isn't bound at the time you call getNameInNamespace, although I would expect that to throw an exception.

I use spring-ldap for this sort of stuff, and have not experienced a similar bug with its DirContextAdapter and LdapTemplate classes. But then again, I always bind them to a specific namespace.

Christian Vest Hansen