From 289be70bf6056b5f3c66a797ecab019891e731d6 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 12 Jul 2017 09:31:54 +0200 Subject: [PATCH] Fix Quality flaws --- .../org/sonar/server/project/ws/GhostsAction.java | 10 ++++++---- .../qualityprofile/ws/BulkChangeWsResponse.java | 12 +++++++----- .../sonar/server/usergroups/ws/UsersAction.java | 10 ++++++---- .../sonar/server/ws/ws/ResponseExampleAction.java | 14 +++++++------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/project/ws/GhostsAction.java b/server/sonar-server/src/main/java/org/sonar/server/project/ws/GhostsAction.java index 35a3ded8af7..4605d58e45d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/project/ws/GhostsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/project/ws/GhostsAction.java @@ -100,10 +100,12 @@ public class GhostsAction implements ProjectsWsAction { long nbOfProjects = dbClient.componentDao().countGhostProjects(dbSession, organization.getUuid(), query); List projects = dbClient.componentDao().selectGhostProjects(dbSession, organization.getUuid(), query, searchOptions.getOffset(), searchOptions.getLimit()); - JsonWriter json = response.newJsonWriter().beginObject(); - writeProjects(json, projects, desiredFields); - searchOptions.writeJson(json, nbOfProjects); - json.endObject().close(); + try (JsonWriter json = response.newJsonWriter()) { + json.beginObject(); + writeProjects(json, projects, desiredFields); + searchOptions.writeJson(json, nbOfProjects); + json.endObject(); + } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkChangeWsResponse.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkChangeWsResponse.java index 066caaed104..69d6a019661 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkChangeWsResponse.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkChangeWsResponse.java @@ -31,10 +31,12 @@ class BulkChangeWsResponse { } static void writeResponse(BulkChangeResult result, Response response) { - JsonWriter json = response.newJsonWriter().beginObject(); - json.prop("succeeded", result.countSucceeded()); - json.prop("failed", result.countFailed()); - WebServiceEngine.writeErrors(json, result.getErrors()); - json.endObject().close(); + try (JsonWriter json = response.newJsonWriter()) { + json.beginObject(); + json.prop("succeeded", result.countSucceeded()); + json.prop("failed", result.countFailed()); + WebServiceEngine.writeErrors(json, result.getErrors()); + json.endObject(); + } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/UsersAction.java b/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/UsersAction.java index c7dc2ffba74..4cc04bd8771 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/UsersAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/UsersAction.java @@ -92,10 +92,12 @@ public class UsersAction implements UserGroupsWsAction { Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(total); List users = dbClient.groupMembershipDao().selectMembers(dbSession, query, paging.offset(), paging.pageSize()); - JsonWriter json = response.newJsonWriter().beginObject(); - writeMembers(json, users); - writePaging(json, paging); - json.endObject().close(); + try (JsonWriter json = response.newJsonWriter()) { + json.beginObject(); + writeMembers(json, users); + writePaging(json, paging); + json.endObject(); + } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/ws/ResponseExampleAction.java b/server/sonar-server/src/main/java/org/sonar/server/ws/ws/ResponseExampleAction.java index 416eb31d8c1..8e461c7f853 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ws/ws/ResponseExampleAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ws/ws/ResponseExampleAction.java @@ -22,6 +22,7 @@ package org.sonar.server.ws.ws; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; +import org.sonar.api.utils.text.JsonWriter; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; @@ -66,13 +67,12 @@ public class ResponseExampleAction implements WebServicesWsAction { return; } - response - .newJsonWriter() - .beginObject() - .prop("format", action.responseExampleFormat()) - .prop("example", action.responseExampleAsString()) - .endObject() - .close(); + try (JsonWriter json = response.newJsonWriter()) { + json.beginObject() + .prop("format", action.responseExampleFormat()) + .prop("example", action.responseExampleAsString()) + .endObject(); + } } @Override -- 2.39.5