Search in sources :

Example 21 with SearchControls

use of javax.naming.directory.SearchControls in project perun by CESNET.

the class ExtSourceLdap method querySource.

/**
	 * Query LDAP using query in defined base. Results can be limited to the maxResults.
	 *
	 * @param query
	 * @param base
	 * @param maxResults
	 * @return List of Map of the LDAP attribute names and theirs values
	 * @throws InternalErrorException
	 */
protected List<Map<String, String>> querySource(String query, String base, int maxResults) throws InternalErrorException {
    NamingEnumeration<SearchResult> results = null;
    List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
    try {
        // If query is null, then we are finding object by the base
        if (query == null) {
            log.trace("search base [{}]", base);
            // TODO jmena atributu spise prijimiat pres vstupni parametr metody
            Attributes ldapAttributes = getContext().getAttributes(base);
            if (ldapAttributes.size() > 0) {
                Map<String, String> attributes = this.getSubjectAttributes(ldapAttributes);
                if (!attributes.isEmpty()) {
                    subjects.add(attributes);
                }
            }
        } else {
            log.trace("search string [{}]", query);
            SearchControls controls = new SearchControls();
            controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            // Set timeout to 5s
            controls.setTimeLimit(5000);
            if (maxResults > 0) {
                controls.setCountLimit(maxResults);
            }
            if (base == null)
                base = "";
            results = getContext().search(base, query, controls);
            while (results.hasMore()) {
                SearchResult searchResult = (SearchResult) results.next();
                Attributes attributes = searchResult.getAttributes();
                Map<String, String> subjectAttributes = this.getSubjectAttributes(attributes);
                if (!subjectAttributes.isEmpty()) {
                    subjects.add(subjectAttributes);
                }
            }
        }
        log.trace("Returning [{}] subjects", subjects.size());
        return subjects;
    } catch (NamingException e) {
        log.error("LDAP exception during running query '{}'", query);
        throw new InternalErrorException("LDAP exception during running query: " + query + ".", e);
    } finally {
        try {
            if (results != null) {
                results.close();
            }
        } catch (Exception e) {
            log.error("LDAP exception during closing result, while running query '{}'", query);
            throw new InternalErrorException(e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) SearchControls(javax.naming.directory.SearchControls) NamingException(javax.naming.NamingException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) HashMap(java.util.HashMap) Map(java.util.Map) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ExtSourceUnsupportedOperationException(cz.metacentrum.perun.core.api.exceptions.ExtSourceUnsupportedOperationException) NamingException(javax.naming.NamingException) SubjectNotExistsException(cz.metacentrum.perun.core.api.exceptions.SubjectNotExistsException)

Example 22 with SearchControls

use of javax.naming.directory.SearchControls in project cloudstack by apache.

the class OpenLdapUserManagerImpl method getUserForDn.

private LdapUser getUserForDn(String userdn, LdapContext context) throws NamingException {
    final SearchControls controls = new SearchControls();
    controls.setSearchScope(_ldapConfiguration.getScope());
    controls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());
    NamingEnumeration<SearchResult> result = context.search(userdn, "(objectClass=" + _ldapConfiguration.getUserObject() + ")", controls);
    if (result.hasMoreElements()) {
        return createUser(result.nextElement());
    } else {
        throw new NamingException("No user found for dn " + userdn);
    }
}
Also used : SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException)

Example 23 with SearchControls

use of javax.naming.directory.SearchControls in project cloudstack by apache.

the class OpenLdapUserManagerImpl method getUsersInGroup.

@Override
public List<LdapUser> getUsersInGroup(String groupName, LdapContext context) throws NamingException {
    String attributeName = _ldapConfiguration.getGroupUniqueMemeberAttribute();
    final SearchControls controls = new SearchControls();
    controls.setSearchScope(_ldapConfiguration.getScope());
    controls.setReturningAttributes(new String[] { attributeName });
    NamingEnumeration<SearchResult> result = context.search(_ldapConfiguration.getBaseDn(), generateGroupSearchFilter(groupName), controls);
    final List<LdapUser> users = new ArrayList<LdapUser>();
    //Expecting only one result which has all the users
    if (result.hasMoreElements()) {
        Attribute attribute = result.nextElement().getAttributes().get(attributeName);
        NamingEnumeration<?> values = attribute.getAll();
        while (values.hasMoreElements()) {
            String userdn = String.valueOf(values.nextElement());
            try {
                users.add(getUserForDn(userdn, context));
            } catch (NamingException e) {
                s_logger.info("Userdn: " + userdn + " Not Found:: Exception message: " + e.getMessage());
            }
        }
    }
    Collections.sort(users);
    return users;
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException)

Example 24 with SearchControls

use of javax.naming.directory.SearchControls in project cloudstack by apache.

the class OpenLdapUserManagerImpl method searchUser.

public LdapUser searchUser(final String basedn, final String searchString, final LdapContext context) throws NamingException, IOException {
    final SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(_ldapConfiguration.getScope());
    searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());
    NamingEnumeration<SearchResult> results = context.search(basedn, searchString, searchControls);
    final List<LdapUser> users = new ArrayList<LdapUser>();
    while (results.hasMoreElements()) {
        final SearchResult result = results.nextElement();
        users.add(createUser(result));
    }
    if (users.size() == 1) {
        return users.get(0);
    } else {
        throw new NamingException("No user found for basedn " + basedn + " and searchString " + searchString);
    }
}
Also used : ArrayList(java.util.ArrayList) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException)

Example 25 with SearchControls

use of javax.naming.directory.SearchControls in project cloudstack by apache.

the class ADLdapUserManagerImpl method getUsersInGroup.

@Override
public List<LdapUser> getUsersInGroup(String groupName, LdapContext context) throws NamingException {
    if (StringUtils.isBlank(groupName)) {
        throw new IllegalArgumentException("ldap group name cannot be blank");
    }
    String basedn = _ldapConfiguration.getBaseDn();
    if (StringUtils.isBlank(basedn)) {
        throw new IllegalArgumentException("ldap basedn is not configured");
    }
    final SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(_ldapConfiguration.getScope());
    searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());
    NamingEnumeration<SearchResult> results = context.search(basedn, generateADGroupSearchFilter(groupName), searchControls);
    final List<LdapUser> users = new ArrayList<LdapUser>();
    while (results.hasMoreElements()) {
        final SearchResult result = results.nextElement();
        users.add(createUser(result));
    }
    return users;
}
Also used : ArrayList(java.util.ArrayList) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult)

Aggregations

SearchControls (javax.naming.directory.SearchControls)59 SearchResult (javax.naming.directory.SearchResult)46 NamingException (javax.naming.NamingException)27 ArrayList (java.util.ArrayList)23 Attributes (javax.naming.directory.Attributes)20 NamingEnumeration (javax.naming.NamingEnumeration)16 Attribute (javax.naming.directory.Attribute)15 DirContext (javax.naming.directory.DirContext)13 InitialDirContext (javax.naming.directory.InitialDirContext)8 LdapContext (javax.naming.ldap.LdapContext)8 HashMap (java.util.HashMap)5 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)5 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)5 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 LinkedHashSet (java.util.LinkedHashSet)4 Map (java.util.Map)4 PartialResultException (javax.naming.PartialResultException)4 Control (javax.naming.ldap.Control)4 PagedResultsControl (javax.naming.ldap.PagedResultsControl)4