Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ANCHOR-822] Fix OkHttp response leak #1502

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,34 @@ public GetCustomerResponse getCustomer(GetCustomerRequest customerRequest)
urlBuilder.addQueryParameter(key, value);
}
});

HttpUrl url = urlBuilder.build();

// Make request
Response response =
try (Response response =
PlatformIntegrationHelper.call(
httpClient,
PlatformIntegrationHelper.getRequestBuilder(authHelper).url(url).get().build());
String responseContent = PlatformIntegrationHelper.getContent(response);
PlatformIntegrationHelper.getRequestBuilder(authHelper).url(url).get().build())) {
String responseContent = PlatformIntegrationHelper.getContent(response);

if (response.code() != HttpStatus.OK.value()) {
throw PlatformIntegrationHelper.httpError(responseContent, response.code(), gson);
}
if (response.code() != HttpStatus.OK.value()) {
throw PlatformIntegrationHelper.httpError(responseContent, response.code(), gson);
}

GetCustomerResponse getCustomerResponse;
try {
getCustomerResponse = gson.fromJson(responseContent, GetCustomerResponse.class);
} catch (Exception e) { // cannot read body from response
throw new ServerErrorException("internal server error", e);
}
GetCustomerResponse getCustomerResponse;
try {
getCustomerResponse = gson.fromJson(responseContent, GetCustomerResponse.class);
} catch (Exception e) { // cannot read body from response
throw new ServerErrorException("internal server error", e);
}

if (getCustomerResponse.getStatus() == null) {
Log.error("GET {callbackAPI}/customer response is missing the status field");
throw new ServerErrorException(
"internal server error: result from Anchor backend is invalid");
if (getCustomerResponse.getStatus() == null) {
Log.error("GET {callbackAPI}/customer response is missing the status field");
throw new ServerErrorException(
"internal server error: result from Anchor backend is invalid");
}
return getCustomerResponse;
}
return getCustomerResponse;
}

@Override
Expand All @@ -89,18 +91,19 @@ public PutCustomerResponse putCustomer(PutCustomerRequest putCustomerRequest)
Request callbackRequest = createCallbackRequest(putCustomerRequest);

// Call anchor
Response response = PlatformIntegrationHelper.call(httpClient, callbackRequest);
String responseContent = PlatformIntegrationHelper.getContent(response);
try (Response response = PlatformIntegrationHelper.call(httpClient, callbackRequest)) {
String responseContent = PlatformIntegrationHelper.getContent(response);

if (!List.of(HttpStatus.OK.value(), HttpStatus.CREATED.value(), HttpStatus.ACCEPTED.value())
.contains(response.code())) {
throw PlatformIntegrationHelper.httpError(responseContent, response.code(), gson);
}
if (!List.of(HttpStatus.OK.value(), HttpStatus.CREATED.value(), HttpStatus.ACCEPTED.value())
.contains(response.code())) {
throw PlatformIntegrationHelper.httpError(responseContent, response.code(), gson);
}

try {
return gson.fromJson(responseContent, PutCustomerResponse.class);
} catch (Exception e) {
throw new ServerErrorException("internal server error", e);
try {
return gson.fromJson(responseContent, PutCustomerResponse.class);
} catch (Exception e) {
throw new ServerErrorException("internal server error", e);
}
}
}

