Search in sources :

Example 6 with PartialResultException

use of javax.naming.PartialResultException in project gerrit by GerritCodeReview.

the class LdapQuery method query.

List<Result> query(final DirContext ctx, final Map<String, String> params) throws NamingException {
    final SearchControls sc = new SearchControls();
    final NamingEnumeration<SearchResult> res;
    sc.setSearchScope(searchScope.scope());
    sc.setReturningAttributes(returnAttributes);
    res = ctx.search(base, pattern.getRawPattern(), pattern.bind(params), sc);
    try {
        final List<Result> r = new ArrayList<>();
        try {
            while (res.hasMore()) {
                r.add(new Result(res.next()));
            }
        } catch (PartialResultException e) {
        // Ignored
        }
        return r;
    } finally {
        res.close();
    }
}
Also used : ArrayList(java.util.ArrayList) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) PartialResultException(javax.naming.PartialResultException) SearchResult(javax.naming.directory.SearchResult)

Example 7 with PartialResultException

use of javax.naming.PartialResultException in project tomcat by apache.

the class JNDIRealm method getUserBySearch.

/**
     * Search the directory to return a User object containing
     * information about the user with the specified username, if
     * found in the directory; otherwise return <code>null</code>.
     *
     * @param context The directory context
     * @param username The username
     * @param attrIds String[]containing names of attributes to retrieve.
     * @return the User object
     * @exception NamingException if a directory server error occurs
     */
protected User getUserBySearch(DirContext context, String username, String[] attrIds) throws NamingException {
    if (username == null || userSearchFormat == null)
        return null;
    // Form the search filter
    String filter = userSearchFormat.format(new String[] { username });
    // Set up the search controls
    SearchControls constraints = new SearchControls();
    if (userSubtree) {
        constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
    } else {
        constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    }
    constraints.setCountLimit(sizeLimit);
    constraints.setTimeLimit(timeLimit);
    // Specify the attributes to be retrieved
    if (attrIds == null)
        attrIds = new String[0];
    constraints.setReturningAttributes(attrIds);
    NamingEnumeration<SearchResult> results = context.search(userBase, filter, constraints);
    try {
        // Fail if no entries found
        try {
            if (results == null || !results.hasMore()) {
                return null;
            }
        } catch (PartialResultException ex) {
            if (!adCompat)
                throw ex;
            else
                return null;
        }
        // Get result for the first entry found
        SearchResult result = results.next();
        // Check no further entries were found
        try {
            if (results.hasMore()) {
                if (containerLog.isInfoEnabled())
                    containerLog.info("username " + username + " has multiple entries");
                return null;
            }
        } catch (PartialResultException ex) {
            if (!adCompat)
                throw ex;
        }
        String dn = getDistinguishedName(context, userBase, result);
        if (containerLog.isTraceEnabled())
            containerLog.trace("  entry found for " + username + " with dn " + dn);
        // Get the entry's attributes
        Attributes attrs = result.getAttributes();
        if (attrs == null)
            return null;
        // Retrieve value of userPassword
        String password = null;
        if (userPassword != null)
            password = getAttributeValue(userPassword, attrs);
        String userRoleAttrValue = null;
        if (userRoleAttribute != null) {
            userRoleAttrValue = getAttributeValue(userRoleAttribute, attrs);
        }
        // Retrieve values of userRoleName attribute
        ArrayList<String> roles = null;
        if (userRoleName != null)
            roles = addAttributeValues(userRoleName, attrs, roles);
        return new User(username, dn, password, roles, userRoleAttrValue);
    } finally {
        if (results != null) {
            results.close();
        }
    }
}
Also used : Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) PartialResultException(javax.naming.PartialResultException)

Example 8 with PartialResultException

use of javax.naming.PartialResultException in project spring-security by spring-projects.

the class SpringSecurityLdapTemplate method searchForSingleEntryInternal.

/**
	 * Internal method extracted to avoid code duplication in AD search.
	 */
public static DirContextOperations searchForSingleEntryInternal(DirContext ctx, SearchControls searchControls, String base, String filter, Object[] params) throws NamingException {
    final DistinguishedName ctxBaseDn = new DistinguishedName(ctx.getNameInNamespace());
    final DistinguishedName searchBaseDn = new DistinguishedName(base);
    final NamingEnumeration<SearchResult> resultsEnum = ctx.search(searchBaseDn, filter, params, buildControls(searchControls));
    if (logger.isDebugEnabled()) {
        logger.debug("Searching for entry under DN '" + ctxBaseDn + "', base = '" + searchBaseDn + "', filter = '" + filter + "'");
    }
    Set<DirContextOperations> results = new HashSet<DirContextOperations>();
    try {
        while (resultsEnum.hasMore()) {
            SearchResult searchResult = resultsEnum.next();
            DirContextAdapter dca = (DirContextAdapter) searchResult.getObject();
            Assert.notNull(dca, "No object returned by search, DirContext is not correctly configured");
            if (logger.isDebugEnabled()) {
                logger.debug("Found DN: " + dca.getDn());
            }
            results.add(dca);
        }
    } catch (PartialResultException e) {
        LdapUtils.closeEnumeration(resultsEnum);
        logger.info("Ignoring PartialResultException");
    }
    if (results.size() == 0) {
        throw new IncorrectResultSizeDataAccessException(1, 0);
    }
    if (results.size() > 1) {
        throw new IncorrectResultSizeDataAccessException(1, results.size());
    }
    return results.iterator().next();
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchResult(javax.naming.directory.SearchResult) PartialResultException(javax.naming.PartialResultException) HashSet(java.util.HashSet)

Aggregations

PartialResultException (javax.naming.PartialResultException)8 SearchResult (javax.naming.directory.SearchResult)5 Attribute (javax.naming.directory.Attribute)4 SearchControls (javax.naming.directory.SearchControls)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ParameterizedString (com.google.gerrit.common.data.ParameterizedString)2 HashMap (java.util.HashMap)2 CompositeName (javax.naming.CompositeName)2 Name (javax.naming.Name)2 Attributes (javax.naming.directory.Attributes)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)1 AccountException (com.google.gerrit.server.account.AccountException)1 IOException (java.io.IOException)1 LinkedHashSet (java.util.LinkedHashSet)1 Entry (java.util.Map.Entry)1 NameParser (javax.naming.NameParser)1 NamingException (javax.naming.NamingException)1 SizeLimitExceededException (javax.naming.SizeLimitExceededException)1