Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
Make sure we close body before throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpherrinm committed Jan 19, 2017
1 parent 98d538b commit 39c2dd9
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions client/src/main/java/keywhiz/client/KeywhizClient.java
Expand Up @@ -278,8 +278,7 @@ public boolean isLoggedIn() throws IOException{
/** /**
* Maps some of the common HTTP errors to the corresponding exceptions. * Maps some of the common HTTP errors to the corresponding exceptions.
*/ */
private void throwOnCommonError(Response response) throws IOException { private void throwOnCommonError(int status) throws IOException {
int status = response.code();
switch (status) { switch (status) {
case HttpStatus.SC_BAD_REQUEST: case HttpStatus.SC_BAD_REQUEST:
throw new MalformedRequestException(); throw new MalformedRequestException();
Expand All @@ -301,15 +300,24 @@ private void throwOnCommonError(Response response) throws IOException {
} }
} }


private String makeCall(Request request) throws IOException {
Response response = client.newCall(request).execute();
try {
throwOnCommonError(response.code());
} catch (IOException e) {
response.body().close();
throw e;
}
return response.body().string();
}

private String httpGet(HttpUrl url) throws IOException { private String httpGet(HttpUrl url) throws IOException {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.get() .get()
.build(); .build();


Response response = client.newCall(request).execute(); return makeCall(request);
throwOnCommonError(response);
return response.body().string();
} }


private String httpPost(HttpUrl url, Object content) throws IOException { private String httpPost(HttpUrl url, Object content) throws IOException {
Expand All @@ -320,9 +328,7 @@ private String httpPost(HttpUrl url, Object content) throws IOException {
.addHeader(HttpHeaders.CONTENT_TYPE, JSON.toString()) .addHeader(HttpHeaders.CONTENT_TYPE, JSON.toString())
.build(); .build();


Response response = client.newCall(request).execute(); return makeCall(request);
throwOnCommonError(response);
return response.body().string();
} }


private String httpPut(HttpUrl url) throws IOException { private String httpPut(HttpUrl url) throws IOException {
Expand All @@ -331,9 +337,7 @@ private String httpPut(HttpUrl url) throws IOException {
.put(RequestBody.create(MediaType.parse("text/plain"), "")) .put(RequestBody.create(MediaType.parse("text/plain"), ""))
.build(); .build();


Response response = client.newCall(request).execute(); return makeCall(request);
throwOnCommonError(response);
return response.body().string();
} }


private String httpDelete(HttpUrl url) throws IOException { private String httpDelete(HttpUrl url) throws IOException {
Expand All @@ -342,8 +346,6 @@ private String httpDelete(HttpUrl url) throws IOException {
.delete() .delete()
.build(); .build();


Response response = client.newCall(request).execute(); return makeCall(request);
throwOnCommonError(response);
return response.body().string();
} }
} }

0 comments on commit 39c2dd9

Please sign in to comment.