Getting items from NamingEnumeration

I am trying to get items from namingenumeration. The naming name 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);
        }
}

      

AD structure (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 down my search base to "OU = ACCOUNTS, DC = PORTAL, DC = COMPANY, DC = BE" gives the following error

Search failed: javax.naming.NameNotFoundException: [LDAP: Error Code 32 - 000020 8D: NameErr: DSID-031522C9, Issue 2001 (NO_OBJECT), Data 0, Best Match: 'DC = PORTAL, DC = COMPANY, DC = BE ']; remaining name "OU = ACCOUNTS, DC = PORTAL, DC = COMPANY, DC = BE"


decision:

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);
        }

      

+3


source to share


1 answer


Start by performing basic sanity checks. For example, the data returned Environment.getEnvironment()

is correct (URL, port, user, password) and allows you to connect to the directory server. Also check for network problems and that you can actually access the server.

Try to narrow your search base a little, for example: "OU = Accounts, DC = PORTAL, DC = COMPANY, DC = BE" and see if any results are returned. Also check to see if the objects in the expected results have the attributes "cn", "givenname", "sn", "mail".



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

+1


source







All Articles