Example usage for javax.naming.directory SearchControls getTimeLimit

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

Introduction

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

Prototype

public int getTimeLimit() 

Source Link

Document

Retrieves the time limit of these SearchControls in milliseconds.

Usage

From source file:de.acosix.alfresco.mtsupport.repo.auth.ldap.EnhancedLDAPUserRegistry.java

/**
 * Invokes the given callback on each entry returned by the given query.
 *
 * @param callback/*from  w ww .ja v a  2 s. c o  m*/
 *            the callback
 * @param searchBase
 *            the base DN for the search
 * @param query
 *            the query
 * @param returningAttributes
 *            the attributes to include in search results
 * @throws AlfrescoRuntimeException
 */
protected void processQuery(final SearchCallback callback, final String searchBase, final String query,
        final String[] returningAttributes) {
    final SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    searchControls.setReturningAttributes(returningAttributes);

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(
                "Processing query {}\nSearch base: {}\n\rReturn result limit: {}\n\tDereflink: {}\n\rReturn named object: {}\n\tTime limit for search: {}\n\tAttributes to return: {} items\n\tAttributes: {}",
                query, searchBase, searchControls.getCountLimit(), searchControls.getDerefLinkFlag(),
                searchControls.getReturningObjFlag(), searchControls.getTimeLimit(),
                String.valueOf(returningAttributes.length), Arrays.toString(returningAttributes));
    }

    InitialDirContext ctx = null;
    NamingEnumeration<SearchResult> searchResults = null;
    SearchResult result = null;
    try {
        ctx = this.ldapInitialContextFactory.getDefaultIntialDirContext(this.queryBatchSize);
        do {
            searchResults = ctx.search(searchBase, query, searchControls);

            while (searchResults.hasMore()) {
                result = searchResults.next();
                callback.process(result);

                this.commonCloseSearchResult(result);
                result = null;
            }
        } while (this.ldapInitialContextFactory.hasNextPage(ctx, this.queryBatchSize));
    } catch (final NamingException e) {
        final Object[] params = { e.getLocalizedMessage() };
        throw new AlfrescoRuntimeException("synchronization.err.ldap.search", params, e);
    } catch (final ParseException e) {
        final Object[] params = { e.getLocalizedMessage() };
        throw new AlfrescoRuntimeException("synchronization.err.ldap.search", params, e);
    } finally {
        this.commonAfterQueryCleanup(searchResults, result, ctx);
    }
}

From source file:dk.magenta.ldap.LDAPMultiBaseUserRegistry.java

/**
 * Invokes the given callback on each entry returned by the given query.
 *
 * @param callback/* ww w  . j  a  v  a2 s  . c om*/
 *            the callback
 * @param searchBase
 *            the base DN for the search
 * @param query
 *            the query
 * @param returningAttributes
 *            the attributes to include in search results
 * @throws org.alfresco.error.AlfrescoRuntimeException
 */
