use of org.graylog.events.notifications.TemporaryEventNotificationException in project graylog2-server by Graylog2.
the class EmailEventNotification method execute.
@Override
public void execute(EventNotificationContext ctx) throws TemporaryEventNotificationException, PermanentEventNotificationException {
final EmailEventNotificationConfig config = (EmailEventNotificationConfig) ctx.notificationConfig();
try {
ImmutableList<MessageSummary> backlog = notificationCallbackService.getBacklogForEvent(ctx);
emailSender.sendEmails(config, ctx, backlog);
} catch (EmailSender.ConfigurationError e) {
throw new TemporaryEventNotificationException(e.getMessage());
} catch (TransportConfigurationException e) {
Notification systemNotification = notificationService.buildNow().addNode(nodeId.toString()).addType(Notification.Type.EMAIL_TRANSPORT_CONFIGURATION_INVALID).addSeverity(Notification.Severity.NORMAL).addDetail("exception", e.getMessage());
notificationService.publishIfFirst(systemNotification);
throw new TemporaryEventNotificationException("Notification has email recipients and is triggered, but email transport is not configured. " + e.getMessage());
} catch (Exception e) {
String exceptionDetail = e.toString();
if (e.getCause() != null) {
exceptionDetail += " (" + e.getCause() + ")";
}
final Notification systemNotification = notificationService.buildNow().addNode(nodeId.toString()).addType(Notification.Type.EMAIL_TRANSPORT_FAILED).addSeverity(Notification.Severity.NORMAL).addDetail("exception", exceptionDetail);
notificationService.publishIfFirst(systemNotification);
throw new PermanentEventNotificationException("Notification has email recipients and is triggered, but sending emails failed. " + e.getMessage());
}
LOG.debug("Sending email to addresses <{}> and users <{}> using notification <{}>", Strings.join(config.emailRecipients(), ','), Strings.join(config.userRecipients(), ','), ctx.notificationId());
}
use of org.graylog.events.notifications.TemporaryEventNotificationException in project graylog2-server by Graylog2.
the class HTTPEventNotification method execute.
@Override
public void execute(EventNotificationContext ctx) throws TemporaryEventNotificationException, PermanentEventNotificationException {
final HTTPEventNotificationConfig config = (HTTPEventNotificationConfig) ctx.notificationConfig();
final HttpUrl httpUrl = HttpUrl.parse(config.url());
if (httpUrl == null) {
throw new TemporaryEventNotificationException("Malformed URL: <" + config.url() + "> in notification <" + ctx.notificationId() + ">");
}
ImmutableList<MessageSummary> backlog = notificationCallbackService.getBacklogForEvent(ctx);
final EventNotificationModelData model = EventNotificationModelData.of(ctx, backlog);
if (!whitelistService.isWhitelisted(config.url())) {
if (!NotificationTestData.TEST_NOTIFICATION_ID.equals(ctx.notificationId())) {
publishSystemNotificationForWhitelistFailure(config.url(), model.eventDefinitionTitle());
}
throw new TemporaryEventNotificationException("URL <" + config.url() + "> is not whitelisted.");
}
final byte[] body;
try {
body = objectMapper.writeValueAsBytes(model);
} catch (JsonProcessingException e) {
throw new PermanentEventNotificationException("Unable to serialize notification", e);
}
final Request request = new Request.Builder().url(httpUrl).post(RequestBody.create(CONTENT_TYPE, body)).build();
LOG.debug("Requesting HTTP endpoint at <{}> in notification <{}>", config.url(), ctx.notificationId());
try (final Response r = httpClient.newCall(request).execute()) {
if (!r.isSuccessful()) {
throw new PermanentEventNotificationException("Expected successful HTTP response [2xx] but got [" + r.code() + "]. " + config.url());
}
} catch (IOException e) {
throw new PermanentEventNotificationException(e.getMessage());
}
}
Aggregations