Skip to content

Commit

Permalink
close body always
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsocha2 committed Sep 26, 2023
1 parent 0c79d17 commit 359f8d6
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions src/main/java/com/box/sdk/BoxAPIResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,40 +147,42 @@ static BoxAPIResponse toBoxResponse(Response response) {
response.headers().toMultimap()
);
}
ResponseBody responseBody = response.body();
if (responseBody.contentLength() == 0 || responseBody.contentType() == null) {
return new BoxAPIResponse(response.code(),
response.request().method(),
response.request().url().toString(),
response.headers().toMultimap()
);
}
if (responseBody != null && responseBody.contentType() != null) {
if (responseBody.contentType().toString().contains(APPLICATION_JSON)) {
String bodyAsString = "";
try {
bodyAsString = responseBody.string();
return new BoxJSONResponse(response.code(),
try (ResponseBody responseBody = response.body()) {
if (responseBody.contentLength() == 0 || responseBody.contentType() == null) {
return new BoxAPIResponse(response.code(),
response.request().method(),
response.request().url().toString(),
response.headers().toMultimap(),
Json.parse(bodyAsString).asObject()
);
} catch (ParseException e) {
throw new BoxAPIException(format("Error parsing JSON:\n%s", bodyAsString), e);
} catch (IOException e) {
throw new RuntimeException("Error getting response to string", e);
response.headers().toMultimap()
);
}
if (responseBody != null && responseBody.contentType() != null) {
if (responseBody.contentType().toString().contains(APPLICATION_JSON)) {
String bodyAsString = "";
try {
bodyAsString = responseBody.string();
return new BoxJSONResponse(response.code(),
response.request().method(),
response.request().url().toString(),
response.headers().toMultimap(),
Json.parse(bodyAsString).asObject()
);
} catch (ParseException e) {
throw new BoxAPIException(format("Error parsing JSON:\n%s", bodyAsString), e);
} catch (IOException e) {
throw new RuntimeException("Error getting response to string", e);
}
}
}

return new BoxAPIResponse(response.code(),
response.request().method(),
response.request().url().toString(),
response.headers().toMultimap(),
responseBody.byteStream(),
Optional.ofNullable(responseBody.contentType()).map(MediaType::toString).orElse(null),
responseBody.contentLength()
);
}
return new BoxAPIResponse(response.code(),
response.request().method(),
response.request().url().toString(),
response.headers().toMultimap(),
responseBody.byteStream(),
Optional.ofNullable(responseBody.contentType()).map(MediaType::toString).orElse(null),
responseBody.contentLength()
);
}

/**
Expand Down

0 comments on commit 359f8d6

Please sign in to comment.