javax.naming.ldap.PagedResultsControl Java Examples

The following examples show how to use javax.naming.ldap.PagedResultsControl. 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 boolean supportsSearchResultPaging()
        throws ChaiUnavailableException, ChaiOperationException
{
    final String enableSettingStr = this.getChaiConfiguration().getSetting( ChaiSetting.LDAP_SEARCH_PAGING_ENABLE );
    if ( "auto".equalsIgnoreCase( enableSettingStr ) )
    {
        if ( cachedPagingEnableSupport == null )
        {
            final ChaiEntry rootDse = ChaiUtility.getRootDSE( this );
            final Set<String> supportedControls = rootDse.readMultiStringAttribute( "supportedControl" );
            cachedPagingEnableSupport = supportedControls.contains( PagedResultsControl.OID );
        }
        return cachedPagingEnableSupport;
    }
    return Boolean.parseBoolean( enableSettingStr );
}
 
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 5 votes vote down vote up
@Test
public void testCreateRequestControl_CookieSet() throws Exception {
    PagedResultsCookie cookie = new PagedResultsCookie(new byte[0]);
    PagedResultsDirContextProcessor tested = new PagedResultsDirContextProcessor(20,
            cookie);

    PagedResultsControl control = (PagedResultsControl) tested
            .createRequestControl();
    assertThat(control).isNotNull();
}
 
Example #6
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 #7
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 #8
Source File: LdapManager.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void initPagedSearch() throws NamingException, IOException{
	this.cookie = null;
       ((LdapContext)ctx).setRequestControls(new Control[]{
       		new PagedResultsControl(pageSize, Control.NONCRITICAL) });
}
 
Example #9
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 #10
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;
    }
}
 
Example #11
Source File: PagedResultsDirContextProcessorTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateRequestControl() throws Exception {
    PagedResultsControl control = (PagedResultsControl) tested
            .createRequestControl();
    assertThat(control).isNotNull();
}