Search in sources :

Example 1 with ExtSourceApi

use of cz.metacentrum.perun.core.implApi.ExtSourceApi in project perun by CESNET.

the class MembersManagerBlImpl method createMember.

public Member createMember(PerunSession sess, Vo vo, ExtSource extSource, String login, List<Group> groups) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, AlreadyMemberException, ExtendMembershipException, GroupOperationsException {
    //First of all get candidate from extSource directly
    Candidate candidate = null;
    try {
        if (extSource instanceof ExtSourceApi) {
            //get first subject, then create candidate
            Map<String, String> subject = ((ExtSourceSimpleApi) extSource).getSubjectByLogin(login);
            candidate = (getPerunBl().getExtSourcesManagerBl().getCandidate(sess, subject, extSource, login));
        } else if (extSource instanceof ExtSourceSimpleApi) {
            // get candidates from external source by login
            candidate = (getPerunBl().getExtSourcesManagerBl().getCandidate(sess, extSource, login));
        }
    } catch (CandidateNotExistsException | SubjectNotExistsException ex) {
        throw new InternalErrorException("Can't find candidate for login " + login + " in extSource " + extSource, ex);
    } catch (ExtSourceUnsupportedOperationException ex) {
        throw new InternalErrorException("Some operation is not allowed for extSource " + extSource, ex);
    } catch (ExtSourceNotExistsException ex) {
        throw new InternalErrorException("ExtSource " + extSource + " not exists.");
    }
    return this.createMember(sess, vo, candidate, groups);
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) SubjectNotExistsException(cz.metacentrum.perun.core.api.exceptions.SubjectNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ExtSourceApi(cz.metacentrum.perun.core.implApi.ExtSourceApi) ExtSourceUnsupportedOperationException(cz.metacentrum.perun.core.api.exceptions.ExtSourceUnsupportedOperationException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) UserExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException) ExtSourceSimpleApi(cz.metacentrum.perun.core.implApi.ExtSourceSimpleApi) CandidateNotExistsException(cz.metacentrum.perun.core.api.exceptions.CandidateNotExistsException)

Example 2 with ExtSourceApi

use of cz.metacentrum.perun.core.implApi.ExtSourceApi in project perun by CESNET.

the class VosManagerBlImpl method findCandidates.

