Skip to content

Commit

Permalink
fix: ensure Resteasy JAX-RS Response object closed (#13333)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <medgar@redhat.com>

Signed-off-by: Michael Edgar <medgar@redhat.com>
  • Loading branch information
MikeEdgar committed Sep 3, 2022
1 parent 2bfbb87 commit afd357b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,38 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {

Entity<?> entity = serialize(body, formParams, contentType);

try (Response response = invoke(invocationBuilder, method, entity)) {
statusCode = response.getStatusInfo().getStatusCode();
responseHeaders = buildResponseHeaders(response);
if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) {
return null;
} else if (response.getStatusInfo().getFamily().equals(Status.Family.SUCCESSFUL)) {
if (returnType == null)
return null;
else
return deserialize(response, returnType);
} else {
String message = "error";
String respBody = null;
if (response.hasEntity()) {
try {
respBody = String.valueOf(response.readEntity(String.class));
message = respBody;
} catch (RuntimeException e) {
// e.printStackTrace();
}
}
throw new ApiException(
response.getStatus(),
message,
buildResponseHeaders(response),
respBody);
}
}
}

private Response invoke(Invocation.Builder invocationBuilder, String method, Entity<?> entity) throws ApiException {
Response response = null;
if ("GET".equals(method)) {
Expand All @@ -694,36 +726,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
throw new ApiException(500, "unknown method type " + method);
}

statusCode = response.getStatusInfo().getStatusCode();
responseHeaders = buildResponseHeaders(response);

if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) {
return null;
} else if (response.getStatusInfo().getFamily().equals(Status.Family.SUCCESSFUL)) {
if (returnType == null)
return null;
else
return deserialize(response, returnType);
} else {
String message = "error";
String respBody = null;
if (response.hasEntity()) {
try {
respBody = String.valueOf(response.readEntity(String.class));
message = respBody;
} catch (RuntimeException e) {
// e.printStackTrace();
}
}
throw new ApiException(
response.getStatus(),
message,
buildResponseHeaders(response),
respBody);
}
return response;
}

/**
/**
* Build the Client used to make HTTP requests.
*/
private Client buildHttpClient(boolean debugging) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,38 @@ public <T> T invokeAPI(String path, String method, List<Pair> queryParams, Objec

Entity<?> entity = serialize(body, formParams, contentType);

try (Response response = invoke(invocationBuilder, method, entity)) {
statusCode = response.getStatusInfo().getStatusCode();
responseHeaders = buildResponseHeaders(response);

if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) {
return null;
} else if (response.getStatusInfo().getFamily().equals(Status.Family.SUCCESSFUL)) {
if (returnType == null)
return null;
else
return deserialize(response, returnType);
} else {
String message = "error";
String respBody = null;
if (response.hasEntity()) {
try {
respBody = String.valueOf(response.readEntity(String.class));
message = respBody;
} catch (RuntimeException e) {
// e.printStackTrace();
}
}
throw new ApiException(
response.getStatus(),
message,
buildResponseHeaders(response),
respBody);
}
}
}

private Response invoke(Invocation.Builder invocationBuilder, String method, Entity<?> entity) throws ApiException {
Response response = null;

if ("GET".equals(method)) {
Expand All @@ -688,36 +720,10 @@ public <T> T invokeAPI(String path, String method, List<Pair> queryParams, Objec
throw new ApiException(500, "unknown method type " + method);
}

statusCode = response.getStatusInfo().getStatusCode();
responseHeaders = buildResponseHeaders(response);

if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) {
return null;
} else if (response.getStatusInfo().getFamily().equals(Status.Family.SUCCESSFUL)) {
if (returnType == null)
return null;
else
return deserialize(response, returnType);
} else {
String message = "error";
String respBody = null;
if (response.hasEntity()) {
try {
respBody = String.valueOf(response.readEntity(String.class));
message = respBody;
} catch (RuntimeException e) {
// e.printStackTrace();
}
}
throw new ApiException(
response.getStatus(),
message,
buildResponseHeaders(response),
respBody);
}
return response;
}

/**
/**
* Build the Client used to make HTTP requests.
*/
private Client buildHttpClient(boolean debugging) {
Expand Down

0 comments on commit afd357b

Please sign in to comment.