Search in sources :

Example 1 with ParserException

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

the class ExtSourcesManagerBlImpl method getCandidate.

public Candidate getCandidate(PerunSession perunSession, Map<String, String> subjectData, ExtSource source, String login) throws InternalErrorException, ExtSourceNotExistsException, CandidateNotExistsException, ExtSourceUnsupportedOperationException {
    if (login == null || login.isEmpty())
        throw new InternalErrorException("Login can't be empty or null.");
    if (subjectData == null || subjectData.isEmpty())
        throw new InternalErrorException("Subject data can't be null or empty, at least login there must exists.");
    // New Canddate
    Candidate candidate = new Candidate();
    // Prepare userExtSource object
    UserExtSource userExtSource = new UserExtSource();
    userExtSource.setExtSource(source);
    userExtSource.setLogin(login);
    // Set the userExtSource
    candidate.setUserExtSource(userExtSource);
    //If first name of candidate is not in format of name, set null instead
    candidate.setFirstName(subjectData.get("firstName"));
    if (candidate.getFirstName() != null) {
        Matcher name = namePattern.matcher(candidate.getFirstName());
        if (!name.matches())
            candidate.setFirstName(null);
    }
    //If last name of candidate is not in format of name, set null instead
    candidate.setLastName(subjectData.get("lastName"));
    if (candidate.getLastName() != null) {
        Matcher name = namePattern.matcher(candidate.getLastName());
        if (!name.matches())
            candidate.setLastName(null);
    }
    candidate.setMiddleName(subjectData.get("middleName"));
    candidate.setTitleAfter(subjectData.get("titleAfter"));
    candidate.setTitleBefore(subjectData.get("titleBefore"));
    //Set service user
    if (subjectData.get("isServiceUser") == null) {
        candidate.setServiceUser(false);
    } else {
        String isServiceUser = subjectData.get("isServiceUser");
        if (isServiceUser.equals("true")) {
            candidate.setServiceUser(true);
        } else {
            candidate.setServiceUser(false);
        }
    }
    //Set sponsored user
    if (subjectData.get("isSponsoredUser") == null) {
        candidate.setSponsoredUser(false);
    } else {
        String isSponsoredUser = subjectData.get("isSponsoredUser");
        if (isSponsoredUser.equals("true")) {
            candidate.setSponsoredUser(true);
        } else {
            candidate.setSponsoredUser(false);
        }
    }
    // Additional userExtSources
    List<UserExtSource> additionalUserExtSources = new ArrayList<UserExtSource>();
    // Filter attributes
    Map<String, String> attributes = new HashMap<String, String>();
    for (String attrName : subjectData.keySet()) {
        // FIXME volat metody z attributesManagera nez kontrolovat na zacatek jmena
        if (attrName.startsWith(AttributesManager.NS_MEMBER_ATTR) || attrName.startsWith(AttributesManager.NS_USER_ATTR)) {
            attributes.put(attrName, subjectData.get(attrName));
        } else if (attrName.startsWith(ExtSourcesManagerImpl.USEREXTSOURCEMAPPING)) {
            //skip null additional ext sources
            if (subjectData.get(attrName) == null)
                continue;
            // Add additionalUserExtSources
            // Entry contains extSourceName|extSourceType|extLogin[|LoA]
            String[] userExtSourceRaw = subjectData.get(attrName).split("\\|");
            log.debug("Processing additionalUserExtSource {}", subjectData.get(attrName));
            //Check if the array has at least 3 parts, this is protection against outOfBoundException
            if (userExtSourceRaw.length < 3) {
                throw new InternalErrorException("There is missing some mandatory part of additional user extSource value when processing it - '" + attrName + "'");
            }
            String additionalExtSourceName = userExtSourceRaw[0];
            String additionalExtSourceType = userExtSourceRaw[1];
            String additionalExtLogin = userExtSourceRaw[2];
            int additionalExtLoa = 0;
            //Loa is not mandatory argument
            if (userExtSourceRaw.length > 3 && userExtSourceRaw[3] != null) {
                try {
                    additionalExtLoa = Integer.parseInt(userExtSourceRaw[3]);
                } catch (NumberFormatException e) {
                    throw new ParserException("Candidate with login [" + login + "] has wrong LoA '" + userExtSourceRaw[3] + "'.", e, "LoA");
                }
            }
            ExtSource additionalExtSource;
            if (additionalExtSourceName == null || additionalExtSourceName.isEmpty() || additionalExtSourceType == null || additionalExtSourceType.isEmpty() || additionalExtLogin == null || additionalExtLogin.isEmpty()) {
                log.error("User with login {} has invalid additional userExtSource defined {}.", login, userExtSourceRaw);
            } else {
                try {
                    // Try to get extSource, with full extSource object (containg ID)
                    additionalExtSource = getPerunBl().getExtSourcesManagerBl().getExtSourceByName(perunSession, additionalExtSourceName);
                } catch (ExtSourceNotExistsException e) {
                    try {
                        // Create new one if not exists
                        additionalExtSource = new ExtSource(additionalExtSourceName, additionalExtSourceType);
                        additionalExtSource = getPerunBl().getExtSourcesManagerBl().createExtSource(perunSession, additionalExtSource, null);
                    } catch (ExtSourceExistsException e1) {
                        throw new ConsistencyErrorException("Creating existin extSource: " + additionalExtSourceName);
                    }
                }
                //add additional user extSource
                additionalUserExtSources.add(new UserExtSource(additionalExtSource, additionalExtLoa, additionalExtLogin));
            }
        }
    }
    candidate.setAdditionalUserExtSources(additionalUserExtSources);
    candidate.setAttributes(attributes);
    return candidate;
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) ParserException(cz.metacentrum.perun.core.api.exceptions.ParserException) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceExistsException) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)

Example 2 with ParserException

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

the class ExtSourcesManagerBlImpl method getCandidate.

@Override
public Candidate getCandidate(PerunSession sess, ExtSource source, String login) throws InternalErrorException, ExtSourceNotExistsException, CandidateNotExistsException, ExtSourceUnsupportedOperationException {
    // New Canddate
    Candidate candidate = new Candidate();
    // Prepare userExtSource object
    UserExtSource userExtSource = new UserExtSource();
    userExtSource.setExtSource(source);
    userExtSource.setLogin(login);
    // Set the userExtSource
    candidate.setUserExtSource(userExtSource);
    // Get the subject from the extSource
    Map<String, String> subject = null;
    try {
        subject = ((ExtSourceSimpleApi) source).getSubjectByLogin(login);
    } catch (SubjectNotExistsException e) {
        throw new CandidateNotExistsException(login);
    }
    if (subject == null) {
        throw new CandidateNotExistsException("Candidate with login [" + login + "] not exists");
    }
    //If first name of candidate is not in format of name, set null instead
    candidate.setFirstName(subject.get("firstName"));
    if (candidate.getFirstName() != null) {
        Matcher name = namePattern.matcher(candidate.getFirstName());
        if (!name.matches())
            candidate.setFirstName(null);
    }
    //If last name of candidate is not in format of name, set null instead
    candidate.setLastName(subject.get("lastName"));
    if (candidate.getLastName() != null) {
        Matcher name = namePattern.matcher(candidate.getLastName());
        if (!name.matches())
            candidate.setLastName(null);
    }
    candidate.setMiddleName(subject.get("middleName"));
    candidate.setTitleAfter(subject.get("titleAfter"));
    candidate.setTitleBefore(subject.get("titleBefore"));
    //Set service user
    if (subject.get("isServiceUser") == null) {
        candidate.setServiceUser(false);
    } else {
        String isServiceUser = subject.get("isServiceUser");
        if (isServiceUser.equals("true")) {
            candidate.setServiceUser(true);
        } else {
            candidate.setServiceUser(false);
        }
    }
    //Set sponsored user
    if (subject.get("isSponsoredUser") == null) {
        candidate.setSponsoredUser(false);
    } else {
        String isSponsoredUser = subject.get("isSponsoredUser");
        if (isSponsoredUser.equals("true")) {
            candidate.setSponsoredUser(true);
        } else {
            candidate.setSponsoredUser(false);
        }
    }
    // Additional userExtSources
    List<UserExtSource> additionalUserExtSources = new ArrayList<UserExtSource>();
    // Filter attributes
    Map<String, String> attributes = new HashMap<String, String>();
    for (String attrName : subject.keySet()) {
        // FIXME volat metody z attributesManagera nez kontrolovat na zacatek jmena
        if (attrName.startsWith(AttributesManager.NS_MEMBER_ATTR) || attrName.startsWith(AttributesManager.NS_USER_ATTR)) {
            attributes.put(attrName, subject.get(attrName));
        } else if (attrName.startsWith(ExtSourcesManagerImpl.USEREXTSOURCEMAPPING)) {
            //skip null additional ext sources
            if (subject.get(attrName) == null)
                continue;
            // Add additionalUserExtSources
            // Entry contains extSourceName|extSourceType|extLogin[|LoA]
            String[] userExtSourceRaw = subject.get(attrName).split("\\|");
            log.debug("Processing additionalUserExtSource {}", subject.get(attrName));
            //Check if the array has at least 3 parts, this is protection against outOfBoundException
            if (userExtSourceRaw.length < 3) {
                throw new InternalErrorException("There is missing some mandatory part of additional user extSource value when processing it - '" + attrName + "'");
            }
            String additionalExtSourceName = userExtSourceRaw[0];
            String additionalExtSourceType = userExtSourceRaw[1];
            String additionalExtLogin = userExtSourceRaw[2];
            int additionalExtLoa = 0;
            //Loa is not mandatory argument
            if (userExtSourceRaw.length > 3 && userExtSourceRaw[3] != null) {
                try {
                    additionalExtLoa = Integer.parseInt(userExtSourceRaw[3]);
                } catch (NumberFormatException e) {
                    throw new ParserException("Candidate with login [" + login + "] has wrong LoA '" + userExtSourceRaw[3] + "'.", e, "LoA");
                }
            }
            ExtSource additionalExtSource;
            if (additionalExtSourceName == null || additionalExtSourceName.isEmpty() || additionalExtSourceType == null || additionalExtSourceType.isEmpty() || additionalExtLogin == null || additionalExtLogin.isEmpty()) {
                log.error("User with login {} has invalid additional userExtSource defined {}.", login, userExtSourceRaw);
            } else {
                try {
                    // Try to get extSource, with full extSource object (containg ID)
                    additionalExtSource = getPerunBl().getExtSourcesManagerBl().getExtSourceByName(sess, additionalExtSourceName);
                } catch (ExtSourceNotExistsException e) {
                    try {
                        // Create new one if not exists
                        additionalExtSource = new ExtSource(additionalExtSourceName, additionalExtSourceType);
                        additionalExtSource = getPerunBl().getExtSourcesManagerBl().createExtSource(sess, additionalExtSource, null);
                    } catch (ExtSourceExistsException e1) {
                        throw new ConsistencyErrorException("Creating existin extSource: " + additionalExtSourceName);
                    }
                }
                //add additional user extSource
                additionalUserExtSources.add(new UserExtSource(additionalExtSource, additionalExtLoa, additionalExtLogin));
            }
        }
    }
    candidate.setAdditionalUserExtSources(additionalUserExtSources);
    candidate.setAttributes(attributes);
    return candidate;
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) ParserException(cz.metacentrum.perun.core.api.exceptions.ParserException) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSourceExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceExistsException) SubjectNotExistsException(cz.metacentrum.perun.core.api.exceptions.SubjectNotExistsException) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ExtSourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException) CandidateNotExistsException(cz.metacentrum.perun.core.api.exceptions.CandidateNotExistsException)

Aggregations

Candidate (cz.metacentrum.perun.core.api.Candidate)2 ExtSource (cz.metacentrum.perun.core.api.ExtSource)2 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)2 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)2 ExtSourceExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceExistsException)2 ExtSourceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ExtSourceNotExistsException)2 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 ParserException (cz.metacentrum.perun.core.api.exceptions.ParserException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Matcher (java.util.regex.Matcher)2 CandidateNotExistsException (cz.metacentrum.perun.core.api.exceptions.CandidateNotExistsException)1 SubjectNotExistsException (cz.metacentrum.perun.core.api.exceptions.SubjectNotExistsException)1