public List<Candidate> findCandidates(PerunSession sess, Vo vo, String searchString, int maxNumOfResults) throws InternalErrorException {
    List<Candidate> candidates = new ArrayList<Candidate>();
    int numOfResults = 0;
    try {
        // Iterate through all registered extSources
        for (ExtSource source : getPerunBl().getExtSourcesManagerBl().getVoExtSources(sess, vo)) {
            // Info if this is only simple ext source, change behavior if not
            boolean simpleExtSource = true;
            // Get potential subjects from the extSource
            List<Map<String, String>> subjects;
            try {
                if (source instanceof ExtSourceApi) {
                    // find subjects with all their properties
                    subjects = ((ExtSourceApi) source).findSubjects(searchString, maxNumOfResults);
                    simpleExtSource = false;
                } else {
                    // find subjects only with logins - they then must be retrieved by login
                    subjects = ((ExtSourceSimpleApi) source).findSubjectsLogins(searchString, maxNumOfResults);
                }
            } catch (ExtSourceUnsupportedOperationException e1) {
                log.warn("ExtSource {} doesn't support findSubjects", source.getName());
                continue;
            } catch (InternalErrorException e) {
                log.error("Error occurred on ExtSource {},  Exception {}.", source.getName(), e);
                continue;
            } finally {
                try {
                    ((ExtSourceSimpleApi) source).close();
                } catch (ExtSourceUnsupportedOperationException e) {
                // ExtSource doesn't support that functionality, so silently skip it.
                } catch (InternalErrorException e) {
                    log.error("Can't close extSource connection. Cause: {}", e);
                }
            }
            Set<String> uniqueLogins = new HashSet<>();
            for (Map<String, String> s : subjects) {
                // Check if the user has unique identifier within extSource
                if ((s.get("login") == null) || (s.get("login") != null && ((String) s.get("login")).isEmpty())) {
                    log.error("User '{}' cannot be added, because he/she doesn't have a unique identifier (login)", s);
                    // Skip to another user
                    continue;
                }
                String extLogin = (String) s.get("login");
                // check uniqueness of every login in extSource
                if (uniqueLogins.contains(extLogin)) {
                    throw new InternalErrorException("There are more than 1 login '" + extLogin + "' getting from extSource '" + source + "'");
                } else {
                    uniqueLogins.add(extLogin);
                }
                // Get Candidate
                Candidate candidate;
                try {
                    if (simpleExtSource) {
                        // retrieve data about subjects from ext source based on ext. login
                        candidate = getPerunBl().getExtSourcesManagerBl().getCandidate(sess, source, extLogin);
                    } else {
                        // retrieve data about subjects from subjects we already have locally
                        candidate = getPerunBl().getExtSourcesManagerBl().getCandidate(sess, s, source, extLogin);
                    }
                } catch (ExtSourceNotExistsException e) {
                    throw new ConsistencyErrorException("Getting candidate from non-existing extSource " + source, e);
                } catch (CandidateNotExistsException e) {
                    throw new ConsistencyErrorException("findSubjects returned that candidate, but getCandidate cannot find him using login " + extLogin, e);
                } catch (ExtSourceUnsupportedOperationException e) {
                    throw new InternalErrorException("extSource supports findSubjects but not getCandidate???", e);
                }
                try {
                    getPerunBl().getMembersManagerBl().getMemberByUserExtSources(sess, vo, candidate.getUserExtSources());
                    // Candidate is already a member of the VO, so do not add him to the list of candidates
                    continue;
                } catch (MemberNotExistsException e) {
                // This is OK
                }
                // Add candidate to the list of candidates
                log.debug("findCandidates: returning candidate: {}", candidate);
                candidates.add(candidate);
                numOfResults++;
                // Stop getting new members if the number of already retrieved members exceeded the maxNumOfResults
                if (maxNumOfResults > 0 && numOfResults >= maxNumOfResults) {
                    break;
                }
            }
            // Stop walking through next sources if the number of already retrieved members exceeded the maxNumOfResults
            if (maxNumOfResults > 0 && numOfResults >= maxNumOfResults) {
                break;
            }
        }
        log.debug("Returning {} potential members for vo {}", candidates.size(), vo);
        return candidates;
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ExtSourceApi(cz.metacentrum.perun.core.implApi.ExtSourceApi) ExtSource(cz.metacentrum.perun.core.api.ExtSource) ExtSourceUnsupportedOperationException(cz.metacentrum.perun.core.api.exceptions.ExtSourceUnsupportedOperationException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) Map(java.util.Map) ExtSourceSimpleApi(cz.metacentrum.perun.core.implApi.ExtSourceSimpleApi) HashSet(java.util.HashSet) CandidateNotExistsException(cz.metacentrum.perun.core.api.exceptions.CandidateNotExistsException)

Example 3 with ExtSourceApi

use of cz.metacentrum.perun.core.implApi.ExtSourceApi in project perun by CESNET.

the class VosManagerBlImpl method findCandidates.

public List<Candidate> findCandidates(PerunSession sess, Group group, String searchString) throws InternalErrorException {
    List<Candidate> candidates = new ArrayList<>();
    try {
        // Iterate through all registered extSources in the group
        for (ExtSource source : getPerunBl().getExtSourcesManagerBl().getGroupExtSources(sess, group)) {
            // Info if this is only simple ext source, change behavior if not
            boolean simpleExtSource = true;
            // Get potential subjects from the extSource
            List<Map<String, String>> subjects;
            try {
                if (source instanceof ExtSourceApi) {
                    // find subjects with all their properties
                    subjects = ((ExtSourceApi) source).findSubjects(searchString);
                    simpleExtSource = false;
                } else {
                    // find subjects only with logins - they then must be retrieved by login
                    subjects = ((ExtSourceSimpleApi) source).findSubjectsLogins(searchString);
                }
            } catch (ExtSourceUnsupportedOperationException e1) {
                log.warn("ExtSource {} doesn't support findSubjects", source.getName());
                continue;
            } catch (InternalErrorException e) {
                log.error("Error occurred on ExtSource {},  Exception {}.", source.getName(), e);
                continue;
            } finally {
                try {
                    ((ExtSourceSimpleApi) source).close();
                } catch (ExtSourceUnsupportedOperationException e) {
                // ExtSource doesn't support that functionality, so silently skip it.
                } catch (InternalErrorException e) {
                    log.error("Can't close extSource connection. Cause: {}", e);
                }
            }
            Set<String> uniqueLogins = new HashSet<>();
            for (Map<String, String> s : subjects) {
                // Check if the user has unique identifier within extSource
                if ((s.get("login") == null) || (s.get("login") != null && ((String) s.get("login")).isEmpty())) {
                    log.error("User '{}' cannot be added, because he/she doesn't have a unique identifier (login)", s);
                    // Skip to another user
                    continue;
                }
                String extLogin = (String) s.get("login");
                // check uniqueness of every login in extSource
                if (uniqueLogins.contains(extLogin)) {
                    throw new InternalErrorException("There are more than 1 login '" + extLogin + "' getting from extSource '" + source + "'");
                } else {
                    uniqueLogins.add(extLogin);
                }
                // Get Candidate
                Candidate candidate;
                try {
                    if (simpleExtSource) {
                        // retrieve data about subjects from ext source based on ext. login
                        candidate = getPerunBl().getExtSourcesManagerBl().getCandidate(sess, source, extLogin);
                    } else {
                        // retrieve data about subjects from subjects we already have locally
                        candidate = getPerunBl().getExtSourcesManagerBl().getCandidate(sess, s, source, extLogin);
                    }
                } catch (ExtSourceNotExistsException e) {
                    throw new ConsistencyErrorException("Getting candidate from non-existing extSource " + source, e);
                } catch (CandidateNotExistsException e) {
                    throw new ConsistencyErrorException("findSubjects returned that candidate, but getCandidate cannot find him using login " + extLogin, e);
                } catch (ExtSourceUnsupportedOperationException e) {
                    throw new InternalErrorException("extSource supports findSubjects but not getCandidate???", e);
                }
                try {
                    Vo vo = getPerunBl().getVosManagerBl().getVoById(sess, group.getVoId());
                    getPerunBl().getMembersManagerBl().getMemberByUserExtSources(sess, vo, candidate.getUserExtSources());
                    // Candidate is already a member of the VO, so do not add him to the list of candidates
                    continue;
                } catch (VoNotExistsException e) {
                    throw new InternalErrorException(e);
                } catch (MemberNotExistsException e) {
                // This is OK
                }
                // Add candidate to the list of candidates
                log.debug("findCandidates: returning candidate: {}", candidate);
                candidates.add(candidate);
            }
        }
        log.debug("Returning {} potential members for group {}", candidates.size(), group);
        return candidates;
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ExtSourceApi(cz.metacentrum.perun.core.implApi.ExtSourceApi) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) Vo(cz.metacentrum.perun.core.api.Vo) ExtSource(cz.metacentrum.perun.core.api.ExtSource) ExtSourceUnsupportedOperationException(cz.metacentrum.perun.core.api.exceptions.ExtSourceUnsupportedOperationException) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) Map(java.util.Map) ExtSourceSimpleApi(cz.metacentrum.perun.core.implApi.ExtSourceSimpleApi) HashSet(java.util.HashSet) CandidateNotExistsException(cz.metacentrum.perun.core.api.exceptions.CandidateNotExistsException)

Aggregations

Candidate (cz.metacentrum.perun.core.api.Candidate)3 CandidateNotExistsException (cz.metacentrum.perun.core.api.exceptions.CandidateNotExistsException)3 ExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)3 ExtSourceUnsupportedOperationException (cz.metacentrum.perun.core.api.exceptions.ExtSourceUnsupportedOperationException)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)3 ExtSourceApi (cz.metacentrum.perun.core.implApi.ExtSourceApi)3 ExtSourceSimpleApi (cz.metacentrum.perun.core.implApi.ExtSourceSimpleApi)3 ExtSource (cz.metacentrum.perun.core.api.ExtSource)2 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)2 MemberNotExistsException (cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Vo (cz.metacentrum.perun.core.api.Vo)1 SubjectNotExistsException (cz.metacentrum.perun.core.api.exceptions.SubjectNotExistsException)1 UserExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceNotExistsException)1 VoNotExistsException (cz.metacentrum.perun.core.api.exceptions.VoNotExistsException)1