From 0a5a21ac0faf206263097fd3f464f8ee0f659d3f Mon Sep 17 00:00:00 2001 From: brianwyka Date: Mon, 19 Jul 2021 08:22:49 -0700 Subject: [PATCH] Add error handling distinction to ContentHttpvalidated --- .../enforcer/file/http/ContentHttpValidated.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/enforcer/file/common/src/main/java/com/optum/sourcehawk/enforcer/file/http/ContentHttpValidated.java b/enforcer/file/common/src/main/java/com/optum/sourcehawk/enforcer/file/http/ContentHttpValidated.java index 2fd40fe..f4f002c 100644 --- a/enforcer/file/common/src/main/java/com/optum/sourcehawk/enforcer/file/http/ContentHttpValidated.java +++ b/enforcer/file/common/src/main/java/com/optum/sourcehawk/enforcer/file/http/ContentHttpValidated.java @@ -28,7 +28,8 @@ public class ContentHttpValidated extends AbstractFileEnforcer { private static final int DEFAULT_BUFFER_SIZE = 8192; - private static final String HTTP_RESPONSE_ERROR_MESSAGE_TEMPLATE = "Validation failed with HTTP response code [%d] and message [%s]"; + private static final String HTTP_RESPONSE_VALIDATION_MESSAGE_TEMPLATE = "Validation failed with reason: %s"; + private static final String HTTP_RESPONSE_ERROR_MESSAGE_TEMPLATE = "HTTP [%s] error performing validation: %s"; /** * The URL in which to make a POST request, to validate the file contents @@ -62,15 +63,19 @@ protected EnforcerResult enforceInternal(final @NonNull InputStream actualFileIn outputStream.write(buffer, 0, read); } } - val responseCode = httpUrlConnection.getResponseCode(); - if (responseCode >= 200 && responseCode < 300) { + val responseCode = String.valueOf(httpUrlConnection.getResponseCode()); + if (responseCode.startsWith("2")) { return EnforcerResult.passed(); } try (val errorStream = httpUrlConnection.getErrorStream(); val bufferedReader = new BufferedReader(new InputStreamReader(errorStream))) { val responseMessage = bufferedReader.lines() .collect(Collectors.joining()); - return EnforcerResult.failed(String.format(HTTP_RESPONSE_ERROR_MESSAGE_TEMPLATE, responseCode, responseMessage)); + if (responseCode.startsWith("4")) { + return EnforcerResult.failed(String.format(HTTP_RESPONSE_VALIDATION_MESSAGE_TEMPLATE, responseMessage)); + } else { + return EnforcerResult.failed(String.format(HTTP_RESPONSE_ERROR_MESSAGE_TEMPLATE, responseCode, responseMessage)); + } } }