directory-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel Lécharny <elecha...@gmail.com>
Subject Re: Simple code example of paged result?
Date Thu, 02 May 2013 15:58:26 GMT
Le 5/2/13 5:15 PM, Carlo.Accorsi@ibs-ag.com a écrit :
> Hi,
>
> For migration purposes, we sometimes need to loop through all entries in the directory.
> For example if we create a search cursor with ( uid=* ) this often leads to a timeout
because the result is tens of thousands.
>
> 10:39:24,614 ERROR root:279 - Cursor error
> org.apache.directory.api.ldap.model.exception.LdapException: TimeOut occurred
>        at org.apache.directory.ldap.client.api.SearchCursorImpl.next(SearchCursorImpl.java:140)
>
> Aside from setting the timeout to 2 mins, I'd like to use a paged result.
>
> Does anyone have a simple working example of a paged search result?  In the PagedSearchIT
there's a JNDI example.
> Was looking for something the in the Apache API if possible. Many thanks!
>
>
>
Here you are (test PagedSearchIT in server-integ) :

    /**
     * Do a test with a paged search and send a wrong cookie in the middle
     */
    @Test
    public void testPagedSearchWrongCookie() throws Exception
    {
        LdapNetworkConnection connection = new LdapNetworkConnection(
"localhost", getLdapServer().getPort() );
        connection.bind( "uid=admin,ou=system", "secret" );
   
        SearchControls controls = new SearchControls();
        controls.setCountLimit( ( int ) LdapServer.NO_SIZE_LIMIT );
        controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
        PagedResults pagedSearchControl = new PagedResultsDecorator(
codec );
        pagedSearchControl.setSize( 3 );
   
        // Loop over all the elements
        int loop = 0;
        List<Entry> results = new ArrayList<Entry>();
        boolean hasUnwillingToPerform = false;
   
        while ( true )
        {
            loop++;
   
            EntryCursor cursor = null;
   
            try
            {
                SearchRequest searchRequest = new SearchRequestImpl();
                searchRequest.setBase( new Dn( "ou=system" ) );
                searchRequest.setFilter( "(ObjectClass=*)" );
                searchRequest.setScope( SearchScope.SUBTREE );
                searchRequest.addAttributes( "*" );
                searchRequest.addControl( pagedSearchControl );
   
                cursor = new EntryCursorImpl( connection.search(
searchRequest ) );
   
                int i = 0;
   
                while ( cursor.next() )
                {
                    Entry result = cursor.get();
                    results.add( result );
                    ++i;
                }
   
                SearchResultDone result = cursor.getSearchResultDone();
                pagedSearchControl = ( PagedResults ) result.getControl(
PagedResults.OID );
   
                if ( result.getLdapResult().getResultCode() ==
ResultCodeEnum.UNWILLING_TO_PERFORM )
                {
                    hasUnwillingToPerform = true;
                    break;
                }
            }
            finally
            {
                if ( cursor != null )
                {
                    cursor.close();
                }
            }
   
            // Now read the next ones
            assertEquals( 0, pagedSearchControl.getSize() );
   
            // check if this is over
            byte[] cookie = pagedSearchControl.getCookie();
   
            if ( Strings.isEmpty( cookie ) )
            {
                // If so, exit the loop
                break;
            }
   
            // Prepare the next iteration, sending a bad cookie
            pagedSearchControl.setCookie( "test".getBytes( "UTF-8" ) );
            pagedSearchControl.setSize( 3 );
        }
   
        assertTrue( hasUnwillingToPerform );
   
        // Cleanup the session
        connection.unBind();
        connection.close();
    }


-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com 


Mime
View raw message