Search in sources :

Example 11 with PartialResultException

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

the class JNDIRealm method addAttributeValues.

/**
 * Add values of a specified attribute to a list
 *
 * @param attrId Attribute name
 * @param attrs Attributes containing the new values
 * @param values ArrayList containing values found so far
 * @return the list of attribute values
 * @exception NamingException if a directory server error occurs
 */
private ArrayList<String> addAttributeValues(String attrId, Attributes attrs, ArrayList<String> values) throws NamingException {
    if (containerLog.isTraceEnabled()) {
        containerLog.trace("  retrieving values for attribute " + attrId);
    }
    if (attrId == null || attrs == null) {
        return values;
    }
    if (values == null) {
        values = new ArrayList<>();
    }
    Attribute attr = attrs.get(attrId);
    if (attr == null) {
        return values;
    }
    NamingEnumeration<?> e = attr.getAll();
    try {
        while (e.hasMore()) {
            String value = (String) e.next();
            values.add(value);
        }
    } catch (PartialResultException ex) {
        if (!adCompat) {
            throw ex;
        }
    } finally {
        e.close();
    }
    return values;
}
Also used : Attribute(javax.naming.directory.Attribute) PartialResultException(javax.naming.PartialResultException)

Example 12 with PartialResultException

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

the class LDAPCache method doGetUserRoles.

private String[] doGetUserRoles(String user, String userDn, String userDnNamespace) throws NamingException {
    DirContext context = open();
    SearchControls controls = new SearchControls();
    if (options.getRoleSearchSubtree()) {
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    } else {
        controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    }
    String filter = options.getRoleFilter();
    if (filter != null) {
        filter = filter.replaceAll(Pattern.quote("%u"), Matcher.quoteReplacement(user));
        filter = filter.replaceAll(Pattern.quote("%dn"), Matcher.quoteReplacement(userDn));
        filter = filter.replaceAll(Pattern.quote("%fqdn"), Matcher.quoteReplacement(userDnNamespace));
        filter = filter.replace("\\", "\\\\");
        LOGGER.debug("Looking for the user roles in LDAP with ");
        LOGGER.debug("  base DN: {}", options.getRoleBaseDn());
        LOGGER.debug("  filter: {}", filter);
        NamingEnumeration<SearchResult> namingEnumeration = context.search(options.getRoleBaseDn(), filter, controls);
        List<String> rolesList = new ArrayList<>();
        try {
            while (namingEnumeration.hasMore()) {
                SearchResult result = namingEnumeration.next();
                Attributes attributes = result.getAttributes();
                Attribute roles1 = attributes.get(options.getRoleNameAttribute());
                if (roles1 != null) {
                    for (int i = 0; i < roles1.size(); i++) {
                        String role = (String) roles1.get(i);
                        if (role != null) {
                            LOGGER.debug("User {} is a member of role {}", user, role);
                            // handle role mapping
                            Set<String> roleMappings = tryMappingRole(role);
                            if (roleMappings.isEmpty()) {
                                rolesList.add(role);
                            } else {
                                rolesList.addAll(roleMappings);
                            }
                        }
                    }
                }
            }
        } catch (PartialResultException e) {
            // Workaround for AD servers not handling referrals correctly.
            if (options.getIgnorePartialResultException()) {
                LOGGER.debug("PartialResultException encountered and ignored", e);
            } else {
                throw e;
            }
        } finally {
            if (namingEnumeration != null) {
                try {
                    namingEnumeration.close();
                } catch (NamingException e) {
                // Ignore
                }
            }
        }
        return rolesList.toArray(new String[rolesList.size()]);
    } else {
        LOGGER.debug("The user role filter is null so no roles are retrieved");
        return new String[] {};
    }
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) PartialResultException(javax.naming.PartialResultException) InitialDirContext(javax.naming.directory.InitialDirContext) EventDirContext(javax.naming.event.EventDirContext) DirContext(javax.naming.directory.DirContext) SearchControls(javax.naming.directory.SearchControls) NamingException(javax.naming.NamingException)

Example 13 with PartialResultException

use of javax.naming.PartialResultException in project iaf by ibissource.

the class LdapClient method mapMultiValuedAttribute.

public void mapMultiValuedAttribute(NamingEnumeration<SearchResult> searchResultEnum, Callback<Attribute, Object> callback) throws NamingException {
    try {
        while (searchResultEnum.hasMore()) {
            Attributes attributes = searchResultEnum.next().getAttributes();
            NamingEnumeration<? extends Attribute> attrenum = attributes.getAll();
            try {
                while (attrenum.hasMore()) {
                    Attribute attr = attrenum.next();
                    NamingEnumeration<?> multivalueattribute = attr.getAll();
                    try {
                        while (multivalueattribute.hasMore()) {
                            callback.handle(attr, multivalueattribute.next());
                        }
                    } finally {
                        multivalueattribute.close();
                    }
                }
            } finally {
                attrenum.close();
            }
        }
    } catch (PartialResultException e) {
        if (log.isDebugEnabled())
            log.debug("ignoring Exception: " + e);
    } finally {
        searchResultEnum.close();
    }
}
Also used : Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) PartialResultException(javax.naming.PartialResultException)

Example 14 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 15 with PartialResultException

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

the class JNDIRealm method addAttributeValues.

/**
 * Add values of a specified attribute to a list
 *
 * @param attrId Attribute name
 * @param attrs Attributes containing the new values
 * @param values ArrayList containing values found so far
 *
 * @exception NamingException if a directory server error occurs
 */
private ArrayList<String> addAttributeValues(String attrId, Attributes attrs, ArrayList<String> values) throws NamingException {
    if (containerLog.isTraceEnabled())
        containerLog.trace("  retrieving values for attribute " + attrId);
    if (attrId == null || attrs == null)
        return values;
    if (values == null)
        values = new ArrayList<String>();
    Attribute attr = attrs.get(attrId);
    if (attr == null)
        return values;
    NamingEnumeration<?> e = attr.getAll();
    try {
        while (e.hasMore()) {
            String value = (String) e.next();
            values.add(value);
        }
    } catch (PartialResultException ex) {
        if (!adCompat)
            throw ex;
    } finally {
        e.close();
    }
    return values;
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) PartialResultException(javax.naming.PartialResultException)

Aggregations

PartialResultException (javax.naming.PartialResultException)22 SearchResult (javax.naming.directory.SearchResult)14 Attribute (javax.naming.directory.Attribute)12 SearchControls (javax.naming.directory.SearchControls)12 Attributes (javax.naming.directory.Attributes)9 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)7 HashMap (java.util.HashMap)5 CompositeName (javax.naming.CompositeName)5 Name (javax.naming.Name)5 ParameterizedString (com.google.gerrit.common.data.ParameterizedString)4 IOException (java.io.IOException)4 LinkedHashSet (java.util.LinkedHashSet)4 SizeLimitExceededException (javax.naming.SizeLimitExceededException)4 PagedResultsControl (javax.naming.ldap.PagedResultsControl)4 MutablePrincipalCollection (org.apache.shiro.subject.MutablePrincipalCollection)4 Entry (java.util.Map.Entry)3 NameParser (javax.naming.NameParser)3 NamingException (javax.naming.NamingException)3 ImmutableSet (com.google.common.collect.ImmutableSet)2