Example usage for javax.naming.ldap LdapContext search

List of usage examples for javax.naming.ldap LdapContext search

Introduction

In this page you can find the example usage for javax.naming.ldap LdapContext search.

Prototype

public NamingEnumeration<SearchResult> search(Name name, Attributes matchingAttributes,
        String[] attributesToReturn) throws NamingException;

Source Link

Document

Searches in a single context for objects that contain a specified set of attributes, and retrieves selected attributes.

Usage

From source file:org.olat.ldap.LDAPLoginManagerImpl.java

private void searchInLdap(final LdapVisitor visitor, final String filter, final String[] returningAttrs,
        final LdapContext ctx) {
    final SearchControls ctls = new SearchControls();
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctls.setReturningAttributes(returningAttrs);
    ctls.setCountLimit(0); // set no limits

    final boolean paging = isPagedResultControlSupported(ctx);
    for (final String ldapBase : LDAPLoginModule.getLdapBases()) {
        int counter = 0;
        try {/*from  w  w w  . j  a v  a2 s .c  o m*/
            if (paging) {
                byte[] cookie = null;
                ctx.setRequestControls(
                        new Control[] { new PagedResultsControl(PAGE_SIZE, Control.NONCRITICAL) });
                do {
                    final NamingEnumeration<SearchResult> enm = ctx.search(ldapBase, filter, ctls);
                    while (enm.hasMore()) {
                        visitor.visit(enm.next());
                    }
                    cookie = getCookie(ctx);
                } while (cookie != null);
            } else {
                final NamingEnumeration<SearchResult> enm = ctx.search(ldapBase, filter, ctls);
                while (enm.hasMore()) {
                    visitor.visit(enm.next());
                }
                counter++;
            }
        } catch (final SizeLimitExceededException e) {
            logError("SizeLimitExceededException after " + counter
                    + " records when getting all users from LDAP, reconfigure your LDAP server, hints: http://www.ldapbrowser.com/forum/viewtopic.php?t=14",
                    null);
        } catch (final NamingException e) {
            logError("NamingException when trying to fetch deleted users from LDAP using ldapBase::" + ldapBase
                    + " on row::" + counter, e);
        } catch (final Exception e) {
            logError("Exception when trying to fetch deleted users from LDAP using ldapBase::" + ldapBase
                    + " on row::" + counter, e);
        }
    }
}

From source file:org.olat.ldap.LDAPLoginManagerImpl.java

private boolean isPagedResultControlSupported(final LdapContext ctx) {
    try {/*from   w ww .j  a v a  2 s .  c  o  m*/
        final SearchControls ctl = new SearchControls();
        ctl.setReturningAttributes(new String[] { "supportedControl" });
        ctl.setSearchScope(SearchControls.OBJECT_SCOPE);

        /* search for the rootDSE object */
        final NamingEnumeration<SearchResult> results = ctx.search("", "(objectClass=*)", ctl);

        while (results.hasMore()) {
            final SearchResult entry = results.next();
            final NamingEnumeration<? extends Attribute> attrs = entry.getAttributes().getAll();
            while (attrs.hasMore()) {
                final Attribute attr = attrs.next();
                final NamingEnumeration<?> vals = attr.getAll();
                while (vals.hasMore()) {
                    final String value = (String) vals.next();
                    if (value.equals(PAGED_RESULT_CONTROL_OID)) {
                        return true;
                    }
                }
            }
        }
        return false;
    } catch (final Exception e) {
        logError("Exception when trying to know if the server support paged results.", e);
        return false;
    }
}

From source file:org.openiam.spml2.spi.example.ShellConnectorImpl.java

private NamingEnumeration search(ManagedSystemObjectMatch matchObj, LdapContext ctx, String searchValue,
        String[] attrAry) throws NamingException {
    SearchControls searchCtls = new SearchControls();

    searchCtls.setReturningAttributes(attrAry);

    searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    String searchFilter = matchObj.getSearchFilter();
    // replace the place holder in the search filter
    searchFilter = searchFilter.replace("?", searchValue);

    System.out.println("Search Filter=" + searchFilter);
    System.out.println("BaseDN=" + matchObj.getBaseDn());

    return ctx.search(matchObj.getSearchBaseDn(), searchFilter, searchCtls);

}

From source file:org.tolven.gatekeeper.bean.LdapBean.java

private List<TolvenPerson> findTolvenPerson(LdapContext ctx, String peopleBaseName, String principalLdapName,
        String realm, int maxResults, int timeLimit) {
    NamingEnumeration<SearchResult> namingEnum = null;
    SearchControls ctls = new SearchControls();
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctls.setCountLimit(maxResults);//from w ww. j ava2  s  .co m
    ctls.setTimeLimit(timeLimit);
    ArrayList<TolvenPerson> searchResults = new ArrayList<TolvenPerson>(10);
    try {
        namingEnum = ctx.search(peopleBaseName, principalLdapName, ctls);
        while (namingEnum.hasMore()) {
            SearchResult rslt = namingEnum.next();
            searchResults.add(new TolvenPerson(rslt));
        }
    } catch (GatekeeperSecurityException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new RuntimeException(
                "Could not search for TolvenPerson: " + principalLdapName + " in realm: " + realm + ": ", ex);
    }
    return searchResults;
}