use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.
the class urn_perun_resource_attribute_def_def_defaultDataQuota method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
Attribute attrDefaultDataLimit = 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());
}
//Get DefaultDataLimit attribute
try {
attrDefaultDataLimit = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultDataLimit);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with defaultDataLimit from resource " + resource.getId() + " could not obtained.", ex);
}
//Get DefaultDataQuota value
if (attribute.getValue() != null) {
defaultDataQuota = (String) attribute.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 WrongAttributeValueException(attribute, resource, null, attribute + " can't be less than 0.");
}
//Get DefaultDataLimit value
if (attrDefaultDataLimit != null && attrDefaultDataLimit.getValue() != null) {
defaultDataLimit = (String) attrDefaultDataLimit.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 WrongReferenceAttributeValueException(attribute, attrDefaultDataLimit, resource, null, resource, null, attrDefaultDataLimit + " cant be less than 0.");
}
//Compare DefaultDataQuota with DefaultDataLimit
if (quotaNumber == null || quotaNumber.compareTo(BigDecimal.valueOf(0)) == 0) {
if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
throw new WrongReferenceAttributeValueException(attribute, attrDefaultDataLimit, resource, null, resource, null, "Try to set unlimited quota, but limit is still " + defaultDataLimitNumber + defaultDataLimitLetter);
}
} else if (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, attrDefaultDataLimit, resource, null, resource, null, attribute + " must be less than or equals to " + defaultDataLimit);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.
the class urn_perun_resource_attribute_def_def_homeMountPoints method checkAttributeValue.
/**
* Allows only homeMountPoints which are contained in underlying facility
* @param perunSession
* @param resource
* @param attribute
* @throws InternalErrorException
* @throws WrongAttributeValueException
* @throws WrongReferenceAttributeValueException
* @throws WrongAttributeAssignmentException
*/
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
if (attribute.getValue() == null) {
throw new WrongAttributeValueException(attribute);
}
Facility facility = perunSession.getPerunBl().getResourcesManagerBl().getFacility(perunSession, resource);
Attribute facilityAttr = null;
try {
facilityAttr = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, facility, A_F_homeMountPoints);
} catch (AttributeNotExistsException ex) {
throw new InternalErrorException(ex);
}
if (facilityAttr.getValue() == null)
throw new WrongReferenceAttributeValueException(attribute, facilityAttr, "Reference attribute have null value.");
if (!((List<String>) facilityAttr.getValue()).containsAll((List<String>) attribute.getValue())) {
throw new WrongAttributeValueException(attribute);
}
List<String> homeMountPoints = (List<String>) attribute.getValue();
if (!homeMountPoints.isEmpty()) {
Pattern pattern = Pattern.compile("^/[-a-zA-Z.0-9_/]*$");
for (String st : homeMountPoints) {
Matcher match = pattern.matcher(st);
if (!match.matches()) {
throw new WrongAttributeValueException(attribute, "Bad homeMountPoints attribute format " + st);
}
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.
the class urn_perun_resource_attribute_def_def_k5loginTargetUser method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
if (attribute.getValue() == null) {
throw new WrongAttributeValueException(attribute, resource, null, "Attribute value can't be null.");
}
String targetUser = (String) attribute.getValue();
Matcher userMatcher = userPattern.matcher(targetUser);
if (!userMatcher.matches()) {
throw new WrongAttributeValueException(attribute, resource, null, "Matcher must match to ^[-a-zA-Z0-9_]+$ pattern.");
}
}
use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.
the class urn_perun_resource_attribute_def_def_mailaliasesTargetUser method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
if (attribute.getValue() == null) {
throw new WrongAttributeValueException(attribute, resource, null, "Attribute value can't be null.");
}
String targetUser = (String) attribute.getValue();
Matcher userMatcher = userPattern.matcher(targetUser);
if (!userMatcher.matches()) {
throw new WrongAttributeValueException(attribute, resource, null, "Matcher must match to ^[-a-zA-Z0-9_]+$ pattern.");
}
}
use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.
the class urn_perun_member_resource_attribute_def_def_filesLimit method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Member member, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
Attribute attrFilesQuota = null;
Integer filesQuota = null;
Integer filesLimit = null;
//Get FilesQuotaAttribute
try {
attrFilesQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, member, A_MR_filesQuota);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with filesQuota from member " + member.getId() + " and resource " + resource.getId() + " could not obtained.", ex);
}
//Get FilesLimit value
if (attribute.getValue() != null) {
filesLimit = (Integer) attribute.getValue();
} else {
try {
attribute = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultFilesLimit);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with defaultFilesLimit from resource " + resource.getId() + " could not obtained.", ex);
}
if (attribute != null && attribute.getValue() != null) {
filesLimit = (Integer) attribute.getValue();
}
}
if (filesLimit != null && filesLimit < 0)
throw new WrongAttributeValueException(attribute, resource, member, attribute + " cannot be less than 0.");
//Get FilesQuota value
if (attrFilesQuota != null && attrFilesQuota.getValue() != null) {
filesQuota = (Integer) attrFilesQuota.getValue();
} else if (attrFilesQuota == null || attrFilesQuota.getValue() == null) {
try {
attrFilesQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultFilesQuota);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with defaultFilesQuota from resource " + resource.getId() + " could not obtained.", ex);
}
if (attrFilesQuota != null || attrFilesQuota.getValue() != null) {
filesQuota = (Integer) attrFilesQuota.getValue();
}
}
if (filesQuota != null && filesQuota < 0)
throw new ConsistencyErrorException(attrFilesQuota + " cannot be less than 0.");
//Compare FilesLimit with FilesQuota
if (filesQuota == null || filesQuota == 0) {
if (filesLimit != null && filesLimit != 0)
throw new WrongReferenceAttributeValueException(attribute, attrFilesQuota, resource, member, resource, null, "Try to set limited limit, but there is still set unlimited Quota.");
} else if ((filesQuota != null && filesQuota != 0) && (filesLimit != null && filesLimit != 0)) {
if (filesLimit < filesQuota)
throw new WrongReferenceAttributeValueException(attribute, attrFilesQuota, resource, member, resource, null, attribute + " must be more than or equals to " + attrFilesQuota);
}
}
Aggregations