Skip to content

Commit 3fbf4cb

Browse files
committed
client-hc: make sure connection is closed also for exception
1 parent 1835622 commit 3fbf4cb

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

client-hc/src/main/java/com/graphhopper/api/GHMatrixAbstractRequester.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,24 @@ public OkHttpClient getDownloader() {
6161

6262
protected String getJson(String url) throws IOException {
6363
Request okRequest = new Request.Builder().url(url).build();
64-
ResponseBody body = downloader.newCall(okRequest).execute().body();
65-
String str = body.string();
66-
body.close();
67-
return str;
64+
ResponseBody body = null;
65+
try {
66+
body = downloader.newCall(okRequest).execute().body();
67+
return body.string();
68+
} finally {
69+
Helper.close(body);
70+
}
6871
}
6972

7073
protected String postJson(String url, JsonNode data) throws IOException {
7174
Request okRequest = new Request.Builder().url(url).post(RequestBody.create(MT_JSON, data.toString())).build();
72-
ResponseBody body = downloader.newCall(okRequest).execute().body();
73-
String str = body.string();
74-
body.close();
75-
return str;
75+
ResponseBody body = null;
76+
try {
77+
body = downloader.newCall(okRequest).execute().body();
78+
return body.string();
79+
} finally {
80+
Helper.close(body);
81+
}
7682
}
7783

7884
protected JsonNode toJSON(String url, String str) {

client-hc/src/main/java/com/graphhopper/api/GraphHopperWeb.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@ public GraphHopperWeb setOptimize(String optimize) {
351351

352352
@Override
353353
public GHResponse route(GHRequest request) {
354+
ResponseBody rspBody = null;
354355
try {
355356
Request okRequest = createRequest(request);
356-
ResponseBody rspBody = getClientForRequest(request).newCall(okRequest).execute().body();
357+
rspBody = getClientForRequest(request).newCall(okRequest).execute().body();
357358
JsonNode json = objectMapper.reader().readTree(rspBody.byteStream());
358-
rspBody.close();
359359

360360
GHResponse res = new GHResponse();
361361
res.addErrors(readErrors(json));
@@ -376,6 +376,8 @@ public GHResponse route(GHRequest request) {
376376

377377
} catch (Exception ex) {
378378
throw new RuntimeException("Problem while fetching path " + request.getPoints() + ": " + ex.getMessage(), ex);
379+
} finally {
380+
Helper.close(rspBody);
379381
}
380382
}
381383

0 commit comments

Comments
 (0)