Search in sources :

Example 1 with PerunNotifReceiver

use of cz.metacentrum.perun.notif.entities.PerunNotifReceiver 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 2 with PerunNotifReceiver

use of cz.metacentrum.perun.notif.entities.PerunNotifReceiver in project perun by CESNET.

the class PerunNotifEmailGroupSender method send.

@Override
public Set<Integer> send(List<PerunNotifMessageDto> dtosToSend) {
    Set<Integer> usedPoolIds = new HashSet<Integer>();
    List<PerunNotifEmailMessageToSendDto> messagesToSend = new ArrayList<PerunNotifEmailMessageToSendDto>();
    for (PerunNotifMessageDto messageDto : dtosToSend) {
        PoolMessage dto = messageDto.getPoolMessage();
        PerunNotifTemplate template = messageDto.getTemplate();
        PerunNotifReceiver receiver = messageDto.getReceiver();
        try {
            String groupSender = dto.getKeyAttributes().get(template.getSender());
            if (groupSender == null || groupSender.isEmpty()) {
                groupSender = template.getSender();
            }
            logger.debug("Calculated sender : {}", groupSender);
            Integer groupId = Integer.valueOf(receiver.getTarget());
            Group group = perun.getGroupsManagerBl().getGroupById(session, groupId);
            List<Member> groupMembers = perun.getGroupsManagerBl().getGroupMembers(session, group);
            if (groupMembers != null) {
                for (Member member : groupMembers) {
                    try {
                        PerunNotifEmailMessageToSendDto memberEmailDto = new PerunNotifEmailMessageToSendDto();
                        memberEmailDto.setMessage(messageDto.getMessageToSend());
                        memberEmailDto.setSubject(messageDto.getSubject());
                        memberEmailDto.setReceiver((String) perun.getAttributesManagerBl().getAttribute(session, perun.getUsersManager().getUserByMember(session, member), "urn:perun:user:attribute-def:def:preferredMail").getValue());
                        memberEmailDto.setSender(groupSender);
                        messagesToSend.add(memberEmailDto);
                    } catch (Exception ex) {
                        logger.error("PreferredEmail cannot be retrieved, userId: {}", member.getUserId(), ex);
                    }
                }
            }
            usedPoolIds.addAll(messageDto.getUsedPoolIds());
        } catch (NumberFormatException ex) {
            logger.error("GroupId cannot be parsed: {}", receiver.getTarget());
        } catch (GroupNotExistsException ex) {
            logger.error("Group with id: {} does not exists.", receiver.getTarget());
        } catch (InternalErrorException ex) {
            logger.error("Error during processing messageDto.", ex);
        }
    }
    perunNotifEmailManager.sendMessages(messagesToSend);
    return usedPoolIds;
}
Also used : PerunNotifTemplate(cz.metacentrum.perun.notif.entities.PerunNotifTemplate) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) ArrayList(java.util.ArrayList) PerunNotifReceiver(cz.metacentrum.perun.notif.entities.PerunNotifReceiver) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) PerunNotifMessageDto(cz.metacentrum.perun.notif.dto.PerunNotifMessageDto) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) PoolMessage(cz.metacentrum.perun.notif.dto.PoolMessage) PerunNotifEmailMessageToSendDto(cz.metacentrum.perun.notif.dto.PerunNotifEmailMessageToSendDto) HashSet(java.util.HashSet)

Example 3 with PerunNotifReceiver

use of cz.metacentrum.perun.notif.entities.PerunNotifReceiver in project perun by CESNET.

the class PerunNotifTemplateDaoImpl method getAllPerunNotifTemplates.

public List<PerunNotifTemplate> getAllPerunNotifTemplates() {
    List<PerunNotifTemplate> result = this.getJdbcTemplate().query("SELECT * from pn_template", PerunNotifTemplate.PERUN_NOTIF_TEMPLATE);
    for (PerunNotifTemplate template : result) {
        // Gets all template ids which are connected to given regexIds
        Set<PerunNotifRegex> perunNotifRegexs = perunNotifRegexDao.getPerunNotifRegexForTemplateId(template.getId());
        template.setMatchingRegexs(perunNotifRegexs);
        List<PerunNotifReceiver> perunNotifReceiver = this.getJdbcTemplate().query("SELECT * from pn_receiver where template_id = ?", new Object[] { template.getId() }, PerunNotifReceiver.PERUN_NOTIF_RECEIVER);
        template.setReceivers(perunNotifReceiver);
        List<PerunNotifTemplateMessage> perunNotifTemplateMessages = this.getJdbcTemplate().query("SELECT * from pn_template_message where template_id = ?", new Object[] { template.getId() }, PerunNotifTemplateMessage.PERUN_NOTIF_TEMPLATE_MESSAGE_ROW_MAPPER);
        template.setPerunNotifTemplateMessages(perunNotifTemplateMessages);
    }
    return result;
}
Also used : PerunNotifTemplate(cz.metacentrum.perun.notif.entities.PerunNotifTemplate) PerunNotifTemplateMessage(cz.metacentrum.perun.notif.entities.PerunNotifTemplateMessage) PerunNotifReceiver(cz.metacentrum.perun.notif.entities.PerunNotifReceiver) PerunNotifRegex(cz.metacentrum.perun.notif.entities.PerunNotifRegex)

Example 4 with PerunNotifReceiver

use of cz.metacentrum.perun.notif.entities.PerunNotifReceiver in project perun by CESNET.

the class PerunNotifTemplateDaoImpl method getPerunNotifTemplateById.