private void processQuery(SearchCallback callback, String searchBase, String query,
        String[] returningAttributes) {
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    searchControls.setReturningAttributes(returningAttributes);
    if (LDAPMultiBaseUserRegistry.logger.isDebugEnabled()) {
        LDAPMultiBaseUserRegistry.logger.debug("Processing query");
        LDAPMultiBaseUserRegistry.logger.debug("Search base: " + searchBase);
        LDAPMultiBaseUserRegistry.logger.debug("    Return result limit: " + searchControls.getCountLimit());
        LDAPMultiBaseUserRegistry.logger.debug("    DerefLink: " + searchControls.getDerefLinkFlag());
        LDAPMultiBaseUserRegistry.logger
                .debug("    Return named object: " + searchControls.getReturningObjFlag());
        LDAPMultiBaseUserRegistry.logger.debug("    Time limit for search: " + searchControls.getTimeLimit());
        LDAPMultiBaseUserRegistry.logger
                .debug("    Attributes to return: " + returningAttributes.length + " items.");
        for (String ra : returningAttributes) {
            LDAPMultiBaseUserRegistry.logger.debug("        Attribute: " + ra);
        }
    }
    InitialDirContext ctx = null;
    NamingEnumeration<SearchResult> searchResults = null;
    SearchResult result = null;
    try {
        ctx = this.ldapInitialContextFactory.getDefaultIntialDirContext(this.queryBatchSize);
        do {
            searchResults = ctx.search(searchBase, query, searchControls);

            while (searchResults.hasMore()) {
                result = searchResults.next();
                callback.process(result);

                // Close the contexts, see ALF-20682
                Context resultCtx = (Context) result.getObject();
                if (resultCtx != null) {
                    resultCtx.close();
                }
                result = null;
            }
        } while (this.ldapInitialContextFactory.hasNextPage(ctx, this.queryBatchSize));
    } catch (NamingException e) {
        Object[] params = { e.getLocalizedMessage() };
        throw new AlfrescoRuntimeException("synchronization.err.ldap.search", params, e);
    } catch (ParseException e) {
        Object[] params = { e.getLocalizedMessage() };
        throw new AlfrescoRuntimeException("synchronization.err.ldap.search", params, e);
    } finally {
        if (result != null) {
            try {
                Context resultCtx = (Context) result.getObject();
                if (resultCtx != null) {
                    resultCtx.close();
                }
            } catch (Exception e) {
                logger.debug("error when closing result block context", e);
            }
        }
        if (searchResults != null) {
            try {
                searchResults.close();
            } catch (Exception e) {
                logger.debug("error when closing searchResults context", e);
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
            }
        }
    }
}

From source file:org.alfresco.repo.security.sync.ldap.LDAPUserRegistry.java

/**
 * Invokes the given callback on each entry returned by the given query.
 * //from w  w  w.  ja  va2 s.  c  o m
 * @param callback
 *            the callback
 * @param searchBase
 *            the base DN for the search
 * @param query
 *            the query
 * @param returningAttributes
 *            the attributes to include in search results
 * @throws AlfrescoRuntimeException           
 */
private void processQuery(SearchCallback callback, String searchBase, String query,
        String[] returningAttributes) {
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    searchControls.setReturningAttributes(returningAttributes);
    if (LDAPUserRegistry.logger.isDebugEnabled()) {
        LDAPUserRegistry.logger.debug("Processing query");
        LDAPUserRegistry.logger.debug("Search base: " + searchBase);
        LDAPUserRegistry.logger.debug("    Return result limit: " + searchControls.getCountLimit());
        LDAPUserRegistry.logger.debug("    DerefLink: " + searchControls.getDerefLinkFlag());
        LDAPUserRegistry.logger.debug("    Return named object: " + searchControls.getReturningObjFlag());
        LDAPUserRegistry.logger.debug("    Time limit for search: " + searchControls.getTimeLimit());
        LDAPUserRegistry.logger.debug("    Attributes to return: " + returningAttributes.length + " items.");
        for (String ra : returningAttributes) {
            LDAPUserRegistry.logger.debug("        Attribute: " + ra);
        }
    }
    InitialDirContext ctx = null;
    NamingEnumeration<SearchResult> searchResults = null;
    SearchResult result = null;
    try {
        ctx = this.ldapInitialContextFactory.getDefaultIntialDirContext(this.queryBatchSize);
        do {
            searchResults = ctx.search(searchBase, query, searchControls);

            while (searchResults.hasMore()) {
                result = searchResults.next();
                callback.process(result);

                // Close the contexts, see ALF-20682
                Context resultCtx = (Context) result.getObject();
                if (resultCtx != null) {
                    resultCtx.close();
                }
                result = null;
            }
        } while (this.ldapInitialContextFactory.hasNextPage(ctx, this.queryBatchSize));
    } catch (NamingException e) {
        Object[] params = { e.getLocalizedMessage() };
        throw new AlfrescoRuntimeException("synchronization.err.ldap.search", params, e);
    } catch (ParseException e) {
        Object[] params = { e.getLocalizedMessage() };
        throw new AlfrescoRuntimeException("synchronization.err.ldap.search", params, e);
    } finally {
        if (result != null) {
            try {
                Context resultCtx = (Context) result.getObject();
                if (resultCtx != null) {
                    resultCtx.close();
                }
            } catch (Exception e) {
                logger.debug("error when closing result block context", e);
            }
        }
        if (searchResults != null) {
            try {
                searchResults.close();
            } catch (Exception e) {
                logger.debug("error when closing searchResults context", e);
            }
            searchResults = null;
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
            }
        }
        try {
            callback.close();
        } catch (NamingException e) {
        }
    }
}

From source file:org.apache.directory.studio.connection.core.io.api.DirectoryApiConnectionWrapper.java

/**
 * {@inheritDoc}/*from  w  w w  .  j a v a  2 s.  c o m*/
 */
public StudioNamingEnumeration search(final String searchBase, final String filter,
        final SearchControls searchControls, final AliasDereferencingMethod aliasesDereferencingMethod,
        final ReferralHandlingMethod referralsHandlingMethod, final Control[] controls,
        final StudioProgressMonitor monitor, final ReferralsInfo referralsInfo) {
    final long requestNum = searchRequestNum++;

    InnerRunnable runnable = new InnerRunnable() {
        public void run() {
            try {
                // Preparing the search request
                SearchRequest request = new SearchRequestImpl();
                request.setBase(new Dn(searchBase));
                ExprNode node = FilterParser.parse(filter, true);
                request.setFilter(node);
                request.setScope(convertSearchScope(searchControls));
                if (searchControls.getReturningAttributes() != null) {
                    request.addAttributes(searchControls.getReturningAttributes());
                }
                request.addAllControls(convertControls(controls));
                request.setSizeLimit(searchControls.getCountLimit());
                request.setTimeLimit(searchControls.getTimeLimit());
                request.setDerefAliases(convertAliasDerefMode(aliasesDereferencingMethod));

                // Performing the search operation
                SearchCursor cursor = ldapConnection.search(request);

                // Returning the result of the search
                namingEnumeration = new CursorStudioNamingEnumeration(connection, cursor, searchBase, filter,
                        searchControls, aliasesDereferencingMethod, referralsHandlingMethod, controls,
                        requestNum, monitor, referralsInfo);
            } catch (Exception e) {
                exception = e;
            }

            NamingException ne = null;
            if (exception != null) {
                ne = new NamingException(exception.getMessage());
            }

            for (IJndiLogger logger : getJndiLoggers()) {
                if (namingEnumeration != null) {
                    logger.logSearchRequest(connection, searchBase, filter, searchControls,
                            aliasesDereferencingMethod, controls, requestNum, ne);
                } else {
                    logger.logSearchRequest(connection, searchBase, filter, searchControls,
                            aliasesDereferencingMethod, controls, requestNum, ne);
                    logger.logSearchResultDone(connection, 0, requestNum, ne);
                }
            }
        }
    };

    try {
        checkConnectionAndRunAndMonitor(runnable, monitor);
    } catch (Exception e) {
        monitor.reportError(e);
        return null;
    }

    if (runnable.isCanceled()) {
        monitor.setCanceled(true);
    }
    if (runnable.getException() != null) {
        monitor.reportError(runnable.getException());
        return null;
    } else {
        return runnable.getResult();
    }
}

From source file:org.apache.directory.studio.connection.core.io.jndi.LdifSearchLogger.java

/**
 * {@inheritDoc}/* w w  w.j a  v a  2s  .com*/
 */
public void logSearchRequest(Connection connection, String searchBase, String filter,
        SearchControls searchControls, AliasDereferencingMethod aliasesDereferencingMethod, Control[] controls,
        long requestNum, NamingException ex) {
    if (!isSearchRequestLogEnabled()) {
        return;
    }

    String scopeAsString = searchControls.getSearchScope() == SearchControls.SUBTREE_SCOPE ? "wholeSubtree (2)" //$NON-NLS-1$
            : searchControls.getSearchScope() == SearchControls.ONELEVEL_SCOPE ? "singleLevel (1)" //$NON-NLS-1$
                    : "baseObject (0)"; //$NON-NLS-1$
    String attributesAsString = searchControls.getReturningAttributes() == null ? "*" //$NON-NLS-1$
            : searchControls.getReturningAttributes().length == 0 ? "1.1" //$NON-NLS-1$
                            : StringUtils.join(searchControls.getReturningAttributes(), " ");
    String aliasAsString = aliasesDereferencingMethod == AliasDereferencingMethod.ALWAYS ? "derefAlways (3)" //$NON-NLS-1$
            : aliasesDereferencingMethod == AliasDereferencingMethod.FINDING ? "derefFindingBaseObj (2)" //$NON-NLS-1$
                    : aliasesDereferencingMethod == AliasDereferencingMethod.SEARCH ? "derefInSearching (1)" //$NON-NLS-1$
                            : "neverDerefAliases (0)"; //$NON-NLS-1$

    // build LDAP URL
    LdapUrl url = Utils.getLdapURL(connection, searchBase, searchControls.getSearchScope(), filter,
            searchControls.getReturningAttributes());

    // build command line
    String cmdLine = Utils.getLdapSearchCommandLine(connection, searchBase, searchControls.getSearchScope(),
            aliasesDereferencingMethod, searchControls.getCountLimit(), searchControls.getTimeLimit(), filter,
            searchControls.getReturningAttributes());

    // build 
    Collection<LdifLineBase> lines = new ArrayList<LdifLineBase>();
    lines.add(LdifCommentLine.create("# LDAP URL     : " + url.toString())); //$NON-NLS-1$
    lines.add(LdifCommentLine.create("# command line : " + cmdLine.toString())); //$NON-NLS-1$
    lines.add(LdifCommentLine.create("# baseObject   : " + searchBase)); //$NON-NLS-1$
    lines.add(LdifCommentLine.create("# scope        : " + scopeAsString)); //$NON-NLS-1$
    lines.add(LdifCommentLine.create("# derefAliases : " + aliasAsString)); //$NON-NLS-1$
    lines.add(LdifCommentLine.create("# sizeLimit    : " + searchControls.getCountLimit())); //$NON-NLS-1$
    lines.add(LdifCommentLine.create("# timeLimit    : " + searchControls.getTimeLimit())); //$NON-NLS-1$
    lines.add(LdifCommentLine.create("# typesOnly    : " + "False")); //$NON-NLS-1$ //$NON-NLS-2$
    lines.add(LdifCommentLine.create("# filter       : " + filter)); //$NON-NLS-1$
    lines.add(LdifCommentLine.create("# attributes   : " + attributesAsString)); //$NON-NLS-1$
    if (controls != null) {
        for (Control control : controls) {
            lines.add(LdifCommentLine.create("# control      : " + control.getID())); //$NON-NLS-1$
        }
    }
    lines.add(LdifSepLine.create());

    String formattedString = ""; //$NON-NLS-1$
    for (LdifLineBase line : lines) {
        formattedString += line.toFormattedString(LdifFormatParameters.DEFAULT);
    }

    log(formattedString, "SEARCH REQUEST (" + requestNum + ")", ex, connection); //$NON-NLS-1$ //$NON-NLS-2$
}

From source file:org.cloudfoundry.identity.uaa.ldap.extension.SpringSecurityLdapTemplate.java

/**
 * We need to make sure the search controls has the return object flag set to true, in order for
 * the search to return DirContextAdapter instances.
 * @param originalControls// ww  w  .  ja v  a2  s. c  o  m
 * @return
 */
private static SearchControls buildControls(SearchControls originalControls) {
    return new SearchControls(originalControls.getSearchScope(), originalControls.getCountLimit(),
            originalControls.getTimeLimit(), originalControls.getReturningAttributes(), RETURN_OBJECT,
            originalControls.getDerefLinkFlag());
}