diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-07-13 11:32:23 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-07-13 14:12:14 +0200 |
commit | dfa96c89fc0949b7c9c3784078c96ecabf56f1dd (patch) | |
tree | bee9a8b03714866db17c18d9cf133a4c8e45c605 /sonar-scanner-engine | |
parent | ec692c4d95dd423740867418227d2cde54c40d79 (diff) | |
download | sonarqube-dfa96c89fc0949b7c9c3784078c96ecabf56f1dd.tar.gz sonarqube-dfa96c89fc0949b7c9c3784078c96ecabf56f1dd.zip |
Fix leaked connection
Diffstat (limited to 'sonar-scanner-engine')
3 files changed, 9 insertions, 5 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 a9066499967..ea47cececbd 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 @@ -56,6 +56,8 @@ public class BatchWsClient { } /** + * If an exception is not thrown, the response needs to be closed by either calling close() directly, or closing the + * body content's stream/reader. * @throws IllegalStateException if the request could not be executed due to * a connectivity problem or timeout. Because networks can * fail during an exchange, it is possible that the remote server @@ -82,6 +84,7 @@ public class BatchWsClient { private void failIfUnauthorized(WsResponse response) { int code = response.code(); if (code == HTTP_UNAUTHORIZED) { + response.close(); if (hasCredentials) { // credentials are not valid throw MessageException.of(format("Not authorized. Please check the properties %s and %s.", @@ -94,6 +97,7 @@ 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-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultGlobalRepositoriesLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultGlobalRepositoriesLoader.java index d7140372d45..84a44c49d12 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultGlobalRepositoriesLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultGlobalRepositoriesLoader.java @@ -39,9 +39,8 @@ public class DefaultGlobalRepositoriesLoader implements GlobalRepositoriesLoader @Override public GlobalRepositories load() { GetRequest getRequest = new GetRequest(BATCH_GLOBAL_URL); - Reader reader = wsClient.call(getRequest).contentReader(); String str; - try { + try (Reader reader = wsClient.call(getRequest).contentReader()) { str = IOUtils.toString(reader); } catch (IOException e) { throw new IllegalStateException(e); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java index 5a9283dbac1..cea4a21ba17 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java @@ -41,6 +41,7 @@ import org.sonarqube.ws.WsBatch.WsProjectResponse.FileDataByPath; import org.sonarqube.ws.WsBatch.WsProjectResponse.Settings; import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.HttpException; +import org.sonarqube.ws.client.WsResponse; public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoader { private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectRepositoriesLoader.class); @@ -53,9 +54,9 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad @Override public ProjectRepositories load(String projectKey, boolean issuesMode) { - try { - GetRequest request = new GetRequest(getUrl(projectKey, issuesMode)); - InputStream is = wsClient.call(request).contentStream(); + GetRequest request = new GetRequest(getUrl(projectKey, issuesMode)); + try (WsResponse response = wsClient.call(request)) { + InputStream is = response.contentStream(); return processStream(is, projectKey); } catch (RuntimeException e) { if (shouldThrow(e)) { |