javax.naming.ldap.PagedResultsResponseControl Java Examples

The following examples show how to use javax.naming.ldap.PagedResultsResponseControl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JNDIProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 6 votes vote down vote up
private byte[] readResultResponsePageCookie( final Control[] controls )
{
    if ( controls != null )
    {
        for ( Control control : controls )
        {
            if ( control instanceof PagedResultsResponseControl )
            {
                final PagedResultsResponseControl prrc = ( PagedResultsResponseControl ) control;
                final byte[] cookie = prrc.getCookie();
                if ( cookie != null )
                {
                    return cookie;
                }
            }
        }
    }
    return null;
}
 
Example #2
Source File: LDAPLoginManagerImpl.java    From olat with Apache License 2.0 6 votes vote down vote up
private byte[] getCookie(final LdapContext ctx) throws NamingException, IOException {
    byte[] cookie = null;
    // Examine the paged results control response
    final Control[] controls = ctx.getResponseControls();
    if (controls != null) {
        for (int i = 0; i < controls.length; i++) {
            if (controls[i] instanceof PagedResultsResponseControl) {
                final PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
                cookie = prrc.getCookie();
            }
        }
    }
    // Re-activate paged results
    ctx.setRequestControls(new Control[] { new PagedResultsControl(PAGE_SIZE, cookie, Control.CRITICAL) });
    return cookie;
}
 
Example #3
Source File: LDAPLoginManagerImpl.java    From olat with Apache License 2.0 6 votes vote down vote up
private byte[] getCookie(final LdapContext ctx) throws NamingException, IOException {
    byte[] cookie = null;
    // Examine the paged results control response
    final Control[] controls = ctx.getResponseControls();
    if (controls != null) {
        for (int i = 0; i < controls.length; i++) {
            if (controls[i] instanceof PagedResultsResponseControl) {
                final PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
                cookie = prrc.getCookie();
            }
        }
    }
    // Re-activate paged results
    ctx.setRequestControls(new Control[] { new PagedResultsControl(PAGE_SIZE, cookie, Control.CRITICAL) });
    return cookie;
}
 
Example #4
Source File: LdapManager.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void reactivatePagedSearch() throws NamingException, IOException{
	  // Examine the paged results control response
       Control[] controls = ((LdapContext)ctx).getResponseControls();
       if (controls != null) {
       for (int i = 0; i < controls.length; i++) {
           if (controls[i] instanceof PagedResultsResponseControl) {
           PagedResultsResponseControl prrc =
                            (PagedResultsResponseControl)controls[i];
           // int total = prrc.getResultSize();
           this.cookie = prrc.getCookie();
           }
       }
       // Re-activate paged results
       ((LdapContext)ctx).setRequestControls(new Control[]{
       		new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });
       }
}
 
Example #5
Source File: PagedResultsDirContextProcessorTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testPostProcess() throws Exception {
    int resultSize = 50;
    byte pageSize = 8;

    byte[] value = new byte[1];
    value[0] = pageSize;
    byte[] cookie = encodeValue(resultSize, value);
    PagedResultsResponseControl control = new PagedResultsResponseControl(
            "dummy", true, cookie);

    when(ldapContextMock.getResponseControls()).thenReturn(new Control[] { control });
    tested.postProcess(ldapContextMock);

    PagedResultsCookie returnedCookie = tested.getCookie();
    assertThat(returnedCookie.getCookie()[0]).isEqualTo((byte)8);
    assertThat(tested.getPageSize()).isEqualTo(20);
    assertThat(tested.getResultSize()).isEqualTo(50);
}
 
Example #6
Source File: ReadOnlyLDAPUserStoreManager.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Parse the controls to navigate to next page.
 *
 * @param controls
 * @return
 */
private static byte[] parseControls(Control[] controls) {

    byte[] cookie = null;
    // Handle the paged results control response
    if (controls != null) {
        for (int i = 0; i < controls.length; i++) {
            if (controls[i] instanceof PagedResultsResponseControl) {
                PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
                cookie = prrc.getCookie();
            }
        }
    }
    return cookie;
}
 
