Search in sources :

Example 51 with WrongAttributeValueException

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

the class urn_perun_resource_attribute_def_def_defaultDataLimit method checkAttributeValue.

@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    Attribute attrDefaultDataQuota = null;
    String defaultDataQuota = null;
    String defaultDataLimit = null;
    String defaultDataQuotaNumber = null;
    String defaultDataQuotaLetter = null;
    String defaultDataLimitNumber = null;
    String defaultDataLimitLetter = null;
    //Check if attribute value has the right exp pattern (can be null)
    if (attribute.getValue() != null) {
        Matcher testMatcher = testingPattern.matcher((String) attribute.getValue());
        if (!testMatcher.find())
            throw new WrongAttributeValueException(attribute, resource, "Format of quota must be something like ex.: 1.30M or 2500K, but it is " + attribute.getValue());
    } else
        return;
    //Get DefaultDataQuota attribute
    try {
        attrDefaultDataQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultDataQuota);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute with defaultDataQuota from resource " + resource.getId() + " could not obtained.", ex);
    }
    //Get DefaultDataLimit value
    if (attribute.getValue() != null) {
        defaultDataLimit = (String) attribute.getValue();
        Matcher numberMatcher = numberPattern.matcher(defaultDataLimit);
        Matcher letterMatcher = letterPattern.matcher(defaultDataLimit);
        numberMatcher.find();
        letterMatcher.find();
        try {
            defaultDataLimitNumber = defaultDataLimit.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            defaultDataLimitNumber = null;
        }
        try {
            defaultDataLimitLetter = defaultDataLimit.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            defaultDataLimitLetter = "G";
        }
    }
    BigDecimal limitNumber;
    if (defaultDataLimitNumber != null)
        limitNumber = new BigDecimal(defaultDataLimitNumber.replace(',', '.'));
    else
        limitNumber = new BigDecimal("0");
    if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongAttributeValueException(attribute, resource, attribute + " can't be less than 0.");
    }
    //Get DefaultDataQuota value
    if (attrDefaultDataQuota != null && attrDefaultDataQuota.getValue() != null) {
        defaultDataQuota = (String) attrDefaultDataQuota.getValue();
        Matcher numberMatcher = numberPattern.matcher(defaultDataQuota);
        Matcher letterMatcher = letterPattern.matcher(defaultDataQuota);
        numberMatcher.find();
        letterMatcher.find();
        try {
            defaultDataQuotaNumber = defaultDataQuota.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            defaultDataQuotaNumber = null;
        }
        try {
            defaultDataQuotaLetter = defaultDataQuota.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            defaultDataQuotaLetter = "G";
        }
    }
    BigDecimal quotaNumber;
    if (defaultDataQuotaNumber != null)
        quotaNumber = new BigDecimal(defaultDataQuotaNumber.replace(',', '.'));
    else
        quotaNumber = new BigDecimal("0");
    if (quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongReferenceAttributeValueException(attribute, attrDefaultDataQuota, resource, null, resource, null, attrDefaultDataQuota + " cant be less than 0.");
    }
    //Compare DefaultDataLimit with DefaultDataQuota
    if (quotaNumber == null || quotaNumber.compareTo(BigDecimal.valueOf(0)) == 0) {
        if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrDefaultDataQuota, resource, null, resource, null, "Try to set limited limit, but there is still set unlimited Quota.");
        }
    } else if ((quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) != 0) && (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0)) {
        if (defaultDataLimitLetter.equals("K"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(K));
        else if (defaultDataLimitLetter.equals("M"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(M));
        else if (defaultDataLimitLetter.equals("T"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(T));
        else if (defaultDataLimitLetter.equals("P"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(P));
        else if (defaultDataLimitLetter.equals("E"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(E));
        else
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(G));
        if (defaultDataQuotaLetter.equals("K"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(K));
        else if (defaultDataQuotaLetter.equals("M"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(M));
        else if (defaultDataQuotaLetter.equals("T"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(T));
        else if (defaultDataQuotaLetter.equals("P"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(P));
        else if (defaultDataQuotaLetter.equals("E"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(E));
        else
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(G));
        if (limitNumber.compareTo(quotaNumber) < 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrDefaultDataQuota, resource, null, resource, null, attribute + " must be more than or equals to " + attrDefaultDataQuota);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) Matcher(java.util.regex.Matcher) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) BigDecimal(java.math.BigDecimal)

Example 52 with WrongAttributeValueException

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

the class urn_perun_resource_attribute_def_def_fairshareGroupName method checkAttributeValue.

public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    //Null is ok, it means this resource is not fairshare group
    if (attribute.getValue() == null) {
        return;
    }
    String gName = (String) attribute.getValue();
    //Test if gName matchers regex
    Matcher matcher = pattern.matcher(gName);
    if (!matcher.matches()) {
        throw new WrongAttributeValueException(attribute, resource, "Wrong format of group fairshare name. Max length is 12, only letters are allowed.");
    }
    //On facility must be fairshare group name unique (between all resources of this facility)
    Facility facility = perunSession.getPerunBl().getResourcesManagerBl().getFacility(perunSession, resource);
    List<Resource> facilityResources = perunSession.getPerunBl().getFacilitiesManagerBl().getAssignedResources(perunSession, facility);
    facilityResources.remove(resource);
    List<String> resourcesFairshareGroupNames = new ArrayList<>();
    for (Resource res : facilityResources) {
        try {
            Attribute resFairshareName = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, res, attribute.getName());
            if (resFairshareName.getValue() == null)
                continue;
            resourcesFairshareGroupNames.add((String) resFairshareName.getValue());
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException(ex);
        }
    }
    if (resourcesFairshareGroupNames.contains(gName))
        throw new WrongAttributeValueException(attribute, resource, "This name is already taken (not unique). Choose another one.");
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Matcher(java.util.regex.Matcher) Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) ArrayList(java.util.ArrayList) Facility(cz.metacentrum.perun.core.api.Facility) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 53 with WrongAttributeValueException

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

the class urn_perun_user_attribute_def_def_userCertDNs method checkAttributeValue.

public void checkAttributeValue(PerunSessionImpl sess, User user, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongAttributeAssignmentException, WrongReferenceAttributeValueException {
    if (attribute.getValue() == null)
        throw new WrongAttributeValueException(attribute, user, "This attribute value can't be null");
    Map<String, String> value = (Map) attribute.getValue();
    if (value.isEmpty())
        throw new WrongAttributeValueException(attribute, "This attribute value can't be empty");
    Set<String> valueKeys = value.keySet();
    for (String k : valueKeys) {
        Matcher certKeyMatcher = certPattern.matcher(k);
        if (!certKeyMatcher.find())
            throw new WrongAttributeValueException(attribute, "There is wrong value for key " + k + " in hashMap of userCertDNs.");
        String valueOfKey = value.get(k);
        Matcher certValueOfKeyMatcher = certPattern.matcher(valueOfKey);
        if (!certValueOfKeyMatcher.find())
            throw new WrongAttributeValueException(attribute, "There is wrong value for key's value " + valueOfKey + " in hashMap of UserCertDns for key " + k);
    }
}
Also used : Matcher(java.util.regex.Matcher) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 54 with WrongAttributeValueException

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

the class urn_perun_user_attribute_def_def_userCertDNs method changedAttributeHook.

@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    Attribute userPreferredCertDN = null;
    try {
        userPreferredCertDN = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, AttributesManager.NS_USER_ATTR_DEF + ":userPreferredCertDN");
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    } catch (WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
    String preferredCertDNValue = null;
    if (userPreferredCertDN.getValue() != null)
        preferredCertDNValue = (String) userPreferredCertDN.getValue();
    Map<String, String> certDNs = null;
    if (attribute.getValue() != null)
        certDNs = (Map<String, String>) attribute.getValue();
    if (certDNs == null || certDNs.isEmpty()) {
        try {
            session.getPerunBl().getAttributesManagerBl().removeAttribute(session, user, userPreferredCertDN);
        } catch (WrongAttributeAssignmentException ex) {
            throw new InternalErrorException(ex);
        } catch (WrongAttributeValueException ex) {
            throw new InternalErrorException(ex);
        }
    } else {
        Set<String> certDNsKeys = certDNs.keySet();
        String newPossibleCertDN = null;
        for (String key : certDNsKeys) {
            if (key != null && !key.isEmpty()) {
                newPossibleCertDN = key;
                break;
            }
        }
        if (preferredCertDNValue == null) {
            userPreferredCertDN.setValue(newPossibleCertDN);
            try {
                session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredCertDN);
            } catch (WrongAttributeAssignmentException ex) {
                throw new InternalErrorException(ex);
            } catch (WrongAttributeValueException ex) {
                throw new InternalErrorException(ex);
            }
        } else {
            if (!certDNsKeys.contains(preferredCertDNValue)) {
                userPreferredCertDN.setValue(newPossibleCertDN);
                try {
                    session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredCertDN);
                } catch (WrongAttributeAssignmentException ex) {
                    throw new InternalErrorException(ex);
                } catch (WrongAttributeValueException ex) {
                    throw new InternalErrorException(ex);
                }
            }
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 55 with WrongAttributeValueException

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

the class urn_perun_user_attribute_def_def_vsupMail method changedAttributeHook.

@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    // map of reserved vsup mails
    Attribute reservedMailsAttribute;
    Map<String, String> reservedMailsAttributeValue;
    // other vsup mail attributes to get values from
    Attribute mailAliasAttribute;
    Attribute mailAliasesAttribute;
    Attribute vsupPreferredMailAttribute;
    // output sets used for comparison
    Set<String> reservedMailsOfUser = new HashSet<>();
    Set<String> actualMailsOfUser = new HashSet<>();
    try {
        reservedMailsAttribute = session.getPerunBl().getAttributesManagerBl().getEntitylessAttributeForUpdate(session, usedMailsKeyVsup, usedMailsUrn);
        mailAliasAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupMailAliasUrn);
        mailAliasesAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupMailAliasesUrn);
        vsupPreferredMailAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupPreferredMailUrn);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute doesn't exists.", ex);
    } catch (WrongAttributeAssignmentException e) {
        throw new InternalErrorException(e);
    }
    if (attribute.getValue() == null && reservedMailsAttribute.getValue() == null) {
        throw new ConsistencyErrorException("Entityless attribute 'urn:perun:entityless:attribute-def:def:usedMails' is empty, but we are removing 'vsupMail' value, so there should have been entry in entityless attribute.");
    }
    if (reservedMailsAttribute.getValue() == null) {
        reservedMailsAttributeValue = new LinkedHashMap<>();
    } else {
        reservedMailsAttributeValue = (Map<String, String>) reservedMailsAttribute.getValue();
    }
    // if SET action and mail is already reserved by other user
    if (attribute.getValue() != null) {
        String ownersUserId = reservedMailsAttributeValue.get((String) attribute.getValue());
        if (ownersUserId != null && !Objects.equals(ownersUserId, String.valueOf(user.getId()))) {
            // TODO - maybe get actual owners attribute and throw WrongReferenceAttributeException to be nice in a GUI ?
            throw new InternalErrorException("VŠUP mail: '" + attribute.getValue() + "' is already in use by User ID: " + ownersUserId + ".");
        }
    }
    for (Map.Entry<String, String> entry : reservedMailsAttributeValue.entrySet()) {
        if (Objects.equals(entry.getValue(), String.valueOf(user.getId()))) {
            // reserved mails of a user
            reservedMailsOfUser.add(entry.getKey());
        }
    }
    if (mailAliasAttribute.getValue() != null) {
        actualMailsOfUser.add((String) mailAliasAttribute.getValue());
    }
    if (vsupPreferredMailAttribute.getValue() != null) {
        actualMailsOfUser.add((String) vsupPreferredMailAttribute.getValue());
    }
    if (mailAliasesAttribute.getValue() != null) {
        actualMailsOfUser.addAll((ArrayList<String>) mailAliasesAttribute.getValue());
    }
    for (String mail : reservedMailsOfUser) {
        if (!actualMailsOfUser.contains(mail)) {
            // Remove mail, which is not in attributes anymore
            reservedMailsAttributeValue.remove(mail);
            // since this attribute holds single value, we can break the cycle here
            break;
        }
    }
    // Put in which is in attribute but not in a map
    if (attribute.getValue() != null) {
        reservedMailsAttributeValue.putIfAbsent((String) attribute.getValue(), String.valueOf(user.getId()));
    }
    // save changes in entityless attribute
    try {
        // always set value to attribute, since we might start with null in attribute and empty map in variable !!
        reservedMailsAttribute.setValue(reservedMailsAttributeValue);
        session.getPerunBl().getAttributesManagerBl().setAttribute(session, usedMailsKeyVsup, reservedMailsAttribute);
    } catch (WrongAttributeValueException | WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
    // if set, check vsupPreferredMail - if is empty, set vsupMail to vsupPreferredMail
    if (vsupPreferredMailAttribute.getValue() == null && attribute.getValue() != null) {
        vsupPreferredMailAttribute.setValue(attribute.getValue());
        try {
            session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, vsupPreferredMailAttribute);
        } catch (WrongAttributeValueException | WrongAttributeAssignmentException e) {
            throw new InternalErrorException("Unable to store generated vsupMail to vsupPreferredMail.", e);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)135 Attribute (cz.metacentrum.perun.core.api.Attribute)100 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)83 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)82 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)75 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)66 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)54 Matcher (java.util.regex.Matcher)31 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)27 ArrayList (java.util.ArrayList)17 LinkedHashMap (java.util.LinkedHashMap)16 Facility (cz.metacentrum.perun.core.api.Facility)14 Map (java.util.Map)14 Resource (cz.metacentrum.perun.core.api.Resource)12 User (cz.metacentrum.perun.core.api.User)11 Group (cz.metacentrum.perun.core.api.Group)9 Pattern (java.util.regex.Pattern)8 BigDecimal (java.math.BigDecimal)7 List (java.util.List)7 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)5