Search in sources :

Example 1 with ContextMapper

use of org.springframework.ldap.core.ContextMapper in project spring-security by spring-projects.

the class SpringSecurityLdapTemplate method searchForMultipleAttributeValues.

/**
	 * Performs a search using the supplied filter and returns the values of each named
	 * attribute found in all entries matched by the search. Note that one directory entry
	 * may have several values for the attribute. Intended for role searches and similar
	 * scenarios.
	 *
	 * @param base the DN to search in
	 * @param filter search filter to use
	 * @param params the parameters to substitute in the search filter
	 * @param attributeNames the attributes' values that are to be retrieved.
	 *
	 * @return the set of String values for each attribute found in all the matching
	 * entries. The attribute name is the key for each set of values. In addition each map
	 * contains the DN as a String with the key predefined key {@link #DN_KEY}.
	 */
public Set<Map<String, List<String>>> searchForMultipleAttributeValues(final String base, final String filter, final Object[] params, final String[] attributeNames) {
    // Escape the params acording to RFC2254
    Object[] encodedParams = new String[params.length];
    for (int i = 0; i < params.length; i++) {
        encodedParams[i] = LdapEncoder.filterEncode(params[i].toString());
    }
    String formattedFilter = MessageFormat.format(filter, encodedParams);
    logger.debug("Using filter: " + formattedFilter);
    final HashSet<Map<String, List<String>>> set = new HashSet<Map<String, List<String>>>();
    ContextMapper roleMapper = new ContextMapper() {

        public Object mapFromContext(Object ctx) {
            DirContextAdapter adapter = (DirContextAdapter) ctx;
            Map<String, List<String>> record = new HashMap<String, List<String>>();
            if (attributeNames == null || attributeNames.length == 0) {
                try {
                    for (NamingEnumeration ae = adapter.getAttributes().getAll(); ae.hasMore(); ) {
                        Attribute attr = (Attribute) ae.next();
                        extractStringAttributeValues(adapter, record, attr.getID());
                    }
                } catch (NamingException x) {
                    org.springframework.ldap.support.LdapUtils.convertLdapException(x);
                }
            } else {
                for (String attributeName : attributeNames) {
                    extractStringAttributeValues(adapter, record, attributeName);
                }
            }
            record.put(DN_KEY, Arrays.asList(getAdapterDN(adapter)));
            set.add(record);
            return null;
        }
    };
    SearchControls ctls = new SearchControls();
    ctls.setSearchScope(searchControls.getSearchScope());
    ctls.setReturningAttributes(attributeNames != null && attributeNames.length > 0 ? attributeNames : null);
    search(base, formattedFilter, ctls, roleMapper);
    return set;
}
Also used : HashMap(java.util.HashMap) Attribute(javax.naming.directory.Attribute) NamingEnumeration(javax.naming.NamingEnumeration) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) ArrayList(java.util.ArrayList) List(java.util.List) NamingException(javax.naming.NamingException) SearchControls(javax.naming.directory.SearchControls) HashMap(java.util.HashMap) Map(java.util.Map) ContextMapper(org.springframework.ldap.core.ContextMapper) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 NamingEnumeration (javax.naming.NamingEnumeration)1 NamingException (javax.naming.NamingException)1 Attribute (javax.naming.directory.Attribute)1 SearchControls (javax.naming.directory.SearchControls)1 ContextMapper (org.springframework.ldap.core.ContextMapper)1 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)1