@Override
public PerunNotifTemplate getPerunNotifTemplateById(int id) {
    PerunNotifTemplate template = null;
    try {
        template = this.getJdbcTemplate().queryForObject("SELECT * from pn_template where id = ?", new Object[] { id }, PerunNotifTemplate.PERUN_NOTIF_TEMPLATE);
    } catch (EmptyResultDataAccessException ex) {
        // This exception is thrown when object is not found
        return null;
    }
    Set<PerunNotifRegex> regexes = perunNotifRegexDao.getPerunNotifRegexForTemplateId(template.getId());
    template.setMatchingRegexs(regexes);
    List<PerunNotifReceiver> perunNotifReceiver = this.getJdbcTemplate().query("SELECT * from pn_receiver where template_id = ?", new Object[] { template.getId() }, PerunNotifReceiver.PERUN_NOTIF_RECEIVER);
    template.setReceivers(perunNotifReceiver);
    List<PerunNotifTemplateMessage> perunNotifTemplateMessages = this.getJdbcTemplate().query("SELECT * from pn_template_message where template_id = ?", new Object[] { template.getId() }, PerunNotifTemplateMessage.PERUN_NOTIF_TEMPLATE_MESSAGE_ROW_MAPPER);
    template.setPerunNotifTemplateMessages(perunNotifTemplateMessages);
    return template;
}
Also used : PerunNotifTemplate(cz.metacentrum.perun.notif.entities.PerunNotifTemplate) PerunNotifTemplateMessage(cz.metacentrum.perun.notif.entities.PerunNotifTemplateMessage) PerunNotifReceiver(cz.metacentrum.perun.notif.entities.PerunNotifReceiver) PerunNotifRegex(cz.metacentrum.perun.notif.entities.PerunNotifRegex) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 5 with PerunNotifReceiver

use of cz.metacentrum.perun.notif.entities.PerunNotifReceiver in project perun by CESNET.

the class PerunNotifEmailUserSender method send.

@Override
public Set<Integer> send(List<PerunNotifMessageDto> dtosToSend) {
    Set<Integer> usedPools = new HashSet<Integer>();
    List<PerunNotifEmailMessageToSendDto> messagesToSend = new ArrayList<PerunNotifEmailMessageToSendDto>();
    for (PerunNotifMessageDto messageDto : dtosToSend) {
        PerunNotifReceiver receiver = messageDto.getReceiver();
        PoolMessage dto = messageDto.getPoolMessage();
        logger.debug("Creating email for user, receiver: {}", receiver.getId());
        PerunNotifEmailMessageToSendDto emailDto = new PerunNotifEmailMessageToSendDto();
        emailDto.setMessage(messageDto.getMessageToSend());
        emailDto.setSubject(messageDto.getSubject());
        usedPools.addAll(messageDto.getUsedPoolIds());
        String sender = messageDto.getSender();
        emailDto.setSender(sender);
        logger.debug("Calculated sender for receiver: {}, sender: {}", receiver.getId(), sender);
        String myReceiverId = dto.getKeyAttributes().get(receiver.getTarget());
        if (myReceiverId == null || myReceiverId.isEmpty()) {
            // Can be set one static account
            emailDto.setReceiver(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: {}", 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:preferredMail");
                    if (emailAttribute != null && StringUtils.hasText(emailAttribute.toString())) {
                        emailDto.setReceiver((String) emailAttribute.getValue());
                    }
                } catch (UserNotExistsException ex) {
                    logger.error("Cannot found user with id: {}, ex: {}", id, ex.getMessage());
                    logger.debug("ST:", ex);
                } catch (AttributeNotExistsException ex) {
                    logger.warn("Cannot found email for user with id: {}, ex: {}", id, ex.getMessage());
                    logger.debug("ST:", ex);
                } catch (Exception ex) {
                    logger.error("Error during user email recognition, ex: {}", ex.getMessage());
                    logger.debug("ST:", ex);
                }
            }
        }
        messagesToSend.add(emailDto);
    }
    perunNotifEmailManager.sendMessages(messagesToSend);
    return usedPools;
}
Also used : UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) PerunNotifReceiver(cz.metacentrum.perun.notif.entities.PerunNotifReceiver) PerunNotifMessageDto(cz.metacentrum.perun.notif.dto.PerunNotifMessageDto) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) PoolMessage(cz.metacentrum.perun.notif.dto.PoolMessage) PerunNotifEmailMessageToSendDto(cz.metacentrum.perun.notif.dto.PerunNotifEmailMessageToSendDto)

Aggregations

PerunNotifReceiver (cz.metacentrum.perun.notif.entities.PerunNotifReceiver)5 PerunNotifMessageDto (cz.metacentrum.perun.notif.dto.PerunNotifMessageDto)3 PoolMessage (cz.metacentrum.perun.notif.dto.PoolMessage)3 PerunNotifTemplate (cz.metacentrum.perun.notif.entities.PerunNotifTemplate)3 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)2 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)2 PerunNotifEmailMessageToSendDto (cz.metacentrum.perun.notif.dto.PerunNotifEmailMessageToSendDto)2 PerunNotifRegex (cz.metacentrum.perun.notif.entities.PerunNotifRegex)2 PerunNotifTemplateMessage (cz.metacentrum.perun.notif.entities.PerunNotifTemplateMessage)2 GroupNotExistsException (cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ConnectionConfiguration (org.jivesoftware.smack.ConnectionConfiguration)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 XMPPException (org.jivesoftware.smack.XMPPException)1 Message (org.jivesoftware.smack.packet.Message)1 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)1