use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class FacilitiesManagerEntryIntegrationTest method getMandatoryAttrs.
private List<AttributeDefinition> getMandatoryAttrs() throws InternalErrorException {
List<String> MANDATORY_ATTRIBUTES_FOR_USER_IN_CONTACT = new ArrayList<>(Arrays.asList(AttributesManager.NS_USER_ATTR_DEF + ":organization", AttributesManager.NS_USER_ATTR_DEF + ":preferredMail"));
List<AttributeDefinition> mandatoryAttrs = new ArrayList<>();
for (String attrName : MANDATORY_ATTRIBUTES_FOR_USER_IN_CONTACT) {
try {
mandatoryAttrs.add(perun.getAttributesManagerBl().getAttributeDefinition(sess, attrName));
} catch (AttributeNotExistsException ex) {
throw new InternalErrorException("Some of mandatory attributes for users in facility contacts not exists.", ex);
}
}
return mandatoryAttrs;
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class PerunNotifJabberSender method send.
@Override
public Set<Integer> send(List<PerunNotifMessageDto> dtosToSend) {
Set<Integer> usedPools = new HashSet<Integer>();
try {
ConnectionConfiguration config = new ConnectionConfiguration(jabberServer, port, serviceName);
XMPPConnection connection = new XMPPConnection(config);
connection.connect();
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
connection.login(username, password);
for (PerunNotifMessageDto messageDto : dtosToSend) {
PerunNotifReceiver receiver = messageDto.getReceiver();
PoolMessage dto = messageDto.getPoolMessage();
Message message = new Message();
message.setSubject(messageDto.getSubject());
message.setBody(messageDto.getMessageToSend());
message.setType(Message.Type.headline);
String myReceiverId = dto.getKeyAttributes().get(receiver.getTarget());
if (myReceiverId == null || myReceiverId.isEmpty()) {
//Can be set one static account
message.setTo(receiver.getTarget());
} else {
//We try to resolve id
Integer id = null;
try {
id = Integer.valueOf(myReceiverId);
} catch (NumberFormatException ex) {
logger.error("Cannot resolve id: {}, error: {}", Arrays.asList(id, ex.getMessage()));
logger.debug("ST:", ex);
}
if (id != null) {
try {
User user = perun.getUsersManagerBl().getUserById(session, id);
Attribute emailAttribute = perun.getAttributesManagerBl().getAttribute(session, user, "urn:perun:user:attribute-def:def:jabber");
if (emailAttribute != null && StringUtils.hasText(emailAttribute.toString())) {
message.setTo((String) emailAttribute.getValue());
}
} catch (UserNotExistsException ex) {
logger.error("Cannot found user with id: {}, ex: {}", Arrays.asList(id, ex.getMessage()));
logger.debug("ST:", ex);
} catch (AttributeNotExistsException ex) {
logger.warn("Cannot found email for user with id: {}, ex: {}", Arrays.asList(id, ex.getMessage()));
logger.debug("ST:", ex);
} catch (Exception ex) {
logger.error("Error during user email recognition, ex: {}", ex.getMessage());
logger.debug("ST:", ex);
}
}
}
connection.sendPacket(message);
usedPools.addAll(messageDto.getUsedPoolIds());
}
connection.disconnect();
} catch (XMPPException ex) {
logger.error("Error during jabber establish connection.", ex);
}
return null;
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class Utils method generateAllResourcesToWriter.
/**
* Method generate all Resources to the text for using in LDIF.
* Write all these information to writer in perunInitializer object.
*
* @param perunInitializer need to be loaded to get all needed dependencies
*
* @throws InternalErrorException if some problem with initializer or objects in perun-core
* @throws IOException if some problem with writer
*/
public static void generateAllResourcesToWriter(PerunInitializer perunInitializer) throws InternalErrorException, IOException {
//Load basic variables
if (perunInitializer == null)
throw new InternalErrorException("PerunInitializer must be loaded before using in generating methods!");
PerunSession perunSession = perunInitializer.getPerunSession();
PerunBl perun = perunInitializer.getPerunBl();
BufferedWriter writer = perunInitializer.getOutputWriter();
//first get all Vos
List<Vo> vos = perun.getVosManagerBl().getVos(perunSession);
//Then from every Vo get all assigned resources and write their data to the writer
for (Vo vo : vos) {
List<Resource> resources;
resources = perun.getResourcesManagerBl().getResources(perunSession, vo);
for (Resource resource : resources) {
//Read facility attribute entityID and write it for the resource if exists
Facility facility = null;
try {
facility = perun.getFacilitiesManagerBl().getFacilityById(perunSession, resource.getFacilityId());
} catch (FacilityNotExistsException ex) {
throw new InternalErrorException("Can't found facility of this resource " + resource, ex);
}
Attribute entityIDAttr = null;
try {
entityIDAttr = perun.getAttributesManagerBl().getAttribute(perunSession, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":entityID");
} catch (AttributeNotExistsException | WrongAttributeAssignmentException ex) {
throw new InternalErrorException("Problem with loading entityID attribute of facility " + facility, ex);
}
String dn = "dn: ";
String oc1 = "objectclass: top";
String oc3 = "objectclass: perunResource";
String cn = "cn: ";
String perunVoId = "perunVoId: ";
String perunFacilityId = "perunFacilityId: ";
String perunResourceId = "perunResourceId: ";
String description = "description: ";
String entityID = "entityID: ";
perunVoId += String.valueOf(resource.getVoId());
perunFacilityId += String.valueOf(resource.getFacilityId());
perunResourceId += String.valueOf(resource.getId());
dn += "perunResourceId=" + resource.getId() + ",perunVoId=" + resource.getVoId() + ",dc=perun,dc=cesnet,dc=cz";
cn += resource.getName();
String descriptionValue = resource.getDescription();
if (descriptionValue != null) {
if (descriptionValue.matches("^[ ]*$"))
descriptionValue = null;
}
writer.write(dn + '\n');
writer.write(oc1 + '\n');
writer.write(oc3 + '\n');
writer.write(cn + '\n');
writer.write(perunResourceId + '\n');
if (descriptionValue != null)
writer.write(description + descriptionValue + '\n');
writer.write(perunVoId + '\n');
writer.write(perunFacilityId + '\n');
if (entityIDAttr.getValue() != null)
writer.write(entityID + (String) entityIDAttr.getValue() + '\n');
//ADD resources which group is assigned to
List<Group> associatedGroups = perun.getResourcesManagerBl().getAssignedGroups(perunSession, resource);
for (Group g : associatedGroups) {
writer.write("assignedGroupId: " + g.getId());
writer.write('\n');
}
writer.write('\n');
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class RTMessagesManagerBlImpl method prepareDataAndGetHttpRequest.
private HttpUriRequest prepareDataAndGetHttpRequest(PerunSession sess, int voId, String queue, String requestor, String subject, String text) throws InternalErrorException {
//Ticket from this part is already evidet like 'new'
String id = "ticket/new";
//If there is no requestor, it is uknown requestor
if (requestor == null || requestor.isEmpty()) {
requestor = "unknown";
}
//If queue is null, try to check if exist value in attribute rtVoQueue, if not, use default
if (queue == null || queue.isEmpty()) {
Vo vo = null;
if (voId != 0) {
try {
vo = perunBl.getVosManagerBl().getVoById(sess, voId);
} catch (VoNotExistsException ex) {
throw new InternalErrorException("VoId with Id=" + voId + " not exists.", ex);
}
Attribute voQueue = null;
try {
voQueue = perunBl.getAttributesManagerBl().getAttribute(sess, vo, AttributesManager.NS_VO_ATTR_DEF + ":RTVoQueue");
} catch (AttributeNotExistsException ex) {
throw new InternalErrorException("Attribute RTVoQueue not exists.", ex);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
}
if (voQueue.getValue() != null) {
queue = (String) voQueue.getValue();
} else
queue = rtDefaultQueue;
} else
queue = rtDefaultQueue;
}
//If subject is null or empty, use Unspecified instead
if (subject == null || subject.isEmpty())
subject = "(No subject)";
//Text can be null so if it is, put empty string
if (text == null)
text = "";
//Prepare credentials
String username = BeansUtils.getCoreConfig().getRtServiceuserUsername();
String password = BeansUtils.getCoreConfig().getRtServiceuserPassword();
//Prepare content of message
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
try {
entityBuilder.addPart("Content-Type", new StringBody("application/x-www-form-urlencoded", ContentType.create("text/plain", Consts.UTF_8)));
entityBuilder.addPart("charset", new StringBody("utf-8", ContentType.create("text/plain", Consts.UTF_8)));
entityBuilder.addPart("Connection", new StringBody("Close", ContentType.create("text/plain", Consts.UTF_8)));
StringBody content = new StringBody("id: " + id + '\n' + "Queue: " + queue + '\n' + "Requestor: " + requestor + '\n' + "Subject: " + subject + '\n' + "Text: " + text, ContentType.create("text/plain", Consts.UTF_8));
entityBuilder.addPart("content", content);
} catch (Exception e) {
throw new RuntimeException(e);
}
//Test rtURL for null
if (rtURL == null || rtURL.length() == 0)
throw new InternalErrorException("rtURL is not prepared and is null in the moment of posting.");
// prepare post request
HttpPost post = new HttpPost(rtURL);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
post.addHeader(BasicScheme.authenticate(credentials, "utf-8", false));
post.setEntity(entityBuilder.build());
return post;
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class SearcherBlImpl method getUsersForCoreAttributes.
public List<User> getUsersForCoreAttributes(PerunSession sess, Map<String, String> coreAttributesWithSearchingValues) throws InternalErrorException, AttributeNotExistsException, WrongAttributeAssignmentException {
List<User> users = getPerunBl().getUsersManagerBl().getUsers(sess);
if (coreAttributesWithSearchingValues == null || coreAttributesWithSearchingValues.isEmpty())
return users;
Map<AttributeDefinition, String> mapOfCoreAttributesWithValues = new HashMap<AttributeDefinition, String>();
Set<String> keys = coreAttributesWithSearchingValues.keySet();
for (String name : keys) {
if (name == null || name.equals(""))
throw new AttributeNotExistsException("There is attribute with no specific name!");
AttributeDefinition attrDef = perunBl.getAttributesManagerBl().getAttributeDefinition(sess, name);
if (getPerunBl().getAttributesManagerBl().isCoreAttribute(sess, attrDef)) {
mapOfCoreAttributesWithValues.put(attrDef, coreAttributesWithSearchingValues.get(name));
} else {
throw new InternalErrorException("Attribute: " + attrDef + " is not core attribute! Can't be get for users by this method.");
}
}
return this.getUsersForCoreAttributesByMapOfAttributes(sess, mapOfCoreAttributesWithValues);
}
Aggregations