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