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;
}
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));
});
}
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");
}
}
});
}
Aggregations