2.3 - Searching (...)

Searching is the most important operation in LDAP. It has to be fast, very fast. On the other hand, as the server does not a lot of processing while looking for entries, the client has to provide many information in order to get some accurate results.

The idea is to define a search API which is easy to use in the simplest cases, but provides all the necessary bolts if you need to send complex search requests.

This part of the API is very likely to change in the next milestone, to provide an easier way to get the results in the simple cases.

Let's first look at a simple search. What we basically need to process a search is a starting point in the tree, a filter, a scope. Here is an example :

@Test
public void testSimpleSearch() throws Exception
{
    SearchCursor cursor = connection.search( "ou=system", "(objectclass=*)", SearchScope.ONELEVEL );

    while ( cursor.next() )
    {
        Response response = cursor.get();
        assertNotNull( response );
        assertTrue( response instanceof SearchResultEntry );
        System.out.println( ((SearchResultEntry)response).getEntry() );
    }

    SearchResultDone done = cursor.getSearchResultDone();

    assertNotNull( done );
    assertEquals( ResultCodeEnum.SUCCESS, done.getLdapResult().getResultCode() );

    cursor.close();
}

In this example, the connection has been previously created. We just search for all the entries starting at ou=system and their children, which have an ObjectClass attribute (all the entries have such an attribute, so we should get back all the entries). The scope (ONELEVEL) says we just search one level under the starting base.

We get back a cursor, which can be walked forward. Every call to the get() method will return a response, which will be either a SearchResultEntry, a SearchResultReference or an IntermediateResponse.