Getting elements out of a NamingEnumeration

Andreas picture Andreas · Mar 14, 2012 · Viewed 9.3k times · Source

I'm trying to get the elements out of a namingenumeration. The namingenumeration itself is not null, but hasNext() gives me false.

What am I doing wrong?

public static void main(String[] args) {
try {
            DirContext context = new InitialDirContext(
                    Environment.getEnvironment());



            SearchControls controls = new SearchControls();
            controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
             String[] attrIDs = { "cn", "givenname", "sn", "mail" };
             controls.setReturningAttributes(attrIDs);
            NamingEnumeration enumResult = context.search(
                    "DC=PORTAL,DC=COMPANY,DC=BE", "(CN=*)",
                    controls);
            System.out.println(enumResult.hasMore());

            context.close();

        } catch (AuthenticationException e) {
            System.out.println("Invalid credentials");
        } catch (NamingException e) {
            System.out.println("Lookup failed: " + e);
        }
}

Structure of AD (on Localhost using AD-LDS)

DC=PORTAL,DC=COMPANY,DC=BE
->OU=Accounts
==>CN=John Doe
==>CN=Jane Doe
->CN=LostAndFound
->CN=NTDS Quotas
->CN=Roles
->OU=System Accounts
==>CN=PortalAdmin

Structure of Directory

Narrowing my searchbase to "OU=ACCOUNTS,DC=PORTAL,DC=COMPANY,DC=BE" gives the following error

Lookup failed: javax.naming.NameNotFoundException: [LDAP: error code 32 - 000020 8D: NameErr: DSID-031522C9, problem 2001 (NO_OBJECT), data 0, best match of: 'DC=PORTAL,DC=COMPANY,DC=BE' ]; remaining name 'OU=ACCOUNTS,DC=PORTAL,DC=COMPANY,DC=BE'


solution:

try {

            DirContext ctx = new InitialDirContext(Environment.getEnvironment());



            // Get all the attributes of named object
            Attributes attrs = ctx
                    .getAttributes("cn=John Doe,ou=Accounts,DC=PORTAL,DC=COMPANY,DC=BE");

            if (attrs == null) {
                System.out.println("No attributes");
            } else {
                /* Print each attribute */
                try {
                    for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) {
                        Attribute attr = (Attribute) ae.next();
                        System.out.println("attribute: " + attr.getID());

                        /* print each value */
                        for (NamingEnumeration e = attr.getAll(); e.hasMore(); System.out
                                .println("value: " + e.next()))
                            ;
                    }
                } catch (NamingException e) {
                    e.printStackTrace();
                }
            }



            ctx.close();

        } catch (AuthenticationException e) {
            System.out.println("Invalid credentials");
        } catch (NamingException e) {
            System.out.println("Lookup failed: " + e);
        }

Answer

Óscar López picture Óscar López · Mar 14, 2012

Start with doing some basic sanity checks. For instance, that the data returned by Environment.getEnvironment() is correct (url, port, user, password) and allows a connection to the directory server. Also check that there are no network problems and that you can, in fact, access the server.

Try limiting the search base a bit more, for instance: "OU=Accounts,DC=PORTAL,DC=COMPANY,DC=BE" and see if some results are returned. Also check if the objects in the expected results actually have the attributes "cn", "givenname", "sn", "mail".

Other than that, there are no obvious mistakes in the code shown in the question, it should work fine.