Expand Down Expand Up @@ -181,11 +184,13 @@ public void deleteCustomer(String id) throws AnchorException {
PlatformIntegrationHelper.getRequestBuilder(authHelper).url(url).delete().build();

// Call anchor
Response response = PlatformIntegrationHelper.call(httpClient, callbackRequest);
String responseContent = PlatformIntegrationHelper.getContent(response);
try (Response response = PlatformIntegrationHelper.call(httpClient, callbackRequest)) {
String responseContent = PlatformIntegrationHelper.getContent(response);

if (!List.of(HttpStatus.OK.value(), HttpStatus.NO_CONTENT.value()).contains(response.code())) {
throw PlatformIntegrationHelper.httpError(responseContent, response.code(), gson);
if (!List.of(HttpStatus.OK.value(), HttpStatus.NO_CONTENT.value())
.contains(response.code())) {
throw PlatformIntegrationHelper.httpError(responseContent, response.code(), gson);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,21 @@ public GetFeeResponse getFee(GetFeeRequest request) throws AnchorException {

Request httpRequest =
PlatformIntegrationHelper.getRequestBuilder(authHelper).url(url).get().build();
Response response = PlatformIntegrationHelper.call(httpClient, httpRequest);
String responseContent = PlatformIntegrationHelper.getContent(response);

if (response.code() != HttpStatus.OK.value()) {
throw PlatformIntegrationHelper.httpError(responseContent, response.code(), gson);
}
try (Response response = PlatformIntegrationHelper.call(httpClient, httpRequest)) {
String responseContent = PlatformIntegrationHelper.getContent(response);

GetFeeResponse feeResponse;
try {
feeResponse = gson.fromJson(responseContent, GetFeeResponse.class);
} catch (Exception e) { // cannot read body from response
throw new ServerErrorException("internal server error", e);
}
if (response.code() != HttpStatus.OK.value()) {
throw PlatformIntegrationHelper.httpError(responseContent, response.code(), gson);
}

return feeResponse;
GetFeeResponse feeResponse;
try {
feeResponse = gson.fromJson(responseContent, GetFeeResponse.class);
} catch (Exception e) { // cannot read body from response
throw new ServerErrorException("internal server error", e);
}
return feeResponse;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,24 @@ public GetRateResponse getRate(GetRateRequest request) throws AnchorException {

Request httpRequest =
PlatformIntegrationHelper.getRequestBuilder(authHelper).url(url).get().build();
Response response = PlatformIntegrationHelper.call(httpClient, httpRequest);
String responseContent = PlatformIntegrationHelper.getContent(response);
try (Response response = PlatformIntegrationHelper.call(httpClient, httpRequest)) {
String responseContent = PlatformIntegrationHelper.getContent(response);

if (response.code() != HttpStatus.OK.value()) {
throw PlatformIntegrationHelper.httpError(responseContent, response.code(), gson);
}
if (response.code() != HttpStatus.OK.value()) {
throw PlatformIntegrationHelper.httpError(responseContent, response.code(), gson);
}

GetRateResponse getRateResponse;
try {
getRateResponse = gson.fromJson(responseContent, GetRateResponse.class);
} catch (Exception e) { // cannot read body from response
errorEx("Error parsing body response to GetRateResponse", e);
throw new ServerErrorException("internal server error", e);
}
GetRateResponse getRateResponse;
try {
getRateResponse = gson.fromJson(responseContent, GetRateResponse.class);
} catch (Exception e) { // cannot read body from response
errorEx("Error parsing body response to GetRateResponse", e);
throw new ServerErrorException("internal server error", e);
}

validateRateResponse(request, getRateResponse);
return getRateResponse;
validateRateResponse(request, getRateResponse);
return getRateResponse;
}
}

void validateRateResponse(GetRateRequest request, GetRateResponse getRateResponse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ public GetUniqueAddressResponse getUniqueAddress(String transactionId) throws An
.build();
Request request =
PlatformIntegrationHelper.getRequestBuilder(authHelper).url(url).get().build();
Response response = PlatformIntegrationHelper.call(httpClient, request);
String content = PlatformIntegrationHelper.getContent(response);
try (Response response = PlatformIntegrationHelper.call(httpClient, request)) {
String content = PlatformIntegrationHelper.getContent(response);

if (!List.of(HttpStatus.OK.value(), HttpStatus.NO_CONTENT.value()).contains(response.code())) {
throw PlatformIntegrationHelper.httpError(content, response.code(), gson);
if (!List.of(HttpStatus.OK.value(), HttpStatus.NO_CONTENT.value())
.contains(response.code())) {
throw PlatformIntegrationHelper.httpError(content, response.code(), gson);
}
return gson.fromJson(content, GetUniqueAddressResponse.class);
}
return gson.fromJson(content, GetUniqueAddressResponse.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ boolean handleEvent(AnchorEvent event) throws IOException {
Request request = buildHttpRequest(signer, event);

if (request != null) {
Response response = httpClient.newCall(request).execute();
debugF("Sending event: {} to client status api: {}", json(event), request.url());
if (response.code() < 200 || response.code() >= 400) {
errorF("Failed to send event to client status API. Error code: {}", response.code());
return false;
try (Response response = httpClient.newCall(request).execute()) {
debugF("Sending event: {} to client status api: {}", json(event), request.url());
if (response.code() < 200 || response.code() >= 400) {
errorF("Failed to send event to client status API. Error code: {}", response.code());
return false;
}
}
}
}
Expand Down
Loading