Example #7
Source File: LdapService.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private byte[] getPagedResponseCookie(Control[] controls) {
if (controls != null) {
    for (Control control : controls) {
	if (control instanceof PagedResultsResponseControl) {
	    PagedResultsResponseControl prrc = (PagedResultsResponseControl) control;
	    return prrc.getCookie();
	}
    }
}
return null;
   }
 
Example #8
Source File: LdapUserDAO.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static byte[] parseControls(Control[] controls) throws NamingException {
    byte[] cookie = null;
    if (controls != null) {
        for (int i = 0; i < controls.length; i++) {
            if (controls[i] instanceof PagedResultsResponseControl) {
                PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
                cookie = prrc.getCookie();
            }
        }
    }
    return (cookie == null) ? new byte[0] : cookie;
}
 
Example #9
Source File: LDAPInitialDirContextFactoryImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public boolean hasNextPage(DirContext ctx, int pageSize)
{
    if (pageSize > 0)
    {
        try
        {
            LdapContext ldapContext = (LdapContext) ctx;
            Control[] controls = ldapContext.getResponseControls();

            // Retrieve the paged result cookie if there is one
            if (controls != null)
            {
                for (Control control : controls)
                {
                    if (control instanceof PagedResultsResponseControl)
                    {
                        byte[] cookie = ((PagedResultsResponseControl) control).getCookie();
                        if (cookie != null)
                        {
                            // Prepare for next page
                            ldapContext.setRequestControls(new Control[]
                            {
                                new PagedResultsControl(pageSize, cookie, Control.CRITICAL)
                            });
                            return true;
                        }
                    }
                }
            }
        }
        catch (NamingException nx)
        {
            throw new AuthenticationException("Unable to connect to LDAP Server; check LDAP configuration", nx);
        }
        catch (IOException e)
        {
            throw new AuthenticationException(
                    "Unable to encode LDAP v3 request controls; check LDAP configuration", e);
        }

    }
    return false;
}
 
Example #10
Source File: OpenLdapUserManagerImpl.java    From cosmic with Apache License 2.0 4 votes vote down vote up
@Override
public List<LdapUser> searchUsers(final String username, final LdapContext context) throws NamingException, IOException {

    final SearchControls searchControls = new SearchControls();

    searchControls.setSearchScope(_ldapConfiguration.getScope());
    searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());

    final String basedn = _ldapConfiguration.getBaseDn();
    if (StringUtils.isBlank(basedn)) {
        throw new IllegalArgumentException("ldap basedn is not configured");
    }
    byte[] cookie = null;
    final int pageSize = _ldapConfiguration.getLdapPageSize();
    context.setRequestControls(new Control[]{new PagedResultsControl(pageSize, Control.NONCRITICAL)});
    final List<LdapUser> users = new ArrayList<>();
    NamingEnumeration<SearchResult> results;
    do {
        results = context.search(basedn, generateSearchFilter(username), searchControls);
        while (results.hasMoreElements()) {
            final SearchResult result = results.nextElement();
            if (!isUserDisabled(result)) {
                users.add(createUser(result));
            }
        }
        final Control[] contextControls = context.getResponseControls();
        if (contextControls != null) {
            for (final Control control : contextControls) {
                if (control instanceof PagedResultsResponseControl) {
                    final PagedResultsResponseControl prrc = (PagedResultsResponseControl) control;
                    cookie = prrc.getCookie();
                }
            }
        } else {
            s_logger.info("No controls were sent from the ldap server");
        }
        context.setRequestControls(new Control[]{new PagedResultsControl(pageSize, cookie, Control.CRITICAL)});
    } while (cookie != null);

    return users;
}
 
