From 970c76a8c57b713e2d75598a0f336d2cfec694e2 Mon Sep 17 00:00:00 2001 From: Trevor Swartz Date: Tue, 1 Aug 2023 10:39:47 -0400 Subject: [PATCH] Upgrading spring framework + removing CustomErrorHandler for spring impl --- pom.xml | 4 +- .../exception/CustomResponseErrorHandler.java | 90 ------------------- 2 files changed, 2 insertions(+), 92 deletions(-) delete mode 100644 src/main/java/com/bullhornsdk/data/exception/CustomResponseErrorHandler.java diff --git a/pom.xml b/pom.xml index 2e66e58b..3371e57a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bullhorn sdk-rest - 2.0.1 + 2.1.0 jar Bullhorn REST SDK @@ -39,7 +39,7 @@ UTF-8 2.10.0 - 4.2.6.RELEASE + 5.3.25 -Xdoclint:none 4.2.0 0.8.8 diff --git a/src/main/java/com/bullhornsdk/data/exception/CustomResponseErrorHandler.java b/src/main/java/com/bullhornsdk/data/exception/CustomResponseErrorHandler.java deleted file mode 100644 index c02167cc..00000000 --- a/src/main/java/com/bullhornsdk/data/exception/CustomResponseErrorHandler.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.bullhornsdk.data.exception; - -import java.io.IOException; -import java.io.InputStream; -import java.io.StringWriter; -import java.nio.charset.Charset; - -import org.apache.commons.io.IOUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.web.client.DefaultResponseErrorHandler; -import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.HttpServerErrorException; -import org.springframework.web.client.RestClientException; -import org.springframework.web.client.UnknownHttpStatusCodeException; - -/** - * Added to handle 400 and 500 errors. The DefaultResponseErrorHandler implementation does not return a responseBody. - * - * It appears that trying to convert from InputStream responseBody = response.getBody(); to a byteArray returns an empty byte[]. - * Converting to String and the to byte[] returns the expected result. - * - * @author magnus.palm - * - */ -public class CustomResponseErrorHandler extends DefaultResponseErrorHandler { - - private static Logger log = LogManager.getLogger(CustomResponseErrorHandler.class); - - @Override - public void handleError(ClientHttpResponse response) throws IOException { - - HttpStatus statusCode = getHttpStatusCode(response); - switch (statusCode.series()) { - case CLIENT_ERROR: - throw new HttpClientErrorException(statusCode, response.getStatusText(), response.getHeaders(), - getResponseBody(response), getCharset(response)); - case SERVER_ERROR: - throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(), - getResponseBody(response), getCharset(response)); - default: - throw new RestClientException("Unknown status code [" + statusCode + "]"); - } - } - - private byte[] getResponseBody(ClientHttpResponse response) { - try { - InputStream responseBody = response.getBody(); - - if (responseBody != null) { - - StringWriter writer = new StringWriter(); - IOUtils.copy(responseBody, writer, "UTF-8"); - String responseBodyString = writer.toString(); - - byte[] bytes = responseBodyString.getBytes(); - return bytes; - } - } catch (IOException ex) { - log.info("here"); - } - return new byte[0]; - } - - private Charset getCharset(ClientHttpResponse response) { - HttpHeaders headers = response.getHeaders(); - MediaType contentType = headers.getContentType(); - return contentType != null ? contentType.getCharSet() : null; - } - - private HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException { - HttpStatus statusCode; - try { - statusCode = response.getStatusCode(); - } catch (IllegalArgumentException ex) { - throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(), - response.getHeaders(), getResponseBody(response), getCharset(response)); - } - return statusCode; - } - - @Override - public boolean hasError(ClientHttpResponse response) throws IOException { - return super.hasError(response); - } -}