Search in sources :

Example 1 with Trigger

use of io.gravitee.alert.api.trigger.Trigger in project gravitee-management-rest-api by gravitee-io.

the class AlertServiceImpl method convert.

private AlertTriggerEntity convert(final AlertTrigger alert) {
    try {
        Trigger trigger = mapper.readValue(alert.getDefinition(), Trigger.class);
        final AlertTriggerEntity alertTriggerEntity = new AlertTriggerEntityWrapper(trigger);
        alertTriggerEntity.setDescription(alert.getDescription());
        alertTriggerEntity.setReferenceId(alert.getReferenceId());
        alertTriggerEntity.setReferenceType(AlertReferenceType.valueOf(alert.getReferenceType()));
        alertTriggerEntity.setCreatedAt(alert.getCreatedAt());
        alertTriggerEntity.setUpdatedAt(alert.getUpdatedAt());
        alertTriggerEntity.setType(alert.getType());
        if (alert.getSeverity() != null) {
            alertTriggerEntity.setSeverity(Trigger.Severity.valueOf(alert.getSeverity()));
        } else {
            alertTriggerEntity.setSeverity(Trigger.Severity.INFO);
        }
        alertTriggerEntity.setEnabled(alert.isEnabled());
        alertTriggerEntity.setTemplate(alert.isTemplate());
        alertTriggerEntity.setParentId(alert.getParentId());
        if (alert.getEventRules() != null) {
            alertTriggerEntity.setEventRules(alert.getEventRules().stream().map(alertEventRule -> new AlertEventRuleEntity(alertEventRule.getEvent().name())).collect(Collectors.toList()));
        }
        return alertTriggerEntity;
    } catch (Exception ex) {
        LOGGER.error("An unexpected error occurs while transforming the alert trigger from its definition", ex);
    }
    return null;
}
Also used : Trigger(io.gravitee.alert.api.trigger.Trigger) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) IOException(java.io.IOException)

Example 2 with Trigger

use of io.gravitee.alert.api.trigger.Trigger in project gravitee-management-rest-api by gravitee-io.

the class ApplicationAlertServiceImpl method addMemberToApplication.

