Search in sources :

Example 81 with NamingException

use of javax.naming.NamingException in project perun by CESNET.

the class ExtSourceEGISSO method querySource.

@Override
protected List<Map<String, String>> querySource(String query, String base, int maxResults) throws InternalErrorException {
    List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
    NamingEnumeration<SearchResult> results = null;
    if (base == null || base.isEmpty()) {
        base = "ou=People,dc=egi,dc=eu";
    }
    if (query == null || query.isEmpty())
        throw new InternalErrorException("Query can't be null when searching through EGI SSO.");
    try {
        SearchControls controls = new SearchControls();
        controls.setTimeLimit(5000);
        if (maxResults > 0) {
            controls.setCountLimit(maxResults);
        }
        results = getContext().search(base, query, controls);
        while (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            subjects.add(processResultToSubject(searchResult));
        }
        log.trace("Returning [{}] subjects", subjects.size());
    } 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);
        }
    }
    return subjects;
}
Also used : ArrayList(java.util.ArrayList) 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) IOException(java.io.IOException) NamingException(javax.naming.NamingException)

Example 82 with NamingException

use of javax.naming.NamingException in project perun by CESNET.

the class ExtSourceLdap method getGroupSubjects.

public List<Map<String, String>> getGroupSubjects(Map<String, String> attributes) throws InternalErrorException {
    NamingEnumeration<SearchResult> results = null;
    List<String> ldapGroupSubjects = new ArrayList<String>();
    // Get the LDAP group name
    String ldapGroupName = attributes.get(GroupsManager.GROUPMEMBERSQUERY_ATTRNAME);
    // Get optional filter for members filtering
    String filter = attributes.get(GroupsManager.GROUPMEMBERSFILTER_ATTRNAME);
    try {
        log.trace("LDAP External Source: searching for group subjects [{}]", ldapGroupName);
        String attrName;
        if (getAttributes().containsKey("memberAttribute")) {
            attrName = (String) getAttributes().get("memberAttribute");
        } else {
            // Default value
            attrName = "uniqueMember";
        }
        List<String> retAttrs = new ArrayList<String>();
        retAttrs.add(attrName);
        String[] retAttrsArray = retAttrs.toArray(new String[retAttrs.size()]);
        Attributes attrs = getContext().getAttributes(ldapGroupName, retAttrsArray);
        Attribute ldapAttribute = null;
        // Get the list of returned groups, should be only one
        if (attrs.get(attrName) != null) {
            // Get the attribute which holds group subjects
            ldapAttribute = attrs.get(attrName);
        }
        if (ldapAttribute != null) {
            // Get the DNs of the subjects
            for (int i = 0; i < ldapAttribute.size(); i++) {
                String ldapSubjectDN = (String) ldapAttribute.get(i);
                ldapGroupSubjects.add(ldapSubjectDN);
                log.trace("LDAP External Source: found group subject [{}].", ldapSubjectDN);
            }
        }
        List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
        // If attribute filter not exists, use optional default filter from extSource definition
        if (filter == null)
            filter = filteredQuery;
        // Now query LDAP again and search for each subject
        for (String ldapSubjectName : ldapGroupSubjects) {
            subjects.addAll(this.querySource(filter, ldapSubjectName, 0));
        }
        return subjects;
    } catch (NamingException e) {
        log.error("LDAP exception during running query '{}'", ldapGroupName);
        throw new InternalErrorException("Entry '" + ldapGroupName + "' was not found in LDAP.", e);
    } finally {
        try {
            if (results != null) {
                results.close();
            }
        } catch (Exception e) {
            log.error("LDAP exception during closing result, while running query '{}'", ldapGroupName);
            throw new InternalErrorException(e);
        }
    }
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) 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) NamingException(javax.naming.NamingException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 83 with NamingException

use of javax.naming.NamingException 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 84 with NamingException

use of javax.naming.NamingException in project perun by CESNET.

the class ExtSourceLdap method initContext.

protected void initContext() throws InternalErrorException {
    // Load mapping between LDAP attributes and Perun attributes
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    if (getAttributes().containsKey("referral")) {
        env.put(Context.REFERRAL, (String) getAttributes().get("referral"));
    }
    if (getAttributes().containsKey("url")) {
        env.put(Context.PROVIDER_URL, (String) getAttributes().get("url"));
    } else {
        throw new InternalErrorException("url attributes is required");
    }
    if (getAttributes().containsKey("user")) {
        env.put(Context.SECURITY_PRINCIPAL, (String) getAttributes().get("user"));
    }
    if (getAttributes().containsKey("password")) {
        env.put(Context.SECURITY_CREDENTIALS, (String) getAttributes().get("password"));
    }
    if (getAttributes().containsKey("filteredQuery")) {
        filteredQuery = (String) getAttributes().get("filteredQuery");
    }
    try {
        // ldapMapping contains entries like: firstName={givenName},lastName={sn},email={mail}
        if (getAttributes().get("ldapMapping") == null) {
            throw new InternalErrorException("ldapMapping attributes is required");
        }
        String[] ldapMapping = ((String) getAttributes().get("ldapMapping")).trim().split(",\n");
        mapping = new HashMap<String, String>();
        for (String entry : ldapMapping) {
            String[] values = entry.trim().split("=", 2);
            mapping.put(values[0].trim(), values[1].trim());
        }
        this.dirContext = new InitialDirContext(env);
    } catch (NamingException e) {
        log.error("LDAP exception during creating the context.");
        throw new InternalErrorException(e);
    }
}
Also used : Hashtable(java.util.Hashtable) NamingException(javax.naming.NamingException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 85 with NamingException

use of javax.naming.NamingException in project perun by CESNET.

the class ExtSourceLdap method close.

public void close() throws InternalErrorException {
    if (this.dirContext != null) {
        try {
            this.dirContext.close();
            this.dirContext = null;
        } catch (NamingException e) {
            throw new InternalErrorException(e);
        }
    }
}
Also used : NamingException(javax.naming.NamingException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

NamingException (javax.naming.NamingException)698 InitialContext (javax.naming.InitialContext)234 Context (javax.naming.Context)169 IOException (java.io.IOException)82 NameNotFoundException (javax.naming.NameNotFoundException)67 SQLException (java.sql.SQLException)58 Reference (javax.naming.Reference)53 DataSource (javax.sql.DataSource)52 Test (org.junit.Test)51 Attribute (javax.naming.directory.Attribute)49 DirContext (javax.naming.directory.DirContext)48 Properties (java.util.Properties)45 ArrayList (java.util.ArrayList)41 Name (javax.naming.Name)36 SearchResult (javax.naming.directory.SearchResult)35 Hashtable (java.util.Hashtable)34 InitialDirContext (javax.naming.directory.InitialDirContext)34 Connection (java.sql.Connection)33 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)32 Attributes (javax.naming.directory.Attributes)30