Search in sources :

Example 41 with AttributeNotExistsException

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;
}
Also used : AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 42 with AttributeNotExistsException

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;
}
Also used : PoolMessage(cz.metacentrum.perun.notif.dto.PoolMessage) Message(org.jivesoftware.smack.packet.Message) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) PerunNotifReceiver(cz.metacentrum.perun.notif.entities.PerunNotifReceiver) XMPPConnection(org.jivesoftware.smack.XMPPConnection) PerunNotifMessageDto(cz.metacentrum.perun.notif.dto.PerunNotifMessageDto) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) XMPPException(org.jivesoftware.smack.XMPPException) ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) PoolMessage(cz.metacentrum.perun.notif.dto.PoolMessage) XMPPException(org.jivesoftware.smack.XMPPException)

Example 43 with AttributeNotExistsException

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');
        }
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) PerunSession(cz.metacentrum.perun.core.api.PerunSession) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) BufferedWriter(java.io.BufferedWriter) Vo(cz.metacentrum.perun.core.api.Vo) Facility(cz.metacentrum.perun.core.api.Facility)

Example 44 with AttributeNotExistsException

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) 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) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) IOException(java.io.IOException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) StringBody(org.apache.http.entity.mime.content.StringBody) Vo(cz.metacentrum.perun.core.api.Vo)

Example 45 with AttributeNotExistsException

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);
}
Also used : User(cz.metacentrum.perun.core.api.User) HashMap(java.util.HashMap) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)138 Attribute (cz.metacentrum.perun.core.api.Attribute)120 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)94 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)81 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)75 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)64 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)59 ArrayList (java.util.ArrayList)30 Resource (cz.metacentrum.perun.core.api.Resource)25 Matcher (java.util.regex.Matcher)19 Facility (cz.metacentrum.perun.core.api.Facility)17 User (cz.metacentrum.perun.core.api.User)17 LinkedHashMap (java.util.LinkedHashMap)15 Group (cz.metacentrum.perun.core.api.Group)14 Map (java.util.Map)13 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)12 List (java.util.List)11 HashSet (java.util.HashSet)8 BigDecimal (java.math.BigDecimal)7 Pattern (java.util.regex.Pattern)6