All,

So far I've had a very good experience working with the LDAP API.  I have used 
it as part of a service-oriented application that does passes data between an 
RDB and Active Directory (via its LDAP interface).

I have just begun attempting to utilize the Paging logic, but am running into a 
problem.  It seems that I am able to get a "Paged" result just fine.  When the 
result is only 1 page long, there is no issue.  However, I'm obviously missing 
the mechanism by which the Page is advanced to the next collection of AD 
results.  Thus, when there are multiple pages, I just keep processing the first 
page over, and over...

I haven't been able to find much information anywhere on the internet.  This SO 
post seems reasonably close: 
https://stackoverflow.com/questions/17963975/apache-directory-ldap-paged-searches
  ...but fails to advance the page.  What follows is a somewhat abbreviated 
version of what I've got--can someone tell me what I'm missing??

        // form up the search request
        SearchRequest searchRequest = new SearchRequestImpl();
        ...

        // set the interface-level search parameters for the (initial) search
        PagedResults pagedResultsDecorator = new 
PagedResultsDecorator(ldc.getCodecService());
        pagedResultsDecorator.setSize(REQUESTED_PAGE_SIZE);
        searchRequest.addControl(pagedResultsDecorator);

        SearchCursor searchCursor = null;
        try {
            int pageNum = 0;
            // set the interface-level search parameters for the initial search
            SearchRequest searchRequest = createSearchRequest(ldc, 
filterString);
            while (true) {
                // Search for the next page of results
                searchCursor = ldc.search(searchRequest);

                pageNum++;
                LOG.debug("Now processing page [{}] of AD search results", 
pageNum);
                // pull all results from the current batch of results that the 
cursor points to
                while (searchCursor.next()) {
                    Response response = searchCursor.get();
                    if (response instanceof SearchResultEntry) {
                        Entry resultEntry = ((SearchResultEntry) 
response).getEntry();
                        entriesQ.put(resultEntry);
                    } else {
                        LOG.trace("element of response is NOT instanceof 
SearchResultEntry, it is [{}]", response.getClass());
                    }
                }

                // IF PAGING is supported ...
                if (ldc.isControlSupported(PagedResults.OID)) {
                    SearchResultDone searchResultDoneObject = 
searchCursor.getSearchResultDone();
                    // AND IF we received a valid search completion object in 
the results
                    if (null != searchResultDoneObject) {
                        ResultCodeEnum resultCode = 
searchResultDoneObject.getLdapResult().getResultCode();

                        // IF the search was SUCCESSFUL...
                        if (ResultCodeEnum.SUCCESS == resultCode) {
                            PagedResults pagedResults = (PagedResults) 
searchResultDoneObject.getControl(PagedResults.OID);

                            // if we've just processed the last PAGE of data, 
then exit the loop
                            // FIXME: hackish/non-obvious, but not sure if 
better way exists...
                            byte[] cookie = pagedResults.getCookie();
                            if ((null == cookie) || (0 == 
pagedResults.getCookie().length)) {
                                LOG.trace("Final PAGE of data processed");
                                break;
                            } else {
                                // otherwise, re-set the decorator for next 
page of results
                                //
                                //    ASSUME I SHOULD BE SETTING SOMETHING HERE 
. . .
                                //
                                pagedResults.setSize(REQUESTED_PAGE_SIZE);
                                pagedResults.setCookie(cookie);   
<-------------CAN/SHOULD THE COOKIE BE MANIPULATED?
                            }

                            // sleep to meter results, moderate memory 
utilization
                            try {
                                Thread.sleep(2000L);
                            } catch (InterruptedException ie) {
                                LOG.warn("Failed to pause between pages of AD 
records!", ie);
                            }
                        }

                        // ELSE IF the server curtailed our search results due 
to some restriction, throw an exception
                        else if (ResultCodeEnum.UNWILLING_TO_PERFORM == 
resultCode) {
                            // error handling...
                        }

                        // OTHERWISE - unknown error
                        else {
                            // error handling...
                        }

                    }
                    // The search failed
                    else {
                            // error handling...
                    }
                }

                // ELSE - we are processing a NON-PAGED result set
                else {
                    LOG.error("We should never get here, as we are requesting 
PAGED results!");
                }
            }

        } catch ( various exceptions) {
            // error handling...
        } finally {
            // close the cursor & ensure the connection has been closed
        }










Thanks,
Hans

Reply via email to