@Override
public void addMemberToApplication(String applicationId, String email) {
    if (StringUtils.isEmpty(email)) {
        return;
    }
    // check existence of application
    applicationService.findById(applicationId);
    alertService.findByReference(AlertReferenceType.APPLICATION, applicationId).forEach(trigger -> {
        if (trigger.getNotifications() == null) {
            trigger.setNotifications(createNotification(trigger.getType()));
        }
        final Optional<Notification> notificationOpt = trigger.getNotifications().stream().filter(n -> DEFAULT_EMAIL_NOTIFIER.equals(n.getType())).findFirst();
        if (notificationOpt.isPresent()) {
            Notification notification = notificationOpt.get();
            try {
                ObjectNode configuration = mapper.createObjectNode();
                JsonNode emailNode = mapper.readTree(notification.getConfiguration());
                configuration.put("to", emailNode.path("to").asText() + "," + email);
                configuration.put("from", emailNode.path("from").asText());
                configuration.put("subject", emailNode.path("subject").asText());
                configuration.put("body", emailNode.path("body").asText());
                notification.setConfiguration(mapper.writeValueAsString(configuration));
            } catch (JsonProcessingException e) {
                LOGGER.error("An error occurs while trying to add a recipient to the Alert notification", e);
                throw new TechnicalManagementException("An error occurs while trying to add a recipient to the Alert notification");
            }
        } else {
            trigger.setNotifications(createNotification(trigger.getType(), singletonList(email)));
        }
        alertService.update(convert(trigger));
    });
}
Also used : AlertHook(io.gravitee.rest.api.service.notification.AlertHook) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) HookScope(io.gravitee.rest.api.service.notification.HookScope) AlertTriggerRepository(io.gravitee.repository.management.api.AlertTriggerRepository) Collections.singletonList(java.util.Collections.singletonList) StringCondition(io.gravitee.alert.api.condition.StringCondition) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) UpdateAlertTriggerEntity(io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) AlertStatusEntity(io.gravitee.rest.api.model.alert.AlertStatusEntity) Collections.emptyList(java.util.Collections.emptyList) MembershipService(io.gravitee.rest.api.service.MembershipService) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) NewAlertTriggerEntity(io.gravitee.rest.api.model.alert.NewAlertTriggerEntity) AlertService(io.gravitee.rest.api.service.AlertService) CollectionUtils(org.springframework.util.CollectionUtils) ApplicationService(io.gravitee.rest.api.service.ApplicationService) Optional(java.util.Optional) Filter(io.gravitee.alert.api.condition.Filter) NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Trigger(io.gravitee.alert.api.trigger.Trigger) Async(org.springframework.scheduling.annotation.Async) ApplicationAlertEventType(io.gravitee.rest.api.model.alert.ApplicationAlertEventType) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ApplicationAlertService(io.gravitee.rest.api.service.ApplicationAlertService) ArrayList(java.util.ArrayList) ApplicationAlertMembershipEvent(io.gravitee.rest.api.model.alert.ApplicationAlertMembershipEvent) HashSet(java.util.HashSet) Value(org.springframework.beans.factory.annotation.Value) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NotificationTemplateService(io.gravitee.rest.api.service.notification.NotificationTemplateService) UserService(io.gravitee.rest.api.service.UserService) Notification(io.gravitee.notifier.api.Notification) AlertReferenceType(io.gravitee.rest.api.model.alert.AlertReferenceType) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Event(io.gravitee.common.event.Event) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AlertTriggerEntity(io.gravitee.rest.api.model.alert.AlertTriggerEntity) Dampening(io.gravitee.alert.api.trigger.Dampening) Consumer(java.util.function.Consumer) MembershipReferenceType(io.gravitee.rest.api.model.MembershipReferenceType) Component(org.springframework.stereotype.Component) NotificationTemplateEvent(io.gravitee.rest.api.model.notification.NotificationTemplateEvent) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) StringUtils(org.springframework.util.StringUtils) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Notification(io.gravitee.notifier.api.Notification) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 3 with Trigger

use of io.gravitee.alert.api.trigger.Trigger in project gravitee-management-rest-api by gravitee-io.

the class ApplicationAlertServiceImpl method deleteMemberFromApplication.

