Search in sources :

Example 16 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 17 with SearchControls

use of javax.naming.directory.SearchControls in project Activiti by Activiti.

the class LDAPGroupManager method createSearchControls.

protected SearchControls createSearchControls() {
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
    return searchControls;
}
Also used : SearchControls(javax.naming.directory.SearchControls)

Example 18 with SearchControls

use of javax.naming.directory.SearchControls in project Activiti by Activiti.

the class LDAPQueryBuilder method createSearchControls.

protected SearchControls createSearchControls(LDAPConfigurator ldapConfigurator) {
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
    return searchControls;
}
Also used : SearchControls(javax.naming.directory.SearchControls)

Example 19 with SearchControls

use of javax.naming.directory.SearchControls in project nhin-d by DirectProject.

the class LdapPublicCertUtilImpl method getBaseNamingContexts.

/**
	 * Gets the base DNs for a connected LDAP context
	 * @param ctx The LDAP connection context.
	 * @return List of string representing the base DNs of the LDAP server.
	 */
protected List<String> getBaseNamingContexts(InitialDirContext ctx) {
    List<String> dNs = new ArrayList<String>();
    try {
        SearchControls ctls = new SearchControls();
        ctls.setReturningObjFlag(true);
        ctls.setSearchScope(SearchControls.OBJECT_SCOPE);
        ctls.setReturningAttributes(new String[] { BASE_DN_ATTRIBUTE });
        NamingEnumeration<SearchResult> objResults = ctx.search("", "objectclass=*", ctls);
        while (objResults != null && objResults.hasMore()) {
            final SearchResult objEntry = objResults.nextElement();
            final Attributes objAttributes = objEntry.getAttributes();
            if (objAttributes != null) {
                final Attribute objAttribute = objAttributes.get(BASE_DN_ATTRIBUTE);
                NamingEnumeration<? extends Object> allValues = objAttribute.getAll();
                while (allValues.hasMoreElements()) dNs.add((String) allValues.nextElement());
            }
        }
        if (dNs.isEmpty())
            LOGGER.warn("No base DNs could be located for LDAP context");
    } catch (Exception e) {
        // no naming contexts could be located or query error
        LOGGER.warn("ERROR looking up base DNs for LDAP context", e);
    }
    return dNs;
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) UnknownHostException(java.net.UnknownHostException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 20 with SearchControls

use of javax.naming.directory.SearchControls in project nhin-d by DirectProject.

the class LDAPResearchTest method searchDNs.

private Set<SearchResult> searchDNs(String filter, String partition, String base, int scope, DirContext appRoot) throws Exception {
    if (appRoot == null)
        appRoot = createContext(partition);
    SearchControls controls = new SearchControls();
    controls.setSearchScope(scope);
    NamingEnumeration<SearchResult> result = appRoot.search(base, filter, controls);
    // collect all results
    Set<SearchResult> entries = new HashSet<SearchResult>();
    while (result.hasMore()) {
        SearchResult entry = (SearchResult) result.next();
        entries.add(entry);
    }
    return entries;
}
Also used : SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) HashSet(java.util.HashSet)

Aggregations

SearchControls (javax.naming.directory.SearchControls)70 SearchResult (javax.naming.directory.SearchResult)55 NamingException (javax.naming.NamingException)35 ArrayList (java.util.ArrayList)24 NamingEnumeration (javax.naming.NamingEnumeration)21 Attributes (javax.naming.directory.Attributes)21 Attribute (javax.naming.directory.Attribute)19 DirContext (javax.naming.directory.DirContext)15 InitialDirContext (javax.naming.directory.InitialDirContext)14 IOException (java.io.IOException)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 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 LoginException (javax.security.auth.login.LoginException)4