From af6b677898a4ac4f0ae65b2ac672a0115c05f765 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 29 Dec 2017 16:08:15 +0100 Subject: [PATCH] SONAR-10154 Use Response.newJsonWriter() to write in WS api/system/info --- .../sonar/server/platform/ws/InfoAction.java | 22 ++++++++----------- .../server/platform/ws/InfoActionTest.java | 4 ++-- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java index c5fe8cf4a7d..c219b4d9fa3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java @@ -19,17 +19,13 @@ */ package org.sonar.server.platform.ws; -import java.io.StringWriter; -import java.nio.charset.StandardCharsets; -import org.apache.commons.io.IOUtils; +import org.sonar.api.server.ws.Change; 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 org.sonar.server.user.UserSession; -import static org.sonarqube.ws.MediaTypes.JSON; - /** * Implementation of the {@code info} action for the System WebService. */ @@ -44,27 +40,27 @@ public class InfoAction implements SystemWsAction { @Override public void define(WebService.NewController controller) { - controller.createAction("info") + WebService.NewAction action = controller.createAction("info") .setDescription("Get detailed information about system configuration.
" + - "Requires 'Administer' permissions.
" + - "Since 5.5, this web service becomes internal in order to more easily update result.") + "Requires 'Administer' permissions.") .setSince("5.1") .setInternal(true) - .setResponseExample(getClass().getResource("/org/sonar/server/platform/ws/info-example.json")) + .setResponseExample(getClass().getResource("info-example.json")) .setHandler(this); + action.setChangelog( + new Change("5.5", "Becomes internal to easily update result") + ); } @Override public void handle(Request request, Response response) throws Exception { userSession.checkIsSystemAdministrator(); - StringWriter stringWriter = new StringWriter(); - JsonWriter json = JsonWriter.of(stringWriter); + JsonWriter json = response.newJsonWriter(); json.beginObject(); systemInfoWriter.write(json); json.endObject(); - response.stream().setMediaType(JSON); - IOUtils.write(stringWriter.toString(), response.stream().output(), StandardCharsets.UTF_8); + json.close(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java index 9d3b0c4d00d..99ed6b79f95 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/InfoActionTest.java @@ -40,7 +40,7 @@ public class InfoActionTest { private SystemInfoWriter jsonWriter = new SystemInfoWriter(null) { @Override - public void write(JsonWriter json) throws Exception { + public void write(JsonWriter json) { json.prop("key", "value"); } }; @@ -48,7 +48,7 @@ public class InfoActionTest { private WsActionTester ws = new WsActionTester(underTest); @Test - public void test_definition() throws Exception { + public void test_definition() { assertThat(ws.getDef().key()).isEqualTo("info"); assertThat(ws.getDef().isInternal()).isTrue(); assertThat(ws.getDef().responseExampleAsString()).isNotEmpty(); -- 2.39.5