Remove thread unsafe statusCode and responseHeaders instance variables from apache http ApiClient (#19054)

* remove thread unsafe statusCode and responseHeaders instance variables

* re-add status code and header getters for backwards compatibility

* add import

* whitespace cleanup

* use deprecated thread id getter for backwards compatibility with pre-19 java
This commit is contained in:
aaronforest-wf 2024-07-08 08:48:52 -05:00 committed by GitHub
parent a3912b7239
commit 8ab3bb491b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 18 deletions

View File

@ -50,6 +50,7 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -125,8 +126,8 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
private Map<String, Authentication> authentications; private Map<String, Authentication> authentications;
private int statusCode; private Map<Long, Integer> lastStatusCodeByThread = new ConcurrentHashMap<>();
private Map<String, List<String>> responseHeaders; private Map<Long, Map<String, List<String>>> lastResponseHeadersByThread = new ConcurrentHashMap<>();
private DateFormat dateFormat; private DateFormat dateFormat;
@ -279,16 +280,18 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
* *
* @return Status code * @return Status code
*/ */
@Deprecated
public int getStatusCode() { public int getStatusCode() {
return statusCode; return lastStatusCodeByThread.get(Thread.currentThread().getId());
} }
/** /**
* Gets the response headers of the previous request * Gets the response headers of the previous request
* @return Response headers * @return Response headers
*/ */
@Deprecated
public Map<String, List<String>> getResponseHeaders() { public Map<String, List<String>> getResponseHeaders() {
return responseHeaders; return lastResponseHeadersByThread.get(Thread.currentThread().getId());
} }
/** /**
@ -863,6 +866,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
// convert input stream to string // convert input stream to string
return (T) EntityUtils.toString(entity); return (T) EntityUtils.toString(entity);
} else { } else {
Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
throw new ApiException( throw new ApiException(
"Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'", "Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'",
response.getCode(), response.getCode(),
@ -1008,12 +1012,15 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
} }
protected <T> T processResponse(CloseableHttpResponse response, TypeReference<T> returnType) throws ApiException, IOException, ParseException { protected <T> T processResponse(CloseableHttpResponse response, TypeReference<T> returnType) throws ApiException, IOException, ParseException {
statusCode = response.getCode(); int statusCode = response.getCode();
lastStatusCodeByThread.put(Thread.currentThread().getId(), statusCode);
if (statusCode == HttpStatus.SC_NO_CONTENT) { if (statusCode == HttpStatus.SC_NO_CONTENT) {
return null; return null;
} }
responseHeaders = transformResponseHeaders(response.getHeaders()); Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
lastResponseHeadersByThread.put(Thread.currentThread().getId(), responseHeaders);
if (isSuccessfulStatus(statusCode)) { if (isSuccessfulStatus(statusCode)) {
return this.deserialize(response, returnType); return this.deserialize(response, returnType);
} else { } else {

View File

@ -56,6 +56,7 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -103,8 +104,8 @@ public class ApiClient extends JavaTimeFormatter {
private Map<String, Authentication> authentications; private Map<String, Authentication> authentications;
private int statusCode; private Map<Long, Integer> lastStatusCodeByThread = new ConcurrentHashMap<>();
private Map<String, List<String>> responseHeaders; private Map<Long, Map<String, List<String>>> lastResponseHeadersByThread = new ConcurrentHashMap<>();
private DateFormat dateFormat; private DateFormat dateFormat;
@ -250,16 +251,18 @@ public class ApiClient extends JavaTimeFormatter {
* *
* @return Status code * @return Status code
*/ */
@Deprecated
public int getStatusCode() { public int getStatusCode() {
return statusCode; return lastStatusCodeByThread.get(Thread.currentThread().getId());
} }
/** /**
* Gets the response headers of the previous request * Gets the response headers of the previous request
* @return Response headers * @return Response headers
*/ */
@Deprecated
public Map<String, List<String>> getResponseHeaders() { public Map<String, List<String>> getResponseHeaders() {
return responseHeaders; return lastResponseHeadersByThread.get(Thread.currentThread().getId());
} }
/** /**
@ -781,6 +784,7 @@ public class ApiClient extends JavaTimeFormatter {
// convert input stream to string // convert input stream to string
return (T) EntityUtils.toString(entity); return (T) EntityUtils.toString(entity);
} else { } else {
Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
throw new ApiException( throw new ApiException(
"Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'", "Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'",
response.getCode(), response.getCode(),
@ -926,12 +930,15 @@ public class ApiClient extends JavaTimeFormatter {
} }
protected <T> T processResponse(CloseableHttpResponse response, TypeReference<T> returnType) throws ApiException, IOException, ParseException { protected <T> T processResponse(CloseableHttpResponse response, TypeReference<T> returnType) throws ApiException, IOException, ParseException {
statusCode = response.getCode(); int statusCode = response.getCode();
lastStatusCodeByThread.put(Thread.currentThread().getId(), statusCode);
if (statusCode == HttpStatus.SC_NO_CONTENT) { if (statusCode == HttpStatus.SC_NO_CONTENT) {
return null; return null;
} }
responseHeaders = transformResponseHeaders(response.getHeaders()); Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
lastResponseHeadersByThread.put(Thread.currentThread().getId(), responseHeaders);
if (isSuccessfulStatus(statusCode)) { if (isSuccessfulStatus(statusCode)) {
return this.deserialize(response, returnType); return this.deserialize(response, returnType);
} else { } else {

View File

@ -56,6 +56,7 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -148,8 +149,8 @@ public class ApiClient extends JavaTimeFormatter {
private Map<String, Authentication> authentications; private Map<String, Authentication> authentications;
private int statusCode; private Map<Long, Integer> lastStatusCodeByThread = new ConcurrentHashMap<>();
private Map<String, List<String>> responseHeaders; private Map<Long, Map<String, List<String>>> lastResponseHeadersByThread = new ConcurrentHashMap<>();
private DateFormat dateFormat; private DateFormat dateFormat;
@ -298,16 +299,18 @@ public class ApiClient extends JavaTimeFormatter {
* *
* @return Status code * @return Status code
*/ */
@Deprecated
public int getStatusCode() { public int getStatusCode() {
return statusCode; return lastStatusCodeByThread.get(Thread.currentThread().getId());
} }
/** /**
* Gets the response headers of the previous request * Gets the response headers of the previous request
* @return Response headers * @return Response headers
*/ */
@Deprecated
public Map<String, List<String>> getResponseHeaders() { public Map<String, List<String>> getResponseHeaders() {
return responseHeaders; return lastResponseHeadersByThread.get(Thread.currentThread().getId());
} }
/** /**
@ -874,6 +877,7 @@ public class ApiClient extends JavaTimeFormatter {
// convert input stream to string // convert input stream to string
return (T) EntityUtils.toString(entity); return (T) EntityUtils.toString(entity);
} else { } else {
Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
throw new ApiException( throw new ApiException(
"Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'", "Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'",
response.getCode(), response.getCode(),
@ -1019,12 +1023,15 @@ public class ApiClient extends JavaTimeFormatter {
} }
protected <T> T processResponse(CloseableHttpResponse response, TypeReference<T> returnType) throws ApiException, IOException, ParseException { protected <T> T processResponse(CloseableHttpResponse response, TypeReference<T> returnType) throws ApiException, IOException, ParseException {
statusCode = response.getCode(); int statusCode = response.getCode();
lastStatusCodeByThread.put(Thread.currentThread().getId(), statusCode);
if (statusCode == HttpStatus.SC_NO_CONTENT) { if (statusCode == HttpStatus.SC_NO_CONTENT) {
return null; return null;
} }
responseHeaders = transformResponseHeaders(response.getHeaders()); Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
lastResponseHeadersByThread.put(Thread.currentThread().getId(), responseHeaders);
if (isSuccessfulStatus(statusCode)) { if (isSuccessfulStatus(statusCode)) {
return this.deserialize(response, returnType); return this.deserialize(response, returnType);
} else { } else {