use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_resource_attribute_def_def_defaultShellTest method fillAttributeWhichNotExists.
@Test
public void fillAttributeWhichNotExists() throws Exception {
System.out.println("fillAttributeWhichNotExists()");
//testujeme scenar, kdy budeme hledat neexistujici atribut a proto ocekavame vyjimku AttrNotExists..
when(ps.getPerunBl().getAttributesManagerBl().getAttribute(any(PerunSession.class), any(Resource.class), anyString())).thenThrow(new AttributeNotExistsException("neexistuje"));
try {
defShellAttr.fillAttribute(ps, new Resource(), new AttributeDefinition());
fail();
} catch (InternalErrorException ex) {
assertTrue("Mela byt vyhozena vyjimka AttributeNotExistsException", (ex.getCause() instanceof AttributeNotExistsException));
}
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_resource_attribute_def_def_defaultShellTest method checkAttributeValueWhichNotExists.
@Test
public void checkAttributeValueWhichNotExists() throws Exception {
System.out.println("checkAttributeValueWhichNotExists()");
// hledame neexistujici atribut, proto ocekavame vyjimku
when(ps.getPerunBl().getAttributesManagerBl().getAttribute(any(PerunSession.class), any(Resource.class), anyString())).thenThrow(new AttributeNotExistsException("neexistuje"));
final Attribute attribute = new Attribute();
attribute.setValue("mujShell");
try {
defShellAttr.checkAttributeValue(ps, new Resource(), attribute);
fail();
} catch (InternalErrorException ex) {
assertTrue("Mela byt vyhozena vyjimka AttributeNotExistsException", (ex.getCause() instanceof AttributeNotExistsException));
}
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class AttributesManagerBlImpl method doTheMagic.
public void doTheMagic(PerunSession sess, Member member, boolean trueMagic) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
User user = getPerunBl().getUsersManagerBl().getUserByMember(sess, member);
List<Resource> resources = getPerunBl().getResourcesManagerBl().getAllowedResources(sess, member);
for (Resource resource : resources) {
Facility facility = getPerunBl().getResourcesManagerBl().getFacility(sess, resource);
List<Attribute> requiredAttributes = getResourceRequiredAttributes(sess, resource, facility, resource, user, member);
boolean allOk = false;
AttributeDefinition lastWrongAttribute = null;
int safetyCounter = 0;
do {
try {
setRequiredAttributes(sess, facility, resource, user, member, requiredAttributes);
allOk = true;
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
} catch (WrongAttributeAssignmentException ex) {
throw new ConsistencyErrorException(ex);
} catch (WrongAttributeValueException ex) {
if (!trueMagic)
throw ex;
AttributeDefinition wrongAttributeDefinition = ex.getAttribute();
if (wrongAttributeDefinition == null)
throw new ConsistencyErrorException("WrongAttributeValueException doesn't have set the wrong attribute.", ex);
if (wrongAttributeDefinition.equals(lastWrongAttribute))
throw new WrongAttributeValueException("Method doTheMagic can't fix this attribute value", ex);
lastWrongAttribute = wrongAttributeDefinition;
findAndSetValueInList(requiredAttributes, wrongAttributeDefinition, null);
} catch (WrongReferenceAttributeValueException ex) {
if (!trueMagic)
throw ex;
AttributeDefinition wrongAttributeDefinition = ex.getReferenceAttribute();
if (wrongAttributeDefinition == null)
throw new ConsistencyErrorException("WrongReferenceAttributeValueException doesn't have set reference attribute.", ex);
if (wrongAttributeDefinition.equals(lastWrongAttribute))
throw new WrongReferenceAttributeValueException("Method doTheMagic can't fix this attribute value", ex);
lastWrongAttribute = wrongAttributeDefinition;
if (!findAndSetValueInList(requiredAttributes, wrongAttributeDefinition, null)) {
//this attribute can't be fixed here
throw ex;
}
}
safetyCounter++;
if (safetyCounter == 50)
throw new InternalErrorException("Method doTheMagic possibly stays in infinite loop.");
} while (trueMagic && !allOk);
}
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_vsupMailAlias method fillAttribute.
@Override
public Attribute fillAttribute(PerunSessionImpl session, User user, AttributeDefinition attribute) throws InternalErrorException, WrongAttributeAssignmentException {
String firstName = user.getFirstName();
String lastName = user.getLastName();
Attribute filledAttribute = new Attribute(attribute);
try {
Attribute artFirstName = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, "urn:perun:user:attribute-def:def:artisticFirstName");
Attribute artLastName = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, "urn:perun:user:attribute-def:def:artisticLastName");
if (artFirstName.getValue() != null)
firstName = (String) artFirstName.getValue();
if (artLastName.getValue() != null)
lastName = (String) artLastName.getValue();
} catch (AttributeNotExistsException e) {
throw new ConsistencyErrorException("Definition for artistic names of user doesn't exists.", e);
}
if (lastName == null || firstName == null) {
return filledAttribute;
}
// remove all diacritics marks from name
String mail = ModulesUtilsBlImpl.normalizeStringForLogin(firstName) + "." + ModulesUtilsBlImpl.normalizeStringForLogin(lastName);
// fill value - start as mail, mail2, mail3, ....
int iterator = 1;
while (iterator >= 1) {
if (iterator > 1) {
filledAttribute.setValue(mail + iterator + "@vsup.cz");
} else {
filledAttribute.setValue(mail + "@vsup.cz");
}
try {
checkAttributeValue(session, user, filledAttribute);
return filledAttribute;
} catch (WrongAttributeValueException ex) {
// continue in a WHILE cycle
iterator++;
}
}
return filledAttribute;
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_vsupMailAliases 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 vsupMailAttribute;
Attribute mailAliasAttribute;
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);
vsupMailAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupMailUrn);
mailAliasAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupMailAliasUrn);
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 'vsupMailAliases' 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) {
List<String> mails = (List<String>) attribute.getValue();
for (String mail : mails) {
String ownersUserId = reservedMailsAttributeValue.get(mail);
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("On of VŠUP mail aliases: '" + mail + "' 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 (vsupMailAttribute.getValue() != null) {
actualMailsOfUser.add((String) vsupMailAttribute.getValue());
}
if (vsupPreferredMailAttribute.getValue() != null) {
actualMailsOfUser.add((String) vsupPreferredMailAttribute.getValue());
}
if (mailAliasAttribute.getValue() != null) {
actualMailsOfUser.add((String) mailAliasAttribute.getValue());
}
for (String mail : reservedMailsOfUser) {
if (!actualMailsOfUser.contains(mail)) {
// Remove mail, which is not in attributes anymore
reservedMailsAttributeValue.remove(mail);
}
}
// Put in which is in attribute but not in a map
if (attribute.getValue() != null) {
List<String> mails = (List<String>) attribute.getValue();
for (String mail : mails) {
reservedMailsAttributeValue.putIfAbsent(mail, 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);
}
}
Aggregations