Skip to content

Commit

Permalink
fix: reworked HTTP status code handling for 2xx and 3xx ranges (MWTEL…
Browse files Browse the repository at this point in the history
…E-68, MWTELE-69)

This ensures any 2xx response is considered valid.
The client now follows redirects (3xx range).

JIRA: https://issues.redhat.com/browse/MWTELE-68
JIRA: https://issues.redhat.com/browse/MWTELE-69
  • Loading branch information
jponge committed May 2, 2023
1 parent cba303b commit f58c195
Showing 1 changed file with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ HttpClient getHttpClient() {
ProxySelector.of(new InetSocketAddress(conf.getHost(), conf.getPort())));
}

return clientBuilder.build();
return clientBuilder.followRedirects(HttpClient.Redirect.NORMAL).build();
}

@Override
Expand Down Expand Up @@ -138,35 +138,40 @@ protected void sendInsightsReportWithClient(
configuration,
() -> {
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
int statusCode = response.statusCode();
logger.debug(
"Red Hat Insights HTTP Client: status="
+ response.statusCode()
+ statusCode
+ ", body="
+ response.body());
switch (response.statusCode()) {
case 201:
logger.debug(
"Red Hat Insights - Advisor content type with no metadata accepted for"
+ " processing");
switch (statusCode / 100) {
case 2:
if (statusCode == 201) {
logger.debug(
"Red Hat Insights - Advisor content type with no metadata accepted for"
+ " processing");
} else {
logger.debug("Red Hat Insights - Payload was accepted for processing");
}
break;
case 202:
logger.debug("Red Hat Insights - Payload was accepted for processing");
break;
case 401:
throw new InsightsException(
ERROR_HTTP_SEND_AUTH_ERROR, "Authentication missing from request");
case 413:
throw new InsightsException(ERROR_HTTP_SEND_PAYLOAD, "Payload too large");
case 415:
throw new InsightsException(
ERROR_HTTP_SEND_INVALID_CONTENT_TYPE,
"Content type of payload is unsupported");
case 500:
case 503:
case 4:
switch (statusCode) {
case 401:
throw new InsightsException(
ERROR_HTTP_SEND_AUTH_ERROR, "Authentication missing from request");
case 413:
throw new InsightsException(ERROR_HTTP_SEND_PAYLOAD, "Payload too large");
case 415:
default:
throw new InsightsException(
ERROR_HTTP_SEND_INVALID_CONTENT_TYPE,
"Content type of payload is unsupported");
}
case 5:
default:
throw new InsightsException(
ERROR_HTTP_SEND_SERVER_ERROR,
"Request failed on the server with code: " + response.statusCode());
"Request failed on the server with code: " + statusCode);
}
});
try {
Expand Down

0 comments on commit f58c195

Please sign in to comment.