Example usage for javax.naming.directory SearchControls setTimeLimit

List of usage examples for javax.naming.directory SearchControls setTimeLimit

Introduction

In this page you can find the example usage for javax.naming.directory SearchControls setTimeLimit.

Prototype

public void setTimeLimit(int ms) 

Source Link

Document

Sets the time limit of these SearchControls in milliseconds.

Usage

From source file:SearchTimeLimit.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//from  ww w. ja va2 s. c o m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Set search controls to limit count to 'timeout'
        SearchControls ctls = new SearchControls();
        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        ctls.setTimeLimit(timeout); //

        // Search for objects with those matching attributes
        NamingEnumeration answer = ctx.search("", "(objectclass=*)", ctls);

        // Print the answer
        printSearchEnumeration(answer);

        // Close the context when we're done
        ctx.close();
    } catch (TimeLimitExceededException e) {
        System.out.println("time limit exceeded");
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:org.jasig.portlet.contacts.adapters.impl.ldap.LdapSearchAdapter.java

/**
 * Construct a new search controls object for our search
 *//*from   w  w  w  .  ja  v a2  s  . c  o  m*/
protected SearchControls getSearchControls() {
    SearchControls searchControls = new SearchControls();
    searchControls.setTimeLimit(timeLimit);
    searchControls.setCountLimit(countLimit);
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    return searchControls;
}

From source file:net.identio.server.service.authentication.ldap.LdapConnectionFactory.java

@Override
public boolean validateObject(PooledObject<InitialLdapContext> p) {

    LOG.debug("Validating connection to LDAP directory {}", ldapAuthMethod.getName());

    SearchControls controls = new SearchControls();
    controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    controls.setCountLimit(1);/* w  ww .j ava 2s .  co  m*/
    controls.setTimeLimit(500);

    try {
        p.getObject().search("", ldapAuthMethod.getPoolConfig().getTestRequestFilter(), controls);
    } catch (NamingException e) {
        LOG.error("Validation of connection to LDAP directory {} failed", ldapAuthMethod.getName());
        return false;
    }

    return true;
}

From source file:org.jasig.schedassist.impl.ldap.LDAPCalendarAccountDaoImpl.java

/**
 * /*from w  ww  .  j  a v  a 2  s.  com*/
 * @param searchFilter
 * @return
 */
@SuppressWarnings("unchecked")
protected List<ICalendarAccount> executeSearchReturnList(final Filter searchFilter) {
    log.debug("executing search filter: " + searchFilter);

    SearchControls sc = new SearchControls();
    sc.setCountLimit(searchResultsLimit);
    sc.setTimeLimit(searchTimeLimit);
    sc.setSearchScope(SearchControls.SUBTREE_SCOPE);

    List<ICalendarAccount> results = Collections.emptyList();
    try {
        results = ldapTemplate.search(baseDn, searchFilter.toString(), sc,
                new DefaultContextMapperImpl(ldapAttributesKey));
    } catch (SizeLimitExceededException e) {
        log.debug("search filter exceeded results size limit(" + searchResultsLimit + "): " + searchFilter);
    } catch (TimeLimitExceededException e) {
        log.warn("search filter exceeded time limit (" + searchTimeLimit + " milliseconds): " + searchFilter);
    }
    return results;
}

From source file:org.jasig.schedassist.impl.oraclecalendar.OracleLdapCalendarAccountDaoImpl.java

/**
 * //from w ww.  j a va  2  s .  co m
 * @param searchFilter
 * @return
 */
@SuppressWarnings("unchecked")
protected List<ICalendarAccount> executeSearchReturnList(final Filter searchFilter) {
    LOG.debug("searchFilter: " + searchFilter);
    SearchControls searchControls = new SearchControls();
    searchControls.setCountLimit(searchResultsLimit);
    searchControls.setTimeLimit(searchTimeLimit);
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    List<ICalendarAccount> results = Collections.emptyList();
    try {
        results = ldapTemplate.search(baseDn, searchFilter.toString(), searchControls,
                new OracleCalendarUserAccountAttributesMapper(this.oracleGUIDSource));
        if (LOG.isDebugEnabled()) {
            LOG.debug("search " + searchFilter + " returned " + results.size() + " results");
        }
        Collections.sort(results, new AccountComparator());
    } catch (SizeLimitExceededException e) {
        LOG.debug("search filter exceeded size limit (" + searchResultsLimit + "): " + searchFilter);
    } catch (TimeLimitExceededException e) {
        LOG.debug("search filter exceeded time limit(" + searchTimeLimit + " milliseconds): " + searchFilter);
    }
    return results;
}

From source file:org.jasig.schedassist.impl.oraclecalendar.OracleLdapCalendarResourceAccountDaoImpl.java

/**
 * /*from www  .j  a  v a  2s  . c  o  m*/
 * @param searchFilter
 * @param owner
 * @return
 */
@SuppressWarnings("unchecked")
protected List<IDelegateCalendarAccount> executeSearchReturnList(final Filter searchFilter,
        final ICalendarAccount owner) {
    SearchControls searchControls = new SearchControls();
    searchControls.setCountLimit(searchResultsLimit);
    searchControls.setTimeLimit(searchTimeLimit);
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    List<IDelegateCalendarAccount> results = Collections.emptyList();
    try {
        results = ldapTemplate.search(baseDn, searchFilter.toString(), searchControls,
                new OracleCalendarResourceAccountAttributesMapper(this.oracleGUIDSource, owner));
        if (LOG.isDebugEnabled()) {
            LOG.debug("search " + searchFilter + " returned " + results.size() + " results");
        }

        Collections.sort(results, new DelegateDisplayNameComparator());
    } catch (SizeLimitExceededException e) {
        LOG.debug("search filter exceeded size limit (" + searchResultsLimit + "): " + searchFilter);
    } catch (TimeLimitExceededException e) {
        LOG.debug("search filter exceeded time limit(" + searchTimeLimit + " milliseconds): " + searchFilter);
    }
    return results;
}

From source file:org.jasig.cas.adaptors.ldap.BindLdapAuthenticationHandler.java

private SearchControls getSearchControls() {
    final SearchControls constraints = new SearchControls();
    constraints.setSearchScope(this.scope);
    constraints.setReturningAttributes(new String[0]);
    constraints.setTimeLimit(this.timeout);
    constraints.setCountLimit(this.maxNumberResults);

    return constraints;
}

From source file:org.jasig.schedassist.impl.ldap.LDAPDelegateCalendarAccountDaoImpl.java

/**
 * // w w  w  .  j  a  va  2 s.  co  m
 * @param searchFilter
 * @param owner
 * @return
 */
@SuppressWarnings("unchecked")
protected List<IDelegateCalendarAccount> executeSearchReturnList(final Filter searchFilter,
        final ICalendarAccount owner) {
    SearchControls searchControls = new SearchControls();
    searchControls.setCountLimit(searchResultsLimit);
    searchControls.setTimeLimit(searchTimeLimit);
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    List<IDelegateCalendarAccount> results = Collections.emptyList();
    try {
        results = ldapTemplate.search(baseDn, searchFilter.toString(), searchControls,
                new DefaultDelegateAccountAttributesMapperImpl(ldapAttributesKey, owner));
        if (log.isDebugEnabled()) {
            log.debug("search " + searchFilter + " returned " + results.size() + " results");
        }

        if (isTreatOwnerAttributeAsDistinguishedName() && owner != null
                && owner instanceof HasDistinguishedName) {
            HasDistinguishedName ldapOwnerAccount = (HasDistinguishedName) owner;
            enforceDistinguishedNameMatch(results, ldapOwnerAccount);
        }
        Collections.sort(results, new DelegateDisplayNameComparator());
    } catch (SizeLimitExceededException e) {
        log.debug("search filter exceeded size limit (" + searchResultsLimit + "): " + searchFilter);
    } catch (TimeLimitExceededException e) {
        log.debug("search filter exceeded time limit(" + searchTimeLimit + " milliseconds): " + searchFilter);
    }
    return results;
}

From source file:org.jasig.cas.authentication.principal.AbstractLdapPersonDirectoryCredentialsToPrincipalResolver.java

protected final SearchControls getSearchControls() {
    final SearchControls constraints = new SearchControls();
    if (log.isDebugEnabled()) {
        log.debug("returning searchcontrols: scope=" + this.scope + "; search base=" + this.searchBase
                + "; attributes=" + Arrays.toString(this.attributeIds) + "; timeout=" + this.timeout);
    }/*from   w  w w. jav  a 2  s  .  c  o  m*/
    constraints.setSearchScope(this.scope);
    constraints.setReturningAttributes(this.attributeIds);
    constraints.setTimeLimit(this.timeout);
    constraints.setCountLimit(DEFAULT_MAX_NUMBER_OF_RESULTS);
    return constraints;
}

From source file:ldap.SearchUtility.java

/**
 * A utility method to get a conrols object.  May be redundant; the default new Controls() would probably
 * suffice./*from  www .ja  v  a  2s.  c  om*/
 * @return a new SearchControls object that can be modified and passed to a context.search method.
 */
private SearchControls getSearchControls() {
    SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
    constraints.setCountLimit(0);
    constraints.setTimeLimit(0);
    return constraints;
}