Skip to content

Commit

Permalink
Remove thread unsafe statusCode and responseHeaders instance variable…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
aaronforest-wf authored Jul 8, 2024
1 parent a3912b7 commit 8ab3bb4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.function.Supplier;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -125,8 +126,8 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {

private Map<String, Authentication> authentications;

private int statusCode;
private Map<String, List<String>> responseHeaders;
private Map<Long, Integer> lastStatusCodeByThread = new ConcurrentHashMap<>();
private Map<Long, Map<String, List<String>>> lastResponseHeadersByThread = new ConcurrentHashMap<>();

private DateFormat dateFormat;

Expand Down Expand Up @@ -279,16 +280,18 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
*
* @return Status code
*/
@Deprecated
public int getStatusCode() {
return statusCode;
return lastStatusCodeByThread.get(Thread.currentThread().getId());
}

/**
* Gets the response headers of the previous request
* @return Response headers
*/
@Deprecated
public Map<String, List<String>> getResponseHeaders() {
return responseHeaders;
return lastResponseHeadersByThread.get(Thread.currentThread().getId());
}

/**
Expand Down Expand Up @@ -863,6 +866,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
// convert input stream to string
return (T) EntityUtils.toString(entity);
} else {
Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
throw new ApiException(
"Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'",
response.getCode(),
Expand Down Expand Up @@ -1008,12 +1012,15 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
}

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) {
return null;
}

responseHeaders = transformResponseHeaders(response.getHeaders());
Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
lastResponseHeadersByThread.put(Thread.currentThread().getId(), responseHeaders);

if (isSuccessfulStatus(statusCode)) {
return this.deserialize(response, returnType);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.Date;
import java.util.function.Supplier;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -103,8 +104,8 @@ public class ApiClient extends JavaTimeFormatter {

private Map<String, Authentication> authentications;

private int statusCode;
private Map<String, List<String>> responseHeaders;
private Map<Long, Integer> lastStatusCodeByThread = new ConcurrentHashMap<>();
private Map<Long, Map<String, List<String>>> lastResponseHeadersByThread = new ConcurrentHashMap<>();

private DateFormat dateFormat;

Expand Down Expand Up @@ -250,16 +251,18 @@ public ApiClient setServerVariables(Map<String, String> serverVariables) {
*
* @return Status code
*/
@Deprecated
public int getStatusCode() {
return statusCode;
return lastStatusCodeByThread.get(Thread.currentThread().getId());
}

/**
* Gets the response headers of the previous request
* @return Response headers
*/
@Deprecated
public Map<String, List<String>> getResponseHeaders() {
return responseHeaders;
return lastResponseHeadersByThread.get(Thread.currentThread().getId());
}

/**
Expand Down Expand Up @@ -781,6 +784,7 @@ public <T> T deserialize(CloseableHttpResponse response, TypeReference<T> valueT
// convert input stream to string
return (T) EntityUtils.toString(entity);
} else {
Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
throw new ApiException(
"Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'",
response.getCode(),
Expand Down Expand Up @@ -926,12 +930,15 @@ protected Cookie buildCookie(String key, String value, URI uri) {
}

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) {
return null;
}

responseHeaders = transformResponseHeaders(response.getHeaders());
Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
lastResponseHeadersByThread.put(Thread.currentThread().getId(), responseHeaders);

if (isSuccessfulStatus(statusCode)) {
return this.deserialize(response, returnType);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.Date;
import java.util.function.Supplier;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -148,8 +149,8 @@ public class ApiClient extends JavaTimeFormatter {

private Map<String, Authentication> authentications;

private int statusCode;
private Map<String, List<String>> responseHeaders;
private Map<Long, Integer> lastStatusCodeByThread = new ConcurrentHashMap<>();
private Map<Long, Map<String, List<String>>> lastResponseHeadersByThread = new ConcurrentHashMap<>();

private DateFormat dateFormat;

Expand Down Expand Up @@ -298,16 +299,18 @@ public ApiClient setServerVariables(Map<String, String> serverVariables) {
*
* @return Status code
*/
@Deprecated
public int getStatusCode() {
return statusCode;
return lastStatusCodeByThread.get(Thread.currentThread().getId());
}

/**
* Gets the response headers of the previous request
* @return Response headers
*/
@Deprecated
public Map<String, List<String>> getResponseHeaders() {
return responseHeaders;
return lastResponseHeadersByThread.get(Thread.currentThread().getId());
}

/**
Expand Down Expand Up @@ -874,6 +877,7 @@ public <T> T deserialize(CloseableHttpResponse response, TypeReference<T> valueT
// convert input stream to string
return (T) EntityUtils.toString(entity);
} else {
Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
throw new ApiException(
"Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'",
response.getCode(),
Expand Down Expand Up @@ -1019,12 +1023,15 @@ protected Cookie buildCookie(String key, String value, URI uri) {
}

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) {
return null;
}

responseHeaders = transformResponseHeaders(response.getHeaders());
Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
lastResponseHeadersByThread.put(Thread.currentThread().getId(), responseHeaders);

if (isSuccessfulStatus(statusCode)) {
return this.deserialize(response, returnType);
} else {
Expand Down

0 comments on commit 8ab3bb4

Please sign in to comment.