@Override
public void deleteMemberFromApplication(String applicationId, String email) {
    if (StringUtils.isEmpty(email)) {
        return;
    }
    // check existence of application
    applicationService.findById(applicationId);
    alertService.findByReference(AlertReferenceType.APPLICATION, applicationId).forEach(trigger -> {
        if (trigger.getNotifications() == null) {
            trigger.setNotifications(createNotification(trigger.getType()));
        }
        final Optional<Notification> notificationOpt = trigger.getNotifications().stream().filter(n -> DEFAULT_EMAIL_NOTIFIER.equals(n.getType())).findFirst();
        if (notificationOpt.isPresent()) {
            final Notification notification = notificationOpt.get();
            try {
                ObjectNode configuration = mapper.createObjectNode();
                JsonNode emailNode = mapper.readTree(notification.getConfiguration());
                final String to = Arrays.stream(emailNode.path("to").asText().split(",|;|\\s")).filter(mailTo -> !mailTo.equals(email)).collect(Collectors.joining(","));
                if (StringUtils.isEmpty(to)) {
                    trigger.setNotifications(emptyList());
                } else {
                    configuration.put("to", to);
                    configuration.put("from", emailNode.path("from").asText());
                    configuration.put("subject", emailNode.path("subject").asText());
                    configuration.put("body", emailNode.path("body").asText());
                    notification.setConfiguration(mapper.writeValueAsString(configuration));
                }
                alertService.update(convert(trigger));
            } catch (JsonProcessingException e) {
                LOGGER.error("An error occurs while trying to add a recipient to the Alert notification", e);
                throw new TechnicalManagementException("An error occurs while trying to add a recipient to the Alert notification");
            }
        }
    });
}
Also used : AlertHook(io.gravitee.rest.api.service.notification.AlertHook) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) HookScope(io.gravitee.rest.api.service.notification.HookScope) AlertTriggerRepository(io.gravitee.repository.management.api.AlertTriggerRepository) Collections.singletonList(java.util.Collections.singletonList) StringCondition(io.gravitee.alert.api.condition.StringCondition) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) UpdateAlertTriggerEntity(io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) AlertStatusEntity(io.gravitee.rest.api.model.alert.AlertStatusEntity) Collections.emptyList(java.util.Collections.emptyList) MembershipService(io.gravitee.rest.api.service.MembershipService) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) NewAlertTriggerEntity(io.gravitee.rest.api.model.alert.NewAlertTriggerEntity) AlertService(io.gravitee.rest.api.service.AlertService) CollectionUtils(org.springframework.util.CollectionUtils) ApplicationService(io.gravitee.rest.api.service.ApplicationService) Optional(java.util.Optional) Filter(io.gravitee.alert.api.condition.Filter) NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Trigger(io.gravitee.alert.api.trigger.Trigger) Async(org.springframework.scheduling.annotation.Async) ApplicationAlertEventType(io.gravitee.rest.api.model.alert.ApplicationAlertEventType) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ApplicationAlertService(io.gravitee.rest.api.service.ApplicationAlertService) ArrayList(java.util.ArrayList) ApplicationAlertMembershipEvent(io.gravitee.rest.api.model.alert.ApplicationAlertMembershipEvent) HashSet(java.util.HashSet) Value(org.springframework.beans.factory.annotation.Value) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NotificationTemplateService(io.gravitee.rest.api.service.notification.NotificationTemplateService) UserService(io.gravitee.rest.api.service.UserService) Notification(io.gravitee.notifier.api.Notification) AlertReferenceType(io.gravitee.rest.api.model.alert.AlertReferenceType) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Event(io.gravitee.common.event.Event) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AlertTriggerEntity(io.gravitee.rest.api.model.alert.AlertTriggerEntity) Dampening(io.gravitee.alert.api.trigger.Dampening) Consumer(java.util.function.Consumer) MembershipReferenceType(io.gravitee.rest.api.model.MembershipReferenceType) Component(org.springframework.stereotype.Component) NotificationTemplateEvent(io.gravitee.rest.api.model.notification.NotificationTemplateEvent) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) StringUtils(org.springframework.util.StringUtils) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Notification(io.gravitee.notifier.api.Notification) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

Trigger (io.gravitee.alert.api.trigger.Trigger)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Filter (io.gravitee.alert.api.condition.Filter)2 StringCondition (io.gravitee.alert.api.condition.StringCondition)2 Dampening (io.gravitee.alert.api.trigger.Dampening)2 Event (io.gravitee.common.event.Event)2 Notification (io.gravitee.notifier.api.Notification)2 AlertTriggerRepository (io.gravitee.repository.management.api.AlertTriggerRepository)2 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)2 MembershipEntity (io.gravitee.rest.api.model.MembershipEntity)2 MembershipReferenceType (io.gravitee.rest.api.model.MembershipReferenceType)2 UserEntity (io.gravitee.rest.api.model.UserEntity)2 AlertReferenceType (io.gravitee.rest.api.model.alert.AlertReferenceType)2 AlertStatusEntity (io.gravitee.rest.api.model.alert.AlertStatusEntity)2 AlertTriggerEntity (io.gravitee.rest.api.model.alert.AlertTriggerEntity)2 ApplicationAlertEventType (io.gravitee.rest.api.model.alert.ApplicationAlertEventType)2 ApplicationAlertMembershipEvent (io.gravitee.rest.api.model.alert.ApplicationAlertMembershipEvent)2