Search in sources :

Example 1 with EventNotificationModelData

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());
    }
}
Also used : Request(okhttp3.Request) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) EventNotificationModelData(org.graylog.events.notifications.EventNotificationModelData) PermanentEventNotificationException(org.graylog.events.notifications.PermanentEventNotificationException) MessageSummary(org.graylog2.plugin.MessageSummary) TemporaryEventNotificationException(org.graylog.events.notifications.TemporaryEventNotificationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 HttpUrl (okhttp3.HttpUrl)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 EventNotificationModelData (org.graylog.events.notifications.EventNotificationModelData)1 PermanentEventNotificationException (org.graylog.events.notifications.PermanentEventNotificationException)1 TemporaryEventNotificationException (org.graylog.events.notifications.TemporaryEventNotificationException)1 MessageSummary (org.graylog2.plugin.MessageSummary)1