Java Code Examples for javax.naming.directory.SearchControls#setTimeLimit()

The following examples show how to use javax.naming.directory.SearchControls#setTimeLimit() . 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: LdapTemplate.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
private SearchControls searchControlsForQuery(LdapQuery query, boolean returnObjFlag) {
    SearchControls searchControls = getDefaultSearchControls(
            defaultSearchScope,
            returnObjFlag,
            query.attributes());

    if(query.searchScope() != null) {
        searchControls.setSearchScope(query.searchScope().getId());
    }

    if(query.countLimit() != null) {
        searchControls.setCountLimit(query.countLimit());
    }

    if(query.timeLimit() != null) {
        searchControls.setTimeLimit(query.timeLimit());
    }
    return searchControls;
}
 
Example 2
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void verifyThatDefaultSearchControlParametersAreAutomaticallyAppliedInSearch() throws Exception {
    tested.setDefaultSearchScope(SearchControls.ONELEVEL_SCOPE);
    tested.setDefaultCountLimit(5000);
    tested.setDefaultTimeLimit(500);

    expectGetReadOnlyContext();

    SearchControls controls = new SearchControls();
    controls.setReturningObjFlag(false);
    controls.setCountLimit(5000);
    controls.setTimeLimit(500);
    controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);

    BasicAttributes expectedAttributes = new BasicAttributes();
    SearchResult searchResult = new SearchResult("", null, expectedAttributes);

    singleSearchResult(controls, searchResult);

    Object expectedResult = new Object();
    when(attributesMapperMock.mapFromAttributes(expectedAttributes)).thenReturn(expectedResult);

    List list = tested.search(nameMock, "(ou=somevalue)", attributesMapperMock);

    verify(namingEnumerationMock).close();
    verify(dirContextMock).close();

    assertThat(list).isNotNull();
    assertThat(list).hasSize(1);
    assertThat(list.get(0)).isSameAs(expectedResult);
}
 
Example 3
Source File: SearchFirstActiveDirectoryRealm.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
/**
 * Finds a distinguished name(DN) of a user by querying the active directory LDAP context for the
 * specified username.
 *
 * @return the DN of the user, or {@code null} if there's no such user
 */
@Nullable
protected String findUserDn(LdapContextFactory ldapContextFactory, String username) throws NamingException {
    LdapContext ctx = null;
    try {
        // Binds using the system username and password.
        ctx = ldapContextFactory.getSystemLdapContext();

        final SearchControls ctrl = new SearchControls();
        ctrl.setCountLimit(1);
        ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
        ctrl.setTimeLimit(searchTimeoutMillis);

        final String filter =
                searchFilter != null ? USERNAME_PLACEHOLDER.matcher(searchFilter)
                                                           .replaceAll(username)
                                     : username;
        final NamingEnumeration<SearchResult> result = ctx.search(searchBase, filter, ctrl);
        try {
            if (!result.hasMore()) {
                return null;
            }
            return result.next().getNameInNamespace();
        } finally {
            result.close();
        }
    } finally {
        LdapUtils.closeContext(ctx);
    }
}
 
Example 4
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private SearchControls getDefaultSearchControls(int searchScope, boolean returningObjFlag, String[] attrs) {
	SearchControls controls = new SearchControls();
	controls.setSearchScope(searchScope);
       controls.setTimeLimit(defaultTimeLimit);
       controls.setCountLimit(defaultCountLimit);
	controls.setReturningObjFlag(returningObjFlag);
	controls.setReturningAttributes(attrs);
	return controls;
}
 
Example 5
Source File: LdapGroupSearcherFactory.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static SearchControls createSearchControl(final boolean recursive, final String[] attributes) {
    if (SECURITY_LOGGER.isTraceEnabled()) {
        SECURITY_LOGGER.tracef("createSearchControl recursive=%b,  attributes=%s", recursive, Arrays.toString(attributes));
    }
    // 2 - Search to identify the DN of the user connecting
    SearchControls searchControls = new SearchControls();
    if (recursive) {
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    } else {
        searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    }
    searchControls.setReturningAttributes(attributes);
    searchControls.setTimeLimit(searchTimeLimit);
    return searchControls;
}
 
Example 6
Source File: LdapManager.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Method that calls the actual search on the jndi context.
 *
 * @param searchbase       the domain name (relative to initial context in ldap) to search from.
 * @param filter           the non-null filter to use for the search
 * @param scope            the scope level of the search, one off "SearchControls.ONELEVEL_SCOPE
 * @param limit            the maximum number of results to return
 * @param timeout          the maximum time to wait before abandoning the search
 * @param returnAttributes an array of strings containing the names of attributes to search. (null = all, empty array = none)
 * @return
 * @throws NamingException
 */
public NamingEnumeration search(String searchbase, String filter,
		String[] returnAttributes, int scope, int limit, int timeout)
		throws NamingException {

	SearchControls constraints = new SearchControls();

	if (SearchControls.ONELEVEL_SCOPE == scope) {
		constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
	} else if (SearchControls.SUBTREE_SCOPE == scope) {
		constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
	} else if (SearchControls.OBJECT_SCOPE == scope) {
		constraints.setSearchScope(SearchControls.OBJECT_SCOPE);
	} else {
		throw new NamingException("Unknown search scope: " + scope);
	}

	if (returnAttributes != null && returnAttributes.length == 0)
		returnAttributes = new String[] { "objectClass" };

	constraints.setCountLimit(limit);
	constraints.setTimeLimit(timeout);

	constraints.setReturningAttributes(returnAttributes);

	Name n = new CompositeName().add(searchbase);
	NamingEnumeration results = ctx.search(n, filter, constraints);

	return results;

}
 
Example 7
Source File: JNDIProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
private SearchControls makeSearchControls()
{
    final SearchControls searchControls = new SearchControls();
    searchControls.setReturningObjFlag( false );
    searchControls.setReturningAttributes( new String[0] );
    searchControls.setSearchScope( searchHelper.getSearchScope().getJndiScopeInt() );
    final String[] returnAttributes = searchHelper.getAttributes() == null
            ? null
            : searchHelper.getAttributes().toArray( new String[searchHelper.getAttributes().size()] );

    searchControls.setReturningAttributes( returnAttributes );
    searchControls.setTimeLimit( searchHelper.getTimeLimit() );
    searchControls.setCountLimit( searchHelper.getMaxResults() );
    return searchControls;
}
 
Example 8
Source File: LDAPQueryBuilder.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
protected SearchControls createSearchControls(LDAPConfiguration ldapConfigurator) {
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
    return searchControls;
}
 
Example 9
Source File: LdapConfiguration.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
public SearchControls getSearchControls() {
  SearchControls searchControls = new SearchControls();
  searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  searchControls.setTimeLimit(30000);
  return searchControls;
}
 
Example 10
Source File: LdapConnection.java    From scriptella-etl with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a connnection to a directory.
 *
 * @param parameters parameters to establish connection.
 */
public LdapConnection(ConnectionParameters parameters) {
    super(Driver.DIALECT, parameters);
    Hashtable<String, Object> env = new Hashtable<String, Object>();
    //Put default settings
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    //Put connection settings
    if (parameters.getUrl() == null) {
        throw new LdapProviderException("Connection URL is required");
    }
    env.put(Context.PROVIDER_URL, parameters.getUrl());
    if (parameters.getUser() != null) {
        env.put(Context.SECURITY_PRINCIPAL, parameters.getUser());
    }
    if (parameters.getPassword() != null) {
        env.put(Context.SECURITY_CREDENTIALS, parameters.getPassword());
    }
    //Override env with user specified connection properties
    env.putAll(parameters.getProperties());
    //Set the search controls used for queries
    searchControls = new SearchControls();
    String scope = parameters.getStringProperty(SEARCH_SCOPE_KEY);
    if (scope != null) {
        if ("object".equalsIgnoreCase(scope)) {
            searchControls.setSearchScope(SearchControls.OBJECT_SCOPE);
        } else if ("onelevel".equalsIgnoreCase(scope)) {
            searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        } else if ("subtree".equalsIgnoreCase(scope)) {
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        } else {
            throw new LdapProviderException("Unsupported " + SEARCH_SCOPE_KEY + "=" + scope);
        }
    }
    String baseDn = parameters.getStringProperty(SEARCH_BASEDN_KEY);
    this.baseDn = baseDn == null ? "" : baseDn;

    Integer tl = parameters.getIntegerProperty(SEARCH_TIMELIMIT_KEY);
    if (tl != null) {
        searchControls.setTimeLimit(tl);
    }
    Integer cl = parameters.getIntegerProperty(SEARCH_COUNTLIMIT_KEY);
    if (cl != null) {
        searchControls.setCountLimit(cl);
    }
    Number mfl = parameters.getNumberProperty(FILE_MAXLENGTH_KEY, null);
    maxFileLength = mfl == null ? null : mfl.longValue();

    driverContext = parameters.getContext();
    initializeContext(env); //Initializing context
}
 
Example 11
Source File: LDAPUserManager.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
protected SearchControls createSearchControls() {
  SearchControls searchControls = new SearchControls();
  searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
  return searchControls;
}
 
Example 12
Source File: LDAPGroupQueryImpl.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
protected SearchControls createSearchControls() {
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
    return searchControls;
}
 
Example 13
Source File: LDAPUserQueryImpl.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
protected SearchControls createSearchControls() {
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
    return searchControls;
}
 
Example 14
Source File: LDAPIdentityServiceImpl.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
protected SearchControls createSearchControls() {
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
    return searchControls;
}
 
Example 15
Source File: LdapCallbackHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 @param ctx - the context to search from
 @param user - the input username
 @param credential - the bind credential
 @param baseDN - base DN to search the ctx from
 @param filter - the search filter string
 @return the userDN string for the successful authentication
 @throws NamingException
 */
@SuppressWarnings("rawtypes")
protected String bindDNAuthentication(InitialLdapContext ctx, String user, Object credential, String baseDN,
      String filter) throws NamingException
{
   SearchControls constraints = new SearchControls();
   constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
   constraints.setTimeLimit(searchTimeLimit);
   String attrList[] = {distinguishedNameAttribute};
   constraints.setReturningAttributes(attrList);

   NamingEnumeration results = null;

   Object[] filterArgs = {user};
   results = ctx.search(baseDN, filter, filterArgs, constraints);
   if (results.hasMore() == false)
   {
      results.close();
      throw PicketBoxMessages.MESSAGES.failedToFindBaseContextDN(baseDN);
   }

   SearchResult sr = (SearchResult) results.next();
   String name = sr.getName();
   String userDN = null;
   Attributes attrs = sr.getAttributes();
   if (attrs != null)
   {
       Attribute dn = attrs.get(distinguishedNameAttribute);
       if (dn != null)
       {
               userDN = (String) dn.get();
       }
   }
   if (userDN == null)
   {
       if (sr.isRelative() == true)
           userDN = name + ("".equals(baseDN) ? "" : "," + baseDN);
       else
           throw PicketBoxMessages.MESSAGES.unableToFollowReferralForAuth(name);
   }

   safeClose(results);
   results = null;

   InitialLdapContext userCtx = constructInitialLdapContext(userDN, credential);
   safeClose(userCtx);

   return userDN;
}
 
Example 16
Source File: LdapUsersLoginModule.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected String bindDNAuthentication(InitialLdapContext ctx, String user, Object credential, String baseDN,
      String filter) throws NamingException
{
   SearchControls constraints = new SearchControls();
   constraints.setSearchScope(searchScope);
   constraints.setTimeLimit(searchTimeLimit);
   String attrList[] = {distinguishedNameAttribute};
   constraints.setReturningAttributes(attrList);

   NamingEnumeration<SearchResult> results = null;

   Object[] filterArgs = {user};
   results = ctx.search(baseDN, filter, filterArgs, constraints);
   if (!results.hasMore())
   {
      results.close();
      throw PicketBoxMessages.MESSAGES.failedToFindBaseContextDN(baseDN);
   }

   SearchResult sr = results.next();
   String name = sr.getName();
   String userDN = null;
   Attributes attrs = sr.getAttributes();
   if (attrs != null)
   {
      Attribute dn = attrs.get(distinguishedNameAttribute);
      if (dn != null)
      {
         userDN = (String) dn.get();
      }
   }
   if (userDN == null)
   {
      if (sr.isRelative())
         userDN = name + ("".equals(baseDN) ? "" : "," + baseDN);
      else
         throw PicketBoxMessages.MESSAGES.unableToFollowReferralForAuth(name);
   }

   results.close();
   results = null;
   // Bind as the user dn to authenticate the user
   InitialLdapContext userCtx = constructInitialLdapContext(userDN, credential);
   userCtx.close();

   return userDN;
}
 
Example 17
Source File: LDAPQueryBuilder.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
protected SearchControls createSearchControls(LDAPConfigurator ldapConfigurator) {
  SearchControls searchControls = new SearchControls();
  searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
  return searchControls;
}
 
Example 18
Source File: LDAPGroupManager.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
protected SearchControls createSearchControls() {
  SearchControls searchControls = new SearchControls();
  searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
  return searchControls;
}