Search in sources :

Example 56 with WrongReferenceAttributeValueException

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

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

the class urn_perun_user_attribute_def_def_userPreferredCertDN method changedAttributeHook.

//TODO what dependencies of this attribute???
@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    if (attribute.getValue() == null) {
        Attribute userCertDNs = null;
        try {
            userCertDNs = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, AttributesManager.NS_USER_ATTR_DEF + ":userCertDNs");
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException(ex);
        } catch (WrongAttributeAssignmentException ex) {
            throw new InternalErrorException(ex);
        }
        Map<String, String> certDNsValue = null;
        if (userCertDNs.getValue() != null) {
            certDNsValue = (Map<String, String>) userCertDNs.getValue();
        }
        if (certDNsValue != null && !certDNsValue.isEmpty()) {
            throw new WrongReferenceAttributeValueException(attribute, "Can't remove preferredCert if there is any existing certDNs for the user.");
        }
    }
}
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) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 58 with WrongReferenceAttributeValueException

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

the class urn_perun_user_facility_attribute_def_virt_login method setAttributeValue.

@Override
public boolean setAttributeValue(PerunSessionImpl sess, Facility facility, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    AttributeDefinition userLoginAttributeDefinition;
    try {
        // Get the f:login-namespace attribute
        Attribute loginNamespaceAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":login-namespace");
        if (loginNamespaceAttribute.getValue() == null) {
            throw new WrongReferenceAttributeValueException(attribute, loginNamespaceAttribute, user, facility, "Facility need to have nonempty login-namespace attribute.");
        }
        userLoginAttributeDefinition = sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_USER_ATTR_DEF + ":login-namespace:" + (String) loginNamespaceAttribute.getValue());
    } catch (AttributeNotExistsException e) {
        throw new InternalErrorException(e);
    } catch (WrongAttributeAssignmentException e) {
        throw new ConsistencyErrorException(e);
    }
    Attribute userLoginAttribute = new Attribute(userLoginAttributeDefinition);
    userLoginAttribute.setValue(attribute.getValue());
    try {
        return sess.getPerunBl().getAttributesManagerBl().setAttributeWithoutCheck(sess, user, userLoginAttribute);
    } catch (WrongAttributeValueException e) {
        throw new InternalErrorException(e);
    } catch (WrongAttributeAssignmentException e) {
        throw new ConsistencyErrorException(e);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 59 with WrongReferenceAttributeValueException

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

the class urn_perun_user_facility_attribute_def_virt_UID method setAttributeValue.

@Override
public boolean setAttributeValue(PerunSessionImpl sess, Facility facility, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    AttributeDefinition userUidAttributeDefinition;
    try {
        // Get the f:uid-namespace attribute
        Attribute uidNamespaceAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":uid-namespace");
        if (uidNamespaceAttribute.getValue() == null) {
            throw new WrongReferenceAttributeValueException(attribute, uidNamespaceAttribute);
        }
        userUidAttributeDefinition = sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_USER_ATTR_DEF + ":uid-namespace:" + (String) uidNamespaceAttribute.getValue());
    } catch (AttributeNotExistsException e) {
        throw new InternalErrorException(e);
    } catch (WrongAttributeAssignmentException e) {
        throw new ConsistencyErrorException(e);
    }
    Attribute userUidAttribute = new Attribute(userUidAttributeDefinition);
    userUidAttribute.setValue(attribute.getValue());
    try {
        return sess.getPerunBl().getAttributesManagerBl().setAttributeWithoutCheck(sess, user, userUidAttribute);
    } catch (WrongAttributeValueException e) {
        throw new InternalErrorException(e);
    } catch (WrongAttributeAssignmentException e) {
        throw new ConsistencyErrorException(e);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 60 with WrongReferenceAttributeValueException

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

the class AttributesManagerBlImpl method removeAttributeWithoutCheck.

// s workWithUserAttr.
public boolean removeAttributeWithoutCheck(PerunSession sess, Member member, Group group, AttributeDefinition attribute) throws InternalErrorException, WrongAttributeAssignmentException {
    getAttributesManagerImpl().checkNamespace(sess, attribute, NS_MEMBER_GROUP_ATTR);
    if (getAttributesManagerImpl().isCoreAttribute(sess, attribute))
        throw new WrongAttributeAssignmentException(attribute);
    boolean changed = getAttributesManagerImpl().removeAttribute(sess, member, group, attribute);
    if (changed) {
        try {
            getAttributesManagerImpl().changedAttributeHook(sess, member, group, new Attribute(attribute));
        } catch (WrongAttributeValueException ex) {
            //TODO better exception here
            throw new InternalErrorException(ex);
        } catch (WrongReferenceAttributeValueException ex) {
            //TODO better exception here
            throw new InternalErrorException(ex);
        }
        getPerunBl().getAuditer().log(sess, "{} removed for {} and {}", attribute, member, group);
    }
    return changed;
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) RichAttribute(cz.metacentrum.perun.core.api.RichAttribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Aggregations

WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)109 Attribute (cz.metacentrum.perun.core.api.Attribute)101 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)83 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)70 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)64 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)62 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)51 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)27 Resource (cz.metacentrum.perun.core.api.Resource)13 ArrayList (java.util.ArrayList)13 Facility (cz.metacentrum.perun.core.api.Facility)12 Matcher (java.util.regex.Matcher)12 User (cz.metacentrum.perun.core.api.User)11 Group (cz.metacentrum.perun.core.api.Group)9 BigDecimal (java.math.BigDecimal)6 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)5 List (java.util.List)5 Pattern (java.util.regex.Pattern)5 Pair (cz.metacentrum.perun.core.api.Pair)3 LinkedHashMap (java.util.LinkedHashMap)3