From 52a1abfb87822b0d84b17bd2633740c7df987579 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Fri, 18 Aug 2017 17:43:57 +0200 Subject: [PATCH] Fields of InvalidResposneException made final Deprecated pseudo-builder methods withStatusCode() and withResponse() in favor of additional constructor arguments to enforce fixed state. --- .../jvault/connector/HTTPVaultConnector.java | 10 +-- .../exception/InvalidResponseException.java | 89 +++++++++++++++++-- 2 files changed, 88 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/stklcode/jvault/connector/HTTPVaultConnector.java b/src/main/java/de/stklcode/jvault/connector/HTTPVaultConnector.java index d3d7f3c..f7b87d0 100644 --- a/src/main/java/de/stklcode/jvault/connector/HTTPVaultConnector.java +++ b/src/main/java/de/stklcode/jvault/connector/HTTPVaultConnector.java @@ -814,7 +814,7 @@ public class HTTPVaultConnector implements VaultConnector { new InputStreamReader(response.getEntity().getContent()))) { return br.lines().collect(Collectors.joining("\n")); } catch (IOException ignored) { - throw new InvalidResponseException("Could not parse response body").withStatusCode(200); + throw new InvalidResponseException("Could not parse response body", 200); } case 204: return ""; @@ -827,8 +827,6 @@ public class HTTPVaultConnector implements VaultConnector { return request(base, retries - 1); } else { /* Fail on different error code and/or no retries left */ - InvalidResponseException ex = new InvalidResponseException("Invalid response code") - .withStatusCode(response.getStatusLine().getStatusCode()); if (response.getEntity() != null) { try (BufferedReader br = new BufferedReader( new InputStreamReader(response.getEntity().getContent()))) { @@ -837,12 +835,14 @@ public class HTTPVaultConnector implements VaultConnector { /* Check for "permission denied" response */ if (!er.getErrors().isEmpty() && er.getErrors().get(0).equals("permission denied")) throw new PermissionDeniedException(); - throw ex.withResponse(er.toString()); + throw new InvalidResponseException("Invalid response code", + response.getStatusLine().getStatusCode(), er.toString()); } catch (IOException ignored) { // Exception ignored. } } - throw ex; + throw new InvalidResponseException("Invalid response code", + response.getStatusLine().getStatusCode()); } } } catch (IOException e) { diff --git a/src/main/java/de/stklcode/jvault/connector/exception/InvalidResponseException.java b/src/main/java/de/stklcode/jvault/connector/exception/InvalidResponseException.java index 7eec2dc..2f23cb3 100644 --- a/src/main/java/de/stklcode/jvault/connector/exception/InvalidResponseException.java +++ b/src/main/java/de/stklcode/jvault/connector/exception/InvalidResponseException.java @@ -24,13 +24,15 @@ package de.stklcode.jvault.connector.exception; * @since 0.1 */ public final class InvalidResponseException extends VaultConnectorException { - private Integer statusCode; - private String response; + private final Integer statusCode; + private final String response; /** * Constructs a new empty exception. */ public InvalidResponseException() { + this.statusCode = null; + this.response = null; } /** @@ -40,6 +42,8 @@ public final class InvalidResponseException extends VaultConnectorException { */ public InvalidResponseException(final String message) { super(message); + this.statusCode = null; + this.response = null; } /** @@ -49,6 +53,8 @@ public final class InvalidResponseException extends VaultConnectorException { */ public InvalidResponseException(final Throwable cause) { super(cause); + this.statusCode = null; + this.response = null; } /** @@ -59,6 +65,75 @@ public final class InvalidResponseException extends VaultConnectorException { */ public InvalidResponseException(final String message, final Throwable cause) { super(message, cause); + this.statusCode = null; + this.response = null; + } + + /** + * Constructs a new exception with the specified detail message and status code. + *

+ * The HTTP status code can be retrieved by {@link #getStatusCode()} later. + * + * @param message the detail message + * @param statusCode status code of the HTTP response + * @since 0.6.2 + */ + public InvalidResponseException(final String message, final Integer statusCode) { + super(message); + this.statusCode = statusCode; + this.response = null; + } + + /** + * Constructs a new exception with the specified detail message, cause and status code. + *

+ * The HTTP status code can be retrieved by {@link #getStatusCode()} later. + * + * @param message the detail message + * @param statusCode status code of the HTTP response + * @param cause the cause + * @since 0.6.2 + */ + public InvalidResponseException(final String message, final Integer statusCode, final Throwable cause) { + this(message, statusCode, null, cause); + } + + /** + * Constructs a new exception with the specified detail message, cause and status code. + *

+ * The HTTP status code can be retrieved by {@link #getStatusCode()} later. + * + * @param message the detail message + * @param statusCode status code of the HTTP response + * @param response HTTP response string + * @since 0.6.2 + */ + public InvalidResponseException(final String message, + final Integer statusCode, + final String response) { + super(message); + this.statusCode = statusCode; + this.response = response; + } + + /** + * Constructs a new exception with the specified detail message, cause and status code. + *

+ * The HTTP status code can be retrieved by {@link #getStatusCode()} later. + * + * @param message the detail message + * @param statusCode status code of the HTTP response + * @param response HTTP response string + * @param cause the cause + * @since 0.6.2 + */ + public InvalidResponseException(final String message, + final Integer statusCode, + final String response, + final Throwable cause) { + super(message, cause); + this.statusCode = statusCode; + this.response = response; } /** @@ -66,10 +141,11 @@ public final class InvalidResponseException extends VaultConnectorException { * * @param statusCode the status code * @return self + * @deprecated as of 0.6.2, use constructor with status code argument instead */ + @Deprecated public InvalidResponseException withStatusCode(final Integer statusCode) { - this.statusCode = statusCode; - return this; + return new InvalidResponseException(getMessage(), statusCode, getResponse(), getCause()); } /** @@ -77,10 +153,11 @@ public final class InvalidResponseException extends VaultConnectorException { * * @param response response text * @return self + * @deprecated use constructor with response argument instead */ + @Deprecated public InvalidResponseException withResponse(final String response) { - this.response = response; - return this; + return new InvalidResponseException(getMessage(), getStatusCode(), response, getCause()); } /**