Copy HttpHeaders to ensure serializability

Not all HttpHeaders implementations are serializable. This commit
ensures that WebClientRequestException and WebClientResponseException
are serializable, by copying any non-serializable HttpHeaders into a
new, serializable, instance.

Closes gh-28321
This commit is contained in:
Arjen Poutsma
2022-07-28 13:55:43 +02:00
parent 95a400abfb
commit 97ea8a6789
2 changed files with 30 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -47,9 +47,20 @@ public class WebClientRequestException extends WebClientException {
this.method = method; this.method = method;
this.uri = uri; this.uri = uri;
this.headers = headers; this.headers = copy(headers);
} }
/**
* Not all {@code HttpHeaders} implementations are serializable, so we
* make a copy to ensure that {@code WebClientResponseException} is.
*/
private static HttpHeaders copy(HttpHeaders headers) {
HttpHeaders result = new HttpHeaders();
result.putAll(headers);
return result;
}
/** /**
* Return the HTTP request method. * Return the HTTP request method.
*/ */

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -96,12 +96,27 @@ public class WebClientResponseException extends WebClientException {
this.statusCode = statusCode; this.statusCode = statusCode;
this.statusText = statusText; this.statusText = statusText;
this.headers = (headers != null ? headers : HttpHeaders.EMPTY); this.headers = copy(headers);
this.responseBody = (responseBody != null ? responseBody : new byte[0]); this.responseBody = (responseBody != null ? responseBody : new byte[0]);
this.responseCharset = charset; this.responseCharset = charset;
this.request = request; this.request = request;
} }
/**
* Not all {@code HttpHeaders} implementations are serializable, so we
* make a copy to ensure that {@code WebClientResponseException} is.
*/
private static HttpHeaders copy(@Nullable HttpHeaders headers) {
if (headers == null) {
return HttpHeaders.EMPTY;
}
else {
HttpHeaders result = new HttpHeaders();
result.putAll(headers);
return result;
}
}
/** /**
* Return the HTTP status code value. * Return the HTTP status code value.