8

I've looking for information to perform paged searches using the Apache Directory API, but I haven't found any example or any information about how build a SearchRequest with the proper PagedResults control and then perform the search.

Any of you have some tip? Or knows where to find such information?

Or, maybe you should recommend me to use some other API, like unboundid sdk

Thanks in advance and kind regards.

Alexey
  • 9,197
  • 5
  • 64
  • 76
Ruben Romero
  • 611
  • 8
  • 15
  • You can us VLV control. https://access.redhat.com/documentation/en-us/red_hat_directory_server/11/html/administration_guide/creating_indexes-creating_vlv_indexes – gao.xiangyang May 29 '23 at 10:11

3 Answers3

9

Today I struggled with making the linked example from kayyagara work.

It has some problems:

  • a special System Variable is needed, to make this work at all
  • the bad cookie breaks the whole thing
  • some settings/lines of code were not needed

The following is working example:

        //Without this you get a class Cast Exception:
        //java.lang.ClassCastException: org.apache.directory.api.ldap.codec.BasicControlDecorator cannot be cast to org.apache.directory.api.ldap.model.message.controls.PagedResults
        System.setProperty(StandaloneLdapApiService.CONTROLS_LIST,
            PagedResultsFactory.class.getName());

        PagedResults pagedSearchControl = new PagedResultsDecorator(
                connection.getCodecService());
        pagedSearchControl.setSize(300);

        // Loop over all the elements
        List<Entry> results = new ArrayList<Entry>();
        boolean hasUnwillingToPerform = false;

        //inspired by http://markmail.org/message/43qjepg6shvfvqud
        while (true) {
            EntryCursor cursor = null;

            try {
                SearchRequest searchRequest = new SearchRequestImpl();
                searchRequest.setBase(new Dn(searchRoot));
                searchRequest.setFilter(searchFilter);
                searchRequest.setScope(SearchScope.SUBTREE);
                searchRequest.addAttributes("*");
                searchRequest.addControl(pagedSearchControl);

                cursor = new EntryCursorImpl(
                        connection.search(searchRequest));

                while (cursor.next()) {
                    Entry result = cursor.get();
                    results.add(result);
                }

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

            // check if this is over
            byte[] cookie = pagedSearchControl.getCookie();

            if (Strings.isEmpty(cookie)) {
                // If so, exit the loop
                break;
            }

            // Prepare the next iteration
            pagedSearchControl.setSize(300);
        }

        if (hasUnwillingToPerform) {
            throw new IllegalStateException("AD can't handle paging");
        }

        // Cleanup the session
        connection.unBind();
        connection.close();
oers
  • 18,436
  • 13
  • 66
  • 75
6

Take a look at this http://markmail.org/message/43qjepg6shvfvqud OTOH, it is always recommended to post ApacheDS related questions on the user mailing list for getting a quick reply, we may not always monitor SO

kayyagari
  • 1,882
  • 13
  • 10
1

This file contains a demonstration of the simple paged results control extension as described in RFC2696. To compile and run, the UnboundID LDAP SDK is required.

see also

Terry Gardner
  • 10,957
  • 2
  • 28
  • 38
  • Thanks a lot, but I would wait for a solution based on apache directory api. I know the feature is available, but I cannot find out how to use it. I've done lot of work with this api so I'll try to avoid rewrite the whole app. – Ruben Romero Aug 01 '13 at 06:21
  • That makes sense. I didn't realize you had a lot of code using apache written already. In any case, the links should prove useful on a general basis. – Terry Gardner Aug 01 '13 at 15:45