Skip to content

Commit

Permalink
Use getRawStatusCode to don't get an exception on fake status code 999
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Nov 4, 2024
1 parent 6087feb commit 69d5cbf
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
public class ErrorResponseClientHttpResponse extends AbstractClientHttpResponse {
private final Exception exception;

/** HTTP code use in response for non HTTP errors. */
private static final int FAKE_HTTP_ERROR_CODE = 999;
/** HTTP code use in response for non HTTP errors, (Not Acceptable). */
private static final int FAKE_HTTP_ERROR_CODE = 406;

public ErrorResponseClientHttpResponse(final Exception e) {
assert e != null;
Expand Down Expand Up @@ -38,7 +38,8 @@ public int getRawStatusCode() {
@Nonnull
public String getStatusText() {
return String.format(
"%s: %s", this.exception.getClass().getSimpleName(), this.exception.getMessage());
"Not true HTTP code, %s: %s, see above error",
this.exception.getClass().getSimpleName(), this.exception.getMessage());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static final CoordinateReferenceSystem parseCoordinateReferenceSystem(
requestFactory.createRequest(new URI(uri), HttpMethod.GET);
try (ClientHttpResponse response = request.execute()) {

if (response.getStatusCode() == HttpStatus.OK) {
if (response.getRawStatusCode() == HttpStatus.OK.value()) {
final String wkt =
IOUtils.toString(response.getBody(), Constants.DEFAULT_ENCODING);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,15 @@ private boolean isResponseStatusCodeValid(
final String stringBody,
final String baseMetricName)
throws IOException {
if (httpResponse.getStatusCode() != HttpStatus.OK) {
if (httpResponse.getRawStatusCode() != HttpStatus.OK.value()) {
String message =
String.format(
"Invalid status code for %s (%d!=%d).With request headers:\n%s\n"
"Invalid status code for %s (%d!=%d), status: %s. With request headers:\n%s\n"
+ "The response was: '%s'\nWith response headers:\n%s",
request.getURI(),
httpResponse.getStatusCode().value(),
httpResponse.getRawStatusCode(),
HttpStatus.OK.value(),
httpResponse.getStatusText(),
String.join("\n", Utils.getPrintableHeadersList(request.getHeaders())),
httpResponse.getStatusText(),
String.join("\n", Utils.getPrintableHeadersList(httpResponse.getHeaders())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static Optional<Style> loadStyleAsURI(
final ClientHttpRequestFactory clientHttpRequestFactory,
final String styleRef,
final Function<byte[], @Nullable Optional<Style>> loadFunction) {
HttpStatus statusCode;
int statusCode;
final byte[] input;

URI uri;
Expand All @@ -47,13 +47,13 @@ public static Optional<Style> loadStyleAsURI(
try {
final ClientHttpRequest request = clientHttpRequestFactory.createRequest(uri, HttpMethod.GET);
try (ClientHttpResponse response = request.execute()) {
statusCode = response.getStatusCode();
statusCode = response.getRawStatusCode();
input = IOUtils.toByteArray(response.getBody());
}
} catch (Exception e) {
return Optional.empty();
}
if (statusCode == HttpStatus.OK) {
if (statusCode == HttpStatus.OK.value()) {
return loadFunction.apply(input);
} else {
return Optional.empty();
Expand Down
23 changes: 12 additions & 11 deletions core/src/main/java/org/mapfish/print/map/tiled/CoverageTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,18 @@ protected Tile compute() {

private Tile handleSpecialStatuses(
final ClientHttpResponse response, final String baseMetricName) throws IOException {
final HttpStatus statusCode = response.getStatusCode();
if (statusCode == HttpStatus.NO_CONTENT || statusCode == HttpStatus.NOT_FOUND) {
if (statusCode == HttpStatus.NOT_FOUND) {
final int httpStatusCode = response.getRawStatusCode();
if (httpStatusCode == HttpStatus.NO_CONTENT.value()
|| httpStatusCode == HttpStatus.NOT_FOUND.value()) {
if (httpStatusCode == HttpStatus.NOT_FOUND.value()) {
LOGGER.info(
"The request {} returns a not found status code, we consider it as an empty tile.",
this.tileRequest.getURI());
}
// Empty response, nothing special to do
return new Tile(null, getTileIndexX(), getTileIndexY());
} else if (statusCode != HttpStatus.OK) {
return handleNonOkStatus(statusCode, response, baseMetricName);
} else if (httpStatusCode != HttpStatus.OK.value()) {
return handleNonOkStatus(response, baseMetricName);
}
return null;
}
Expand All @@ -281,19 +282,19 @@ private BufferedImage getImageFromResponse(
return image;
}

private Tile handleNonOkStatus(
final HttpStatus statusCode, final ClientHttpResponse response, final String baseMetricName)
private Tile handleNonOkStatus(final ClientHttpResponse response, final String baseMetricName)
throws IOException {
final int httpStatusCode = response.getRawStatusCode();
String errorMessage =
String.format(
"Error making tile request: %s\n\tStatus: %s\n\tStatus message: %s",
this.tileRequest.getURI(), statusCode, response.getStatusText());
"Error making tile request: %s\n\tStatus: %d\n\tStatus message: %s",
this.tileRequest.getURI(), httpStatusCode, response.getStatusText());
LOGGER.debug(
String.format(
"Error making tile request: %s\nStatus: %s\n"
"Error making tile request: %s\nStatus: %d\n"
+ "Status message: %s\nServer:%s\nBody:\n%s",
this.tileRequest.getURI(),
statusCode,
httpStatusCode,
response.getStatusText(),
response.getHeaders().getFirst(HttpHeaders.SERVER),
IOUtils.toString(response.getBody(), StandardCharsets.UTF_8)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public BufferedImage resolve(final MfClientHttpRequestFactory requestFactory, fi

private BufferedImage getImageFromResponse(final ClientHttpResponse response, final URI url)
throws IOException {
if (response.getStatusCode() == HttpStatus.OK) {
if (response.getRawStatusCode() == HttpStatus.OK.value()) {
try {
final BufferedImage image = ImageIO.read(response.getBody());
if (image == null) {
Expand All @@ -97,7 +97,7 @@ private BufferedImage getImageFromResponse(final ClientHttpResponse response, fi
LOGGER.warn(
"Error loading the table row image: {}.\nStatus Code: {}\nStatus Text: {}",
url,
response.getStatusCode(),
response.getRawStatusCode(),
response.getStatusText());
}
return this.defaultImage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ private BufferedImage loadImage(final URI uri) {
try (Timer.Context ignored =
LegendProcessor.this.metricRegistry.timer(metricName).time()) {
try (ClientHttpResponse httpResponse = request.execute()) {
if (httpResponse.getStatusCode() == HttpStatus.OK) {
if (httpResponse.getRawStatusCode() == HttpStatus.OK.value()) {
image = ImageIO.read(httpResponse.getBody());
if (image == null) {
LOGGER.warn("There is no image in this response body {}", httpResponse.getBody());
Expand Down Expand Up @@ -422,7 +422,7 @@ private void logNotOkResponseStatus(final ClientHttpResponse httpResponse) throw
+ "\tResponse Text: {}\n"
+ "\tWith Headers:\n\t{}",
this.icon,
httpResponse.getStatusCode(),
httpResponse.getRawStatusCode(),
httpResponse.getStatusText(),
String.join("\n\t", Utils.getPrintableHeadersList(httpResponse.getHeaders())));
}
Expand Down

0 comments on commit 69d5cbf

Please sign in to comment.