You are viewing a plain text version of this content. The canonical link for it is here.
Posted to api@directory.apache.org by Gabriel Albano <ga...@pilixo.com> on 2014/03/12 15:40:14 UTC

java.lang.ClassCastException when doing paginated search

Hi, I have followed the instructions from a thread reporting the same issue
(
http://mail-archives.apache.org/mod_mbox/directory-api/201312.mbox/%3CCAPmZGnw%2B7jMiACgxc14DMg-Ss-2JY%3Db-9j8Euxb8iJ%2BC1sb%3DXQ%40mail.gmail.com%3E)
but I am still getting the ClassCastException error.

Using 2.0.0-M15 server and 1.0.0.-M20 api.  This is the code:

        connection = new LdapNetworkConnection("localhost", 10389);
        connection.setTimeOut(0);
        connection.bind();


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;

        Integer p = 0;
        while (true) {
            EntryCursor cursor = null;

            try {
                SearchRequest searchRequest = new SearchRequestImpl();
                searchRequest.setBase(new Dn("ou=organizations,dc=test"));
                searchRequest.setFilter("(objectclass=device)");
                searchRequest.setScope(SearchScope.SUBTREE);
                searchRequest.addAttributes("*");
                searchRequest.addControl(pagedSearchControl);
                searchRequest.setSizeLimit(10000);

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

                while (cursor.next()) {
                    Entry result = cursor.get();
                    results.add(result);
                }
                p++;
                System.out.println("PAGE: " + p.toString());
                System.out.println("TOTAL of Results: " + results.size());

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


I have checked the system properties at run time and I see this:
apacheds.controls=org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsFactory

Still getting "Exception in thread "main" java.lang.ClassCastException:
org.apache.directory.api.ldap.codec.BasicControlDecorator cannot be cast to
org.apache.directory.api.ldap.model.message.controls.PagedResults

I appreciate any help.

Thanks,
Gabriel

Re: java.lang.ClassCastException when doing paginated search

Posted by Gabriel Albano <ga...@pilixo.com>.
That did it.  Switching to M21 next.

Thanks!


On Wed, Mar 12, 2014 at 10:43 AM, Kiran Ayyagari <ka...@apache.org>wrote:

> On Wed, Mar 12, 2014 at 8:10 PM, Gabriel Albano
> <ga...@pilixo.com>wrote:
>
> > Hi, I have followed the instructions from a thread reporting the same
> issue
> > (
> >
> >
> http://mail-archives.apache.org/mod_mbox/directory-api/201312.mbox/%3CCAPmZGnw%2B7jMiACgxc14DMg-Ss-2JY%3Db-9j8Euxb8iJ%2BC1sb%3DXQ%40mail.gmail.com%3E
> > )
> > but I am still getting the ClassCastException error.
> >
> > Using 2.0.0-M15 server and 1.0.0.-M20 api.  This is the code:
> >
> >         connection = new LdapNetworkConnection("localhost", 10389);
> >         connection.setTimeOut(0);
> >         connection.bind();
> >
> >
> >
> >
> System.setProperty(StandaloneLdapApiService.CONTROLS_LIST,PagedResultsFactory.class.getName());
> >
> > you need to set this before creating the LdapNetworkConnection
> or try using M21 (was just released) in this version you don't need to set
> this property
>
> >         PagedResults pagedSearchControl = new PagedResultsDecorator(
> >                 connection.getCodecService());
> >         pagedSearchControl.setSize(300);
> >
> >         // Loop over all the elements
> >         List<Entry> results = new ArrayList<Entry>();
> >         boolean hasUnwillingToPerform = false;
> >
> >         Integer p = 0;
> >         while (true) {
> >             EntryCursor cursor = null;
> >
> >             try {
> >                 SearchRequest searchRequest = new SearchRequestImpl();
> >                 searchRequest.setBase(new
> Dn("ou=organizations,dc=test"));
> >                 searchRequest.setFilter("(objectclass=device)");
> >                 searchRequest.setScope(SearchScope.SUBTREE);
> >                 searchRequest.addAttributes("*");
> >                 searchRequest.addControl(pagedSearchControl);
> >                 searchRequest.setSizeLimit(10000);
> >
> >                 cursor = new EntryCursorImpl(
> >                         connection.search(searchRequest));
> >
> >                 while (cursor.next()) {
> >                     Entry result = cursor.get();
> >                     results.add(result);
> >                 }
> >                 p++;
> >                 System.out.println("PAGE: " + p.toString());
> >                 System.out.println("TOTAL of Results: " +
> results.size());
> >
> >                 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();
> >
> >
> > I have checked the system properties at run time and I see this:
> >
> >
> apacheds.controls=org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsFactory
> >
> > Still getting "Exception in thread "main" java.lang.ClassCastException:
> > org.apache.directory.api.ldap.codec.BasicControlDecorator cannot be cast
> to
> > org.apache.directory.api.ldap.model.message.controls.PagedResults
> >
> > I appreciate any help.
> >
> > Thanks,
> > Gabriel
> >
>
>
>
> --
> Kiran Ayyagari
> http://keydap.com
>

Re: java.lang.ClassCastException when doing paginated search

Posted by Kiran Ayyagari <ka...@apache.org>.
On Wed, Mar 12, 2014 at 8:10 PM, Gabriel Albano
<ga...@pilixo.com>wrote:

> Hi, I have followed the instructions from a thread reporting the same issue
> (
>
> http://mail-archives.apache.org/mod_mbox/directory-api/201312.mbox/%3CCAPmZGnw%2B7jMiACgxc14DMg-Ss-2JY%3Db-9j8Euxb8iJ%2BC1sb%3DXQ%40mail.gmail.com%3E
> )
> but I am still getting the ClassCastException error.
>
> Using 2.0.0-M15 server and 1.0.0.-M20 api.  This is the code:
>
>         connection = new LdapNetworkConnection("localhost", 10389);
>         connection.setTimeOut(0);
>         connection.bind();
>
>
>
> System.setProperty(StandaloneLdapApiService.CONTROLS_LIST,PagedResultsFactory.class.getName());
>
> you need to set this before creating the LdapNetworkConnection
or try using M21 (was just released) in this version you don't need to set
this property

>         PagedResults pagedSearchControl = new PagedResultsDecorator(
>                 connection.getCodecService());
>         pagedSearchControl.setSize(300);
>
>         // Loop over all the elements
>         List<Entry> results = new ArrayList<Entry>();
>         boolean hasUnwillingToPerform = false;
>
>         Integer p = 0;
>         while (true) {
>             EntryCursor cursor = null;
>
>             try {
>                 SearchRequest searchRequest = new SearchRequestImpl();
>                 searchRequest.setBase(new Dn("ou=organizations,dc=test"));
>                 searchRequest.setFilter("(objectclass=device)");
>                 searchRequest.setScope(SearchScope.SUBTREE);
>                 searchRequest.addAttributes("*");
>                 searchRequest.addControl(pagedSearchControl);
>                 searchRequest.setSizeLimit(10000);
>
>                 cursor = new EntryCursorImpl(
>                         connection.search(searchRequest));
>
>                 while (cursor.next()) {
>                     Entry result = cursor.get();
>                     results.add(result);
>                 }
>                 p++;
>                 System.out.println("PAGE: " + p.toString());
>                 System.out.println("TOTAL of Results: " + results.size());
>
>                 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();
>
>
> I have checked the system properties at run time and I see this:
>
> apacheds.controls=org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsFactory
>
> Still getting "Exception in thread "main" java.lang.ClassCastException:
> org.apache.directory.api.ldap.codec.BasicControlDecorator cannot be cast to
> org.apache.directory.api.ldap.model.message.controls.PagedResults
>
> I appreciate any help.
>
> Thanks,
> Gabriel
>



-- 
Kiran Ayyagari
http://keydap.com