Search in sources :

Example 86 with InternalErrorException

use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException in project perun by CESNET.

the class ExtSourcesManagerImpl method deleteExtSource.

public void deleteExtSource(PerunSession sess, ExtSource extSource) throws InternalErrorException, ExtSourceAlreadyRemovedException {
    try {
        // Delete associated attributes
        jdbc.update("delete from ext_sources_attributes where ext_sources_id=?", extSource.getId());
        // Delete the external source
        int numAffected = jdbc.update("delete from ext_sources where id=?", extSource.getId());
        if (numAffected == 0)
            throw new ExtSourceAlreadyRemovedException("ExtSource: " + extSource);
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : ExtSourceAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.ExtSourceAlreadyRemovedException) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 87 with InternalErrorException

use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException 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 88 with InternalErrorException

use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException 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 89 with InternalErrorException

use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException 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 90 with InternalErrorException

use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException 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

InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)376 Attribute (cz.metacentrum.perun.core.api.Attribute)119 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)104 ArrayList (java.util.ArrayList)94 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)89 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)78 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)68 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)67 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)44 User (cz.metacentrum.perun.core.api.User)37 Group (cz.metacentrum.perun.core.api.Group)36 InternalErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException)33 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)33 HashMap (java.util.HashMap)30 IOException (java.io.IOException)28 Member (cz.metacentrum.perun.core.api.Member)24 Map (java.util.Map)24 Facility (cz.metacentrum.perun.core.api.Facility)23 List (java.util.List)23 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)22