@ -20,6 +20,7 @@ import java.nio.charset.Charset;
@@ -20,6 +20,7 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets ;
import org.springframework.http.HttpHeaders ;
import org.springframework.http.HttpRequest ;
import org.springframework.http.HttpStatus ;
import org.springframework.lang.Nullable ;
@ -44,6 +45,9 @@ public class WebClientResponseException extends WebClientException {
@@ -44,6 +45,9 @@ public class WebClientResponseException extends WebClientException {
private final Charset responseCharset ;
@Nullable
private final HttpRequest request ;
/ * *
* Constructor with response data only , and a default message .
@ -52,22 +56,44 @@ public class WebClientResponseException extends WebClientException {
@@ -52,22 +56,44 @@ public class WebClientResponseException extends WebClientException {
public WebClientResponseException ( int statusCode , String statusText ,
@Nullable HttpHeaders headers , @Nullable byte [ ] body , @Nullable Charset charset ) {
this ( statusCode + " " + statusText , statusCode , statusText , headers , body , charset ) ;
this ( statusCode , statusText , headers , body , charset , null ) ;
}
/ * *
* Constructor with response data only , and a default message .
* @since 5 . 1 . 4
* /
public WebClientResponseException ( int statusCode , String statusText ,
@Nullable HttpHeaders headers , @Nullable byte [ ] body , @Nullable Charset charset ,
@Nullable HttpRequest request ) {
this ( statusCode + " " + statusText , statusCode , statusText , headers , body , charset , request ) ;
}
/ * *
* Constructor with a prepared message .
* /
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 ) ;
}
/ * *
* Constructor with a prepared message .
* @since 5 . 1 . 4
* /
public WebClientResponseException ( String message , int statusCode , String statusText ,
@Nullable HttpHeaders headers , @Nullable byte [ ] responsebody , @Nullable Charset charset ) {
@Nullable HttpHeaders headers , @Nullable byte [ ] responseBody , @Nullable Charset charset ,
@Nullable HttpRequest request ) {
super ( message ) ;
this . statusCode = statusCode ;
this . statusText = statusText ;
this . headers = ( headers ! = null ? headers : HttpHeaders . EMPTY ) ;
this . responseBody = ( responsebody ! = null ? responsebody : new byte [ 0 ] ) ;
this . responseBody = ( responseB ody ! = null ? responseB ody : new byte [ 0 ] ) ;
this . responseCharset = ( charset ! = null ? charset : StandardCharsets . ISO_8859_1 ) ;
this . request = request ;
}
@ -114,6 +140,14 @@ public class WebClientResponseException extends WebClientException {
@@ -114,6 +140,14 @@ public class WebClientResponseException extends WebClientException {
return new String ( this . responseBody , this . responseCharset ) ;
}
/ * *
* Return the corresponding request .
* @since 5 . 1 . 4
* /
@Nullable
public HttpRequest getRequest ( ) {
return this . request ;
}
/ * *
* Create { @code WebClientResponseException } or an HTTP status specific sub - class .
@ -122,44 +156,55 @@ public class WebClientResponseException extends WebClientException {
@@ -122,44 +156,55 @@ public class WebClientResponseException extends WebClientException {
public static WebClientResponseException create (
int statusCode , String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
return create ( statusCode , statusText , headers , body , charset , null ) ;
}
/ * *
* Create { @code WebClientResponseException } or an HTTP status specific sub - class .
* @since 5 . 1 . 4
* /
public static WebClientResponseException create (
int statusCode , String statusText , HttpHeaders headers , byte [ ] body ,
@Nullable Charset charset , @Nullable HttpRequest request ) {
HttpStatus httpStatus = HttpStatus . resolve ( statusCode ) ;
if ( httpStatus ! = null ) {
switch ( httpStatus ) {
case BAD_REQUEST :
return new WebClientResponseException . BadRequest ( statusText , headers , body , charset ) ;
return new WebClientResponseException . BadRequest ( statusText , headers , body , charset , request ) ;
case UNAUTHORIZED :
return new WebClientResponseException . Unauthorized ( statusText , headers , body , charset ) ;
return new WebClientResponseException . Unauthorized ( statusText , headers , body , charset , request ) ;
case FORBIDDEN :
return new WebClientResponseException . Forbidden ( statusText , headers , body , charset ) ;
return new WebClientResponseException . Forbidden ( statusText , headers , body , charset , request ) ;
case NOT_FOUND :
return new WebClientResponseException . NotFound ( statusText , headers , body , charset ) ;
return new WebClientResponseException . NotFound ( statusText , headers , body , charset , request ) ;
case METHOD_NOT_ALLOWED :
return new WebClientResponseException . MethodNotAllowed ( statusText , headers , body , charset ) ;
return new WebClientResponseException . MethodNotAllowed ( statusText , headers , body , charset , request ) ;
case NOT_ACCEPTABLE :
return new WebClientResponseException . NotAcceptable ( statusText , headers , body , charset ) ;
return new WebClientResponseException . NotAcceptable ( statusText , headers , body , charset , request ) ;
case CONFLICT :
return new WebClientResponseException . Conflict ( statusText , headers , body , charset ) ;
return new WebClientResponseException . Conflict ( statusText , headers , body , charset , request ) ;
case GONE :
return new WebClientResponseException . Gone ( statusText , headers , body , charset ) ;
return new WebClientResponseException . Gone ( statusText , headers , body , charset , request ) ;
case UNSUPPORTED_MEDIA_TYPE :
return new WebClientResponseException . UnsupportedMediaType ( statusText , headers , body , charset ) ;
return new WebClientResponseException . UnsupportedMediaType ( statusText , headers , body , charset , request ) ;
case TOO_MANY_REQUESTS :
return new WebClientResponseException . TooManyRequests ( statusText , headers , body , charset ) ;
return new WebClientResponseException . TooManyRequests ( statusText , headers , body , charset , request ) ;
case UNPROCESSABLE_ENTITY :
return new WebClientResponseException . UnprocessableEntity ( statusText , headers , body , charset ) ;
return new WebClientResponseException . UnprocessableEntity ( statusText , headers , body , charset , request ) ;
case INTERNAL_SERVER_ERROR :
return new WebClientResponseException . InternalServerError ( statusText , headers , body , charset ) ;
return new WebClientResponseException . InternalServerError ( statusText , headers , body , charset , request ) ;
case NOT_IMPLEMENTED :
return new WebClientResponseException . NotImplemented ( statusText , headers , body , charset ) ;
return new WebClientResponseException . NotImplemented ( statusText , headers , body , charset , request ) ;
case BAD_GATEWAY :
return new WebClientResponseException . BadGateway ( statusText , headers , body , charset ) ;
return new WebClientResponseException . BadGateway ( statusText , headers , body , charset , request ) ;
case SERVICE_UNAVAILABLE :
return new WebClientResponseException . ServiceUnavailable ( statusText , headers , body , charset ) ;
return new WebClientResponseException . ServiceUnavailable ( statusText , headers , body , charset , request ) ;
case GATEWAY_TIMEOUT :
return new WebClientResponseException . GatewayTimeout ( statusText , headers , body , charset ) ;
return new WebClientResponseException . GatewayTimeout ( statusText , headers , body , charset , request ) ;
}
}
return new WebClientResponseException ( statusCode , statusText , headers , body , charset ) ;
return new WebClientResponseException ( statusCode , statusText , headers , body , charset , request ) ;
}
@ -173,9 +218,11 @@ public class WebClientResponseException extends WebClientException {
@@ -173,9 +218,11 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class BadRequest extends WebClientResponseException {
BadRequest ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . BAD_REQUEST . value ( ) , statusText , headers , body , charset ) ;
BadRequest ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ,
@Nullable HttpRequest request ) {
super ( HttpStatus . BAD_REQUEST . value ( ) , statusText , headers , body , charset , request ) ;
}
}
/ * *
@ -185,8 +232,9 @@ public class WebClientResponseException extends WebClientException {
@@ -185,8 +232,9 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class Unauthorized extends WebClientResponseException {
Unauthorized ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . UNAUTHORIZED . value ( ) , statusText , headers , body , charset ) ;
Unauthorized ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ,
@Nullable HttpRequest request ) {
super ( HttpStatus . UNAUTHORIZED . value ( ) , statusText , headers , body , charset , request ) ;
}
}
@ -197,8 +245,9 @@ public class WebClientResponseException extends WebClientException {
@@ -197,8 +245,9 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class Forbidden extends WebClientResponseException {
Forbidden ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . FORBIDDEN . value ( ) , statusText , headers , body , charset ) ;
Forbidden ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ,
@Nullable HttpRequest request ) {
super ( HttpStatus . FORBIDDEN . value ( ) , statusText , headers , body , charset , request ) ;
}
}
@ -209,8 +258,9 @@ public class WebClientResponseException extends WebClientException {
@@ -209,8 +258,9 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class NotFound extends WebClientResponseException {
NotFound ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . NOT_FOUND . value ( ) , statusText , headers , body , charset ) ;
NotFound ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ,
@Nullable HttpRequest request ) {
super ( HttpStatus . NOT_FOUND . value ( ) , statusText , headers , body , charset , request ) ;
}
}
@ -221,8 +271,10 @@ public class WebClientResponseException extends WebClientException {
@@ -221,8 +271,10 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class MethodNotAllowed extends WebClientResponseException {
MethodNotAllowed ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . METHOD_NOT_ALLOWED . value ( ) , statusText , headers , body , charset ) ;
MethodNotAllowed ( String statusText , HttpHeaders headers , byte [ ] body ,
@Nullable Charset charset , @Nullable HttpRequest request ) {
super ( HttpStatus . METHOD_NOT_ALLOWED . value ( ) , statusText , headers , body , charset ,
request ) ;
}
}
@ -233,8 +285,9 @@ public class WebClientResponseException extends WebClientException {
@@ -233,8 +285,9 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class NotAcceptable extends WebClientResponseException {
NotAcceptable ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . NOT_ACCEPTABLE . value ( ) , statusText , headers , body , charset ) ;
NotAcceptable ( String statusText , HttpHeaders headers , byte [ ] body ,
@Nullable Charset charset , @Nullable HttpRequest request ) {
super ( HttpStatus . NOT_ACCEPTABLE . value ( ) , statusText , headers , body , charset , request ) ;
}
}
@ -245,8 +298,9 @@ public class WebClientResponseException extends WebClientException {
@@ -245,8 +298,9 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class Conflict extends WebClientResponseException {
Conflict ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . CONFLICT . value ( ) , statusText , headers , body , charset ) ;
Conflict ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ,
@Nullable HttpRequest request ) {
super ( HttpStatus . CONFLICT . value ( ) , statusText , headers , body , charset , request ) ;
}
}
@ -257,8 +311,9 @@ public class WebClientResponseException extends WebClientException {
@@ -257,8 +311,9 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class Gone extends WebClientResponseException {
Gone ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . GONE . value ( ) , statusText , headers , body , charset ) ;
Gone ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ,
@Nullable HttpRequest request ) {
super ( HttpStatus . GONE . value ( ) , statusText , headers , body , charset , request ) ;
}
}
@ -269,8 +324,11 @@ public class WebClientResponseException extends WebClientException {
@@ -269,8 +324,11 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class UnsupportedMediaType extends WebClientResponseException {
UnsupportedMediaType ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . UNSUPPORTED_MEDIA_TYPE . value ( ) , statusText , headers , body , charset ) ;
UnsupportedMediaType ( String statusText , HttpHeaders headers , byte [ ] body ,
@Nullable Charset charset , @Nullable HttpRequest request ) {
super ( HttpStatus . UNSUPPORTED_MEDIA_TYPE . value ( ) , statusText , headers , body , charset ,
request ) ;
}
}
@ -281,8 +339,10 @@ public class WebClientResponseException extends WebClientException {
@@ -281,8 +339,10 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class UnprocessableEntity extends WebClientResponseException {
UnprocessableEntity ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . UNPROCESSABLE_ENTITY . value ( ) , statusText , headers , body , charset ) ;
UnprocessableEntity ( String statusText , HttpHeaders headers , byte [ ] body ,
@Nullable Charset charset , @Nullable HttpRequest request ) {
super ( HttpStatus . UNPROCESSABLE_ENTITY . value ( ) , statusText , headers , body , charset ,
request ) ;
}
}
@ -293,8 +353,10 @@ public class WebClientResponseException extends WebClientException {
@@ -293,8 +353,10 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class TooManyRequests extends WebClientResponseException {
TooManyRequests ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . TOO_MANY_REQUESTS . value ( ) , statusText , headers , body , charset ) ;
TooManyRequests ( String statusText , HttpHeaders headers , byte [ ] body ,
@Nullable Charset charset , @Nullable HttpRequest request ) {
super ( HttpStatus . TOO_MANY_REQUESTS . value ( ) , statusText , headers , body , charset ,
request ) ;
}
}
@ -309,8 +371,10 @@ public class WebClientResponseException extends WebClientException {
@@ -309,8 +371,10 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class InternalServerError extends WebClientResponseException {
InternalServerError ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . INTERNAL_SERVER_ERROR . value ( ) , statusText , headers , body , charset ) ;
InternalServerError ( String statusText , HttpHeaders headers , byte [ ] body ,
@Nullable Charset charset , @Nullable HttpRequest request ) {
super ( HttpStatus . INTERNAL_SERVER_ERROR . value ( ) , statusText , headers , body , charset ,
request ) ;
}
}
@ -321,8 +385,9 @@ public class WebClientResponseException extends WebClientException {
@@ -321,8 +385,9 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class NotImplemented extends WebClientResponseException {
NotImplemented ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . NOT_IMPLEMENTED . value ( ) , statusText , headers , body , charset ) ;
NotImplemented ( String statusText , HttpHeaders headers , byte [ ] body ,
@Nullable Charset charset , @Nullable HttpRequest request ) {
super ( HttpStatus . NOT_IMPLEMENTED . value ( ) , statusText , headers , body , charset , request ) ;
}
}
@ -333,8 +398,9 @@ public class WebClientResponseException extends WebClientException {
@@ -333,8 +398,9 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class BadGateway extends WebClientResponseException {
BadGateway ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . BAD_GATEWAY . value ( ) , statusText , headers , body , charset ) ;
BadGateway ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ,
@Nullable HttpRequest request ) {
super ( HttpStatus . BAD_GATEWAY . value ( ) , statusText , headers , body , charset , request ) ;
}
}
@ -345,8 +411,10 @@ public class WebClientResponseException extends WebClientException {
@@ -345,8 +411,10 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class ServiceUnavailable extends WebClientResponseException {
ServiceUnavailable ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . SERVICE_UNAVAILABLE . value ( ) , statusText , headers , body , charset ) ;
ServiceUnavailable ( String statusText , HttpHeaders headers , byte [ ] body ,
@Nullable Charset charset , @Nullable HttpRequest request ) {
super ( HttpStatus . SERVICE_UNAVAILABLE . value ( ) , statusText , headers , body , charset ,
request ) ;
}
}
@ -357,8 +425,10 @@ public class WebClientResponseException extends WebClientException {
@@ -357,8 +425,10 @@ public class WebClientResponseException extends WebClientException {
@SuppressWarnings ( "serial" )
public static class GatewayTimeout extends WebClientResponseException {
GatewayTimeout ( String statusText , HttpHeaders headers , byte [ ] body , @Nullable Charset charset ) {
super ( HttpStatus . GATEWAY_TIMEOUT . value ( ) , statusText , headers , body , charset ) ;
GatewayTimeout ( String statusText , HttpHeaders headers , byte [ ] body ,
@Nullable Charset charset , @Nullable HttpRequest request ) {
super ( HttpStatus . GATEWAY_TIMEOUT . value ( ) , statusText , headers , body , charset ,
request ) ;
}
}