Search in sources :

Example 16 with NotificationHistory

use of com.redhat.cloud.notifications.models.NotificationHistory in project notifications-backend by RedHatInsights.

the class WebhookTypeProcessor method doHttpRequest.

public NotificationHistory doHttpRequest(Notification item, HttpRequest<Buffer> req, JsonObject payload) {
    final long startTime = System.currentTimeMillis();
    try {
        return Failsafe.with(retryPolicy).get(() -> {
            // TODO NOTIF-488 We may want to move to a non-reactive HTTP client in the future.
            HttpResponse<Buffer> resp = req.sendJsonObject(payload).await().atMost(awaitTimeout);
            NotificationHistory history = buildNotificationHistory(item, startTime);
            HttpRequestImpl<Buffer> reqImpl = (HttpRequestImpl<Buffer>) req.getDelegate();
            boolean serverError = false;
            if (resp.statusCode() >= 200 && resp.statusCode() < 300) {
                // Accepted
                LOGGER.debugf("Webhook request to %s was successful: %d", reqImpl.host(), resp.statusCode());
                history.setInvocationResult(true);
            } else if (resp.statusCode() >= 500) {
                // Temporary error, allow retry
                serverError = true;
                LOGGER.debugf("Webhook request to %s failed: %d %s", reqImpl.host(), resp.statusCode(), resp.statusMessage());
                history.setInvocationResult(false);
            } else {
                // Disable the target endpoint, it's not working correctly for us (such as 400)
                // must be manually re-enabled
                // Redirects etc should have been followed by the vertx (test this)
                LOGGER.debugf("Webhook request to %s failed: %d %s %s", reqImpl.host(), resp.statusCode(), resp.statusMessage(), payload);
                history.setInvocationResult(false);
            }
            if (!history.isInvocationResult()) {
                JsonObject details = new JsonObject();
                details.put("url", getCallUrl(reqImpl));
                details.put("method", reqImpl.method().name());
                details.put("code", resp.statusCode());
                // This isn't async body reading, lets hope vertx handles it async underneath before calling this apply method
                details.put("response_body", resp.bodyAsString());
                history.setDetails(details.getMap());
            }
            if (serverError) {
                throw new ServerErrorException(history);
            }
            return history;
        });
    } catch (Exception e) {
        if (e instanceof ServerErrorException) {
            return ((ServerErrorException) e).getNotificationHistory();
        }
        NotificationHistory history = buildNotificationHistory(item, startTime);
        HttpRequestImpl<Buffer> reqImpl = (HttpRequestImpl<Buffer>) req.getDelegate();
        LOGGER.debugf("Failed: %s", e.getMessage());
        // TODO Duplicate code with the error return code part
        JsonObject details = new JsonObject();
        details.put("url", reqImpl.uri());
        details.put("method", reqImpl.method());
        // TODO This message isn't always the most descriptive..
        details.put("error_message", e.getMessage());
        history.setDetails(details.getMap());
        return history;
    }
}
Also used : Buffer(io.vertx.mutiny.core.buffer.Buffer) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) HttpRequestImpl(io.vertx.ext.web.client.impl.HttpRequestImpl) JsonObject(io.vertx.core.json.JsonObject) VertxException(io.vertx.core.VertxException) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) IOException(java.io.IOException)

Aggregations

NotificationHistory (com.redhat.cloud.notifications.models.NotificationHistory)16 Endpoint (com.redhat.cloud.notifications.models.Endpoint)14 Event (com.redhat.cloud.notifications.models.Event)10 JsonObject (io.vertx.core.json.JsonObject)9 LocalDateTime (java.time.LocalDateTime)8 Test (org.junit.jupiter.api.Test)8 Action (com.redhat.cloud.notifications.ingress.Action)7 List (java.util.List)7 Inject (javax.inject.Inject)7 Context (com.redhat.cloud.notifications.ingress.Context)6 QuarkusTest (io.quarkus.test.junit.QuarkusTest)6 EndpointType (com.redhat.cloud.notifications.models.EndpointType)5 MockServerLifecycleManager (com.redhat.cloud.notifications.MockServerLifecycleManager)4 StatelessSessionFactory (com.redhat.cloud.notifications.db.StatelessSessionFactory)4 Metadata (com.redhat.cloud.notifications.ingress.Metadata)4 Payload (com.redhat.cloud.notifications.ingress.Payload)4 Arrays (java.util.Arrays)4 Map (java.util.Map)4 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)4 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)4