Example #11
Source File: OpenLdapUserManagerImpl.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public List<LdapUser> searchUsers(final String username, final LdapContext context, Long domainId) throws NamingException, IOException {

    final SearchControls searchControls = new SearchControls();

    searchControls.setSearchScope(_ldapConfiguration.getScope());
    searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes(domainId));

    String basedn = _ldapConfiguration.getBaseDn(domainId);
    if (StringUtils.isBlank(basedn)) {
        throw new IllegalArgumentException(String.format("ldap basedn is not configured (for domain: %s)", domainId));
    }
    byte[] cookie = null;
    int pageSize = _ldapConfiguration.getLdapPageSize(domainId);
    context.setRequestControls(new Control[]{new PagedResultsControl(pageSize, Control.NONCRITICAL)});
    final List<LdapUser> users = new ArrayList<LdapUser>();
    NamingEnumeration<SearchResult> results;
    do {
        results = context.search(basedn, generateSearchFilter(username, domainId), searchControls);
        while (results.hasMoreElements()) {
            final SearchResult result = results.nextElement();
            if (!isUserDisabled(result)) {
                users.add(createUser(result, domainId));
            }
        }
        Control[] contextControls = context.getResponseControls();
        if (contextControls != null) {
            for (Control control : contextControls) {
                if (control instanceof PagedResultsResponseControl) {
                    PagedResultsResponseControl prrc = (PagedResultsResponseControl) control;
                    cookie = prrc.getCookie();
                }
            }
        } else {
            LOGGER.info("No controls were sent from the ldap server");
        }
        context.setRequestControls(new Control[] {new PagedResultsControl(pageSize, cookie, Control.CRITICAL)});
    } while (cookie != null);

    return users;
}
 
Example #12
Source File: LDAPOperationManager.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public List<SearchResult> searchPaginated(final String baseDN, final String filter, final LDAPQuery identityQuery) throws NamingException {
    final List<SearchResult> result = new ArrayList<SearchResult>();
    final SearchControls cons = getSearchControls(identityQuery.getReturningLdapAttributes(), identityQuery.getSearchScope());

    // Very 1st page. Pagination context is not yet present
    if (identityQuery.getPaginationContext() == null) {
        identityQuery.initPagination();
    }

    try {
        return execute(new LdapOperation<List<SearchResult>>() {

            @Override
            public List<SearchResult> execute(LdapContext context) throws NamingException {
                try {
                    byte[] cookie = identityQuery.getPaginationContext().getCookie();
                    PagedResultsControl pagedControls = new PagedResultsControl(identityQuery.getLimit(), cookie, Control.CRITICAL);
                    context.setRequestControls(new Control[] { pagedControls });

                    NamingEnumeration<SearchResult> search = context.search(new LdapName(baseDN), filter, cons);

                    while (search.hasMoreElements()) {
                        result.add(search.nextElement());
                    }

                    search.close();

                    Control[] responseControls = context.getResponseControls();
                    if (responseControls != null) {
                        for (Control respControl : responseControls) {
                            if (respControl instanceof PagedResultsResponseControl) {
                                PagedResultsResponseControl prrc = (PagedResultsResponseControl)respControl;
                                cookie = prrc.getCookie();
                                identityQuery.getPaginationContext().setCookie(cookie);
                            }
                        }
                    }

                    return result;
                } catch (IOException ioe) {
                    logger.errorf(ioe, "Could not query server with paginated query using DN [%s], filter [%s]", baseDN, filter);
                    throw new NamingException(ioe.getMessage());
                }
            }


            @Override
            public String toString() {
                return new StringBuilder("LdapOperation: searchPaginated\n")
                        .append(" baseDn: ").append(baseDN).append("\n")
                        .append(" filter: ").append(filter).append("\n")
                        .append(" searchScope: ").append(identityQuery.getSearchScope()).append("\n")
                        .append(" returningAttrs: ").append(identityQuery.getReturningLdapAttributes()).append("\n")
                        .append(" limit: ").append(identityQuery.getLimit()).append("\n")
                        .append(" resultSize: ").append(result.size())
                        .toString();
            }

        }, identityQuery.getPaginationContext().getLdapContext(), null);
    } catch (NamingException e) {
        logger.errorf(e, "Could not query server using DN [%s] and filter [%s]", baseDN, filter);
        throw e;
    }
}