From 6b1a8452fa376828a6c64dd5c1b5ca702af36d35 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Tue, 10 May 2022 09:08:03 +0100 Subject: [PATCH] Polishing --- .../client/RestClientResponseException.java | 22 +-- .../client/WebClientResponseException.java | 144 +++++++++++------- 2 files changed, 101 insertions(+), 65 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java b/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java index 44ff2e2aa01..754633c7eeb 100644 --- a/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java +++ b/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,30 +54,34 @@ public class RestClientResponseException extends RestClientException { * Construct a new instance of with the given response data. * @param statusCode the raw status code value * @param statusText the status text - * @param responseHeaders the response headers (may be {@code null}) + * @param headers the response headers (may be {@code null}) * @param responseBody the response body content (may be {@code null}) * @param responseCharset the response body charset (may be {@code null}) */ - public RestClientResponseException(String message, int statusCode, String statusText, - @Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) { - this(message, HttpStatusCode.valueOf(statusCode), statusText, responseHeaders, responseBody, responseCharset); + public RestClientResponseException( + String message, int statusCode, String statusText, @Nullable HttpHeaders headers, + @Nullable byte[] responseBody, @Nullable Charset responseCharset) { + + this(message, HttpStatusCode.valueOf(statusCode), statusText, headers, responseBody, responseCharset); } /** * Construct a new instance of with the given response data. * @param statusCode the raw status code value * @param statusText the status text - * @param responseHeaders the response headers (may be {@code null}) + * @param headers the response headers (may be {@code null}) * @param responseBody the response body content (may be {@code null}) * @param responseCharset the response body charset (may be {@code null}) * @since 6.0 */ - public RestClientResponseException(String message, HttpStatusCode statusCode, String statusText, - @Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) { + public RestClientResponseException( + String message, HttpStatusCode statusCode, String statusText, @Nullable HttpHeaders headers, + @Nullable byte[] responseBody, @Nullable Charset responseCharset) { + super(message); this.statusCode = statusCode; this.statusText = statusText; - this.responseHeaders = responseHeaders; + this.responseHeaders = headers; this.responseBody = (responseBody != null ? responseBody : new byte[0]); this.responseCharset = (responseCharset != null ? responseCharset.name() : null); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java index aa114f45370..7b41a40ecef 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ import org.springframework.lang.Nullable; * @author Arjen Poutsma * @since 5.0 */ +@SuppressWarnings("RedundantSuppression") public class WebClientResponseException extends WebClientException { private static final long serialVersionUID = 4127543205414951611L; @@ -55,8 +56,9 @@ public class WebClientResponseException extends WebClientException { * Constructor with response data only, and a default message. * @since 5.1 */ - public WebClientResponseException(int statusCode, String statusText, - @Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset charset) { + public WebClientResponseException( + int statusCode, String statusText, @Nullable HttpHeaders headers, + @Nullable byte[] body, @Nullable Charset charset) { this(statusCode, statusText, headers, body, charset, null); } @@ -65,9 +67,9 @@ public class WebClientResponseException extends WebClientException { * Constructor with response data only, and a default message. * @since 5.1.4 */ - public WebClientResponseException(int status, String reasonPhrase, - @Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset charset, - @Nullable HttpRequest request) { + public WebClientResponseException( + int status, String reasonPhrase, @Nullable HttpHeaders headers, + @Nullable byte[] body, @Nullable Charset charset, @Nullable HttpRequest request) { this(HttpStatusCode.valueOf(status), reasonPhrase, headers, body, charset, request); } @@ -76,10 +78,12 @@ public class WebClientResponseException extends WebClientException { * Constructor with response data only, and a default message. * @since 6.0 */ - public WebClientResponseException(HttpStatusCode statusCode, String reasonPhrase, - @Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset charset, - @Nullable HttpRequest request) { - this(initMessage(statusCode, reasonPhrase, request), statusCode, reasonPhrase, headers, body, charset, request); + public WebClientResponseException( + HttpStatusCode statusCode, String reasonPhrase, @Nullable HttpHeaders headers, + @Nullable byte[] body, @Nullable Charset charset, @Nullable HttpRequest request) { + + this(initMessage(statusCode, reasonPhrase, request), + statusCode, reasonPhrase, headers, body, charset, request); } private static String initMessage(HttpStatusCode status, String reasonPhrase, @Nullable HttpRequest request) { @@ -90,8 +94,10 @@ public class WebClientResponseException extends WebClientException { /** * Constructor with a prepared message. */ - public WebClientResponseException(String message, int statusCode, String statusText, + public WebClientResponseException( + String message, int statusCode, String statusText, @Nullable HttpHeaders headers, @Nullable byte[] responseBody, @Nullable Charset charset) { + this(message, statusCode, statusText, headers, responseBody, charset, null); } @@ -99,9 +105,11 @@ public class WebClientResponseException extends WebClientException { * Constructor with a prepared message. * @since 5.1.4 */ - public WebClientResponseException(String message, int statusCode, String statusText, + public WebClientResponseException( + String message, int statusCode, String statusText, @Nullable HttpHeaders headers, @Nullable byte[] responseBody, @Nullable Charset charset, @Nullable HttpRequest request) { + this(message, HttpStatusCode.valueOf(statusCode), statusText, headers, responseBody, charset, request); @@ -210,8 +218,9 @@ public class WebClientResponseException extends WebClientException { * @since 5.1.4 */ public static WebClientResponseException create( - int statusCode, String statusText, HttpHeaders headers, byte[] body, - @Nullable Charset charset, @Nullable HttpRequest request) { + int statusCode, String statusText, HttpHeaders headers, + byte[] body, @Nullable Charset charset, @Nullable HttpRequest request) { + return create(HttpStatusCode.valueOf(statusCode), statusText, headers, body, charset, request); } @@ -220,8 +229,8 @@ public class WebClientResponseException extends WebClientException { * @since 6.0 */ public static WebClientResponseException create( - HttpStatusCode statusCode, String statusText, HttpHeaders headers, byte[] body, - @Nullable Charset charset, @Nullable HttpRequest request) { + HttpStatusCode statusCode, String statusText, HttpHeaders headers, + byte[] body, @Nullable Charset charset, @Nullable HttpRequest request) { if (statusCode instanceof HttpStatus httpStatus) { switch (httpStatus) { @@ -263,7 +272,6 @@ public class WebClientResponseException extends WebClientException { } - // Subclasses for specific, client-side, HTTP status codes /** @@ -273,8 +281,10 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class BadRequest extends WebClientResponseException { - BadRequest(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + BadRequest( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, @Nullable HttpRequest request) { + super(HttpStatus.BAD_REQUEST.value(), statusText, headers, body, charset, request); } @@ -287,8 +297,10 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class Unauthorized extends WebClientResponseException { - Unauthorized(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + Unauthorized( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, @Nullable HttpRequest request) { + super(HttpStatus.UNAUTHORIZED.value(), statusText, headers, body, charset, request); } } @@ -300,8 +312,10 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class Forbidden extends WebClientResponseException { - Forbidden(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + Forbidden( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, @Nullable HttpRequest request) { + super(HttpStatus.FORBIDDEN.value(), statusText, headers, body, charset, request); } } @@ -313,8 +327,10 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class NotFound extends WebClientResponseException { - NotFound(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + NotFound( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, @Nullable HttpRequest request) { + super(HttpStatus.NOT_FOUND.value(), statusText, headers, body, charset, request); } } @@ -326,10 +342,11 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class MethodNotAllowed extends WebClientResponseException { - MethodNotAllowed(String statusText, HttpHeaders headers, byte[] body, - @Nullable Charset charset, @Nullable HttpRequest request) { - super(HttpStatus.METHOD_NOT_ALLOWED.value(), statusText, headers, body, charset, - request); + MethodNotAllowed( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + @Nullable HttpRequest request) { + + super(HttpStatus.METHOD_NOT_ALLOWED.value(), statusText, headers, body, charset, request); } } @@ -340,8 +357,10 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class NotAcceptable extends WebClientResponseException { - NotAcceptable(String statusText, HttpHeaders headers, byte[] body, - @Nullable Charset charset, @Nullable HttpRequest request) { + NotAcceptable( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + @Nullable HttpRequest request) { + super(HttpStatus.NOT_ACCEPTABLE.value(), statusText, headers, body, charset, request); } } @@ -353,8 +372,10 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class Conflict extends WebClientResponseException { - Conflict(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + Conflict( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, @Nullable HttpRequest request) { + super(HttpStatus.CONFLICT.value(), statusText, headers, body, charset, request); } } @@ -366,8 +387,10 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class Gone extends WebClientResponseException { - Gone(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + Gone( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, @Nullable HttpRequest request) { + super(HttpStatus.GONE.value(), statusText, headers, body, charset, request); } } @@ -379,11 +402,11 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class UnsupportedMediaType extends WebClientResponseException { - UnsupportedMediaType(String statusText, HttpHeaders headers, byte[] body, - @Nullable Charset charset, @Nullable HttpRequest request) { + UnsupportedMediaType( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + @Nullable HttpRequest request) { - super(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value(), statusText, headers, body, charset, - request); + super(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value(), statusText, headers, body, charset, request); } } @@ -394,10 +417,11 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class UnprocessableEntity extends WebClientResponseException { - UnprocessableEntity(String statusText, HttpHeaders headers, byte[] body, - @Nullable Charset charset, @Nullable HttpRequest request) { - super(HttpStatus.UNPROCESSABLE_ENTITY.value(), statusText, headers, body, charset, - request); + UnprocessableEntity( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + @Nullable HttpRequest request) { + + super(HttpStatus.UNPROCESSABLE_ENTITY.value(), statusText, headers, body, charset, request); } } @@ -408,10 +432,11 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class TooManyRequests extends WebClientResponseException { - TooManyRequests(String statusText, HttpHeaders headers, byte[] body, - @Nullable Charset charset, @Nullable HttpRequest request) { - super(HttpStatus.TOO_MANY_REQUESTS.value(), statusText, headers, body, charset, - request); + TooManyRequests( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + @Nullable HttpRequest request) { + + super(HttpStatus.TOO_MANY_REQUESTS.value(), statusText, headers, body, charset, request); } } @@ -426,10 +451,11 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class InternalServerError extends WebClientResponseException { - InternalServerError(String statusText, HttpHeaders headers, byte[] body, - @Nullable Charset charset, @Nullable HttpRequest request) { - super(HttpStatus.INTERNAL_SERVER_ERROR.value(), statusText, headers, body, charset, - request); + InternalServerError( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + @Nullable HttpRequest request) { + + super(HttpStatus.INTERNAL_SERVER_ERROR.value(), statusText, headers, body, charset, request); } } @@ -440,8 +466,10 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class NotImplemented extends WebClientResponseException { - NotImplemented(String statusText, HttpHeaders headers, byte[] body, - @Nullable Charset charset, @Nullable HttpRequest request) { + NotImplemented( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + @Nullable HttpRequest request) { + super(HttpStatus.NOT_IMPLEMENTED.value(), statusText, headers, body, charset, request); } } @@ -453,8 +481,10 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class BadGateway extends WebClientResponseException { - BadGateway(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + BadGateway( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, @Nullable HttpRequest request) { + super(HttpStatus.BAD_GATEWAY.value(), statusText, headers, body, charset, request); } } @@ -466,10 +496,11 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class ServiceUnavailable extends WebClientResponseException { - ServiceUnavailable(String statusText, HttpHeaders headers, byte[] body, - @Nullable Charset charset, @Nullable HttpRequest request) { - super(HttpStatus.SERVICE_UNAVAILABLE.value(), statusText, headers, body, charset, - request); + ServiceUnavailable( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + @Nullable HttpRequest request) { + + super(HttpStatus.SERVICE_UNAVAILABLE.value(), statusText, headers, body, charset, request); } } @@ -480,10 +511,11 @@ public class WebClientResponseException extends WebClientException { @SuppressWarnings("serial") public static class GatewayTimeout extends WebClientResponseException { - GatewayTimeout(String statusText, HttpHeaders headers, byte[] body, - @Nullable Charset charset, @Nullable HttpRequest request) { - super(HttpStatus.GATEWAY_TIMEOUT.value(), statusText, headers, body, charset, - request); + GatewayTimeout( + String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset, + @Nullable HttpRequest request) { + + super(HttpStatus.GATEWAY_TIMEOUT.value(), statusText, headers, body, charset, request); } }