From 10c0d319d02d984c2a075311006434ff3c8251f5 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Wed, 13 Jul 2016 17:01:55 +0200 Subject: [PATCH] Fix close connection --- .../org/sonar/scanner/bootstrap/BatchWsClient.java | 1 - .../java/org/sonarqube/ws/client/OkHttpResponse.java | 10 +++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/BatchWsClient.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/BatchWsClient.java index ea47cececbd..98269c46a67 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/BatchWsClient.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/BatchWsClient.java @@ -97,7 +97,6 @@ public class BatchWsClient { } if (code == HTTP_FORBIDDEN || code == HTTP_BAD_REQUEST) { // SONAR-4397 Details are in response content - response.close(); throw MessageException.of(tryParseAsJsonError(response.content())); } response.failIfNotSuccessful(); diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpResponse.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpResponse.java index ce412f69de8..0532e9f70f8 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpResponse.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/OkHttpResponse.java @@ -20,6 +20,8 @@ package org.sonarqube.ws.client; import okhttp3.Response; +import okhttp3.ResponseBody; + import java.io.IOException; import java.io.InputStream; import java.io.Reader; @@ -65,12 +67,18 @@ class OkHttpResponse extends BaseResponse { return okResponse.body().charStream(); } + /** + * Get body content as a String. This response will be automatically closed. + */ @Override public String content() { + ResponseBody body = okResponse.body(); try { - return okResponse.body().string(); + return body.string(); } catch (IOException e) { throw fail(e); + } finally { + body.close(); } } -- 2.39.5