use of org.graylog.events.notifications.EventNotificationModelData 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