Search code examples
javaattributesldapjndi

Retrieving operational (internal) LDAP attributes via JNDI in one batch


The JNDI InitialLdapContext class allows me to retrieve the attributes of an LDAP entry in one line if the Distinguished Name is known:

Attributes attributes = ctx.getAttributes(entryDN);

However, this does not include the operational attributes, such as the entryCSN, modifyTimestamp etc. Of course, one can always specify which attributes to get with a string array:

Attributes attributes = ctx.getAttributes(entryDN, new String[] {"entryCSN"});

But then only the specified attributes are returned.

Things I have tried, but don't work for me:

  1. Retrieving the attributes via ctx.search()

    I know I can get all the attributes via search (see here), but I don't want a) to do a whole Ldap query if I already know the dn and b) to deal cumbersomely which the search result set.

  2. Doing a second query only for the operational attribute

    Of course I can do just a second query, but I want to save the additional trip and adding the second attributes to first ones like this:

    Attributes attributes = ctx.getAttributes(entryDN);
    attributes.put(ctx.getAttributes(entryDN, new String[] {"entryCSN"}).get("entryCSN"));
    

    results in a NoSuchElementException. Stacktrace:

    Exception thrown: java.util.NoSuchElementException: Vector Enumeration
    at java.util.Vector$1.nextElement(Vector.java:352)
    at javax.naming.directory.BasicAttribute$ValuesEnumImpl.nextElement(BasicAttribute.java:537)
    
  3. Listing all attributes in the string array

    Since the Ldap entries returned can be different object classes and have therefore different attributes, I see no practical way to do this.

Does anyone know how to get the normal attributes and the operational attributes in one trip?


Solution

  • There are 2 magic values for attributes that are defined in LDAP RFCs : "*" means all user attributes. "+" means all operational attributes.

    The following code should work:

    Attributes attributes = ctx.getAttributes(entryDN, new String[] {"*", "+"});