tags:

views:

110

answers:

3

I'm hoping someone can help me understand how to work with the object returned by a call to DirContext.lookup.

The following code snippet works and returns an object. I just can't figure out how to get the attributes from the object.

javax.naming.directory.DirContext ctx =
    javax.naming.directory.getContext(false);
Object o = ctx.lookup(rdn);

Any help would be much appreciated.

A: 

You should know what object you are expecting to receive from the lookup(), explicitly cast to it, and then do whatever you want with it.

In the end you should have something like this:

InitialContext iCtx = new InitialContext();
// load the iCtx with environment variables if necessary
Object o = iCtx.lookup("objectNameOrString");
ExpectedObjectType eot = (ExpectedObjectType) o;
eot.doWhatever();
Yuval A
I must be missing something here. I know the object class of the object in LDAP but I don't have a corresponding application object. There must some reflection-like mechanism for querying the object's attributes?
James Watt
A: 

In an LDAP directory, you would do:

Attributes attrs = ctx.getAttributes(dn);

To obtain the attributes of the object

Maurice Perry
+2  A: 

Attributes attrs = ctx.getAttributes(dn); will retrieve the user attributes assuming the entry asking for the arrtibute values has proper rights.

However, best practice is you only query for the attributes you need.

If you wish to see all attributes, you should query the objectclass attribute values and then query the schema to obtain "all" the attributes assigned and decide which attributes you need to retrieve.

-jim

jeemster