aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/authentication/ws/ValidateAction.java9
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/component/ws/AppAction.java19
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPayload.java23
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowAction.java7
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java17
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/issue/ws/ComponentTagsAction.java18
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QPMeasureData.java24
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/test/index/TestResultSetIterator.java38
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/ws/WsUtils.java4
-rw-r--r--sonar-core/src/main/java/org/sonar/core/util/ProtobufJsonFormat.java5
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java38
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java9
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/report/JSONReport.java5
13 files changed, 108 insertions, 108 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/authentication/ws/ValidateAction.java b/server/sonar-server/src/main/java/org/sonar/server/authentication/ws/ValidateAction.java
index 1aed8f0ed5b..1d4c2eb6e1d 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/authentication/ws/ValidateAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/authentication/ws/ValidateAction.java
@@ -81,10 +81,11 @@ public class ValidateAction extends ServletFilter implements AuthenticationWsAct
boolean isAuthenticated = authenticate(request, response);
response.setContentType(MediaTypes.JSON);
- JsonWriter jsonWriter = JsonWriter.of(response.getWriter());
- jsonWriter.beginObject();
- jsonWriter.prop("valid", isAuthenticated);
- jsonWriter.endObject();
+ try (JsonWriter jsonWriter = JsonWriter.of(response.getWriter())) {
+ jsonWriter.beginObject();
+ jsonWriter.prop("valid", isAuthenticated);
+ jsonWriter.endObject();
+ }
}
private boolean authenticate(HttpServletRequest request, HttpServletResponse response) {
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/AppAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/AppAction.java
index 844aabc2edd..2aa366f3731 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/AppAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/AppAction.java
@@ -37,7 +37,6 @@ import org.sonar.api.utils.text.JsonWriter;
import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.MyBatis;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.measure.MeasureDto;
import org.sonar.db.measure.MeasureQuery;
@@ -92,13 +91,10 @@ public class AppAction implements RequestHandler {
@Override
public void handle(Request request, Response response) {
- String componentUuid = request.mandatoryParam(PARAM_UUID);
-
- JsonWriter json = response.newJsonWriter();
- json.beginObject();
-
- DbSession session = dbClient.openSession(false);
- try {
+ try (DbSession session = dbClient.openSession(false);
+ JsonWriter json = response.newJsonWriter()) {
+ json.beginObject();
+ String componentUuid = request.mandatoryParam(PARAM_UUID);
ComponentDto component = componentFinder.getByUuid(session, componentUuid);
userSession.checkComponentPermission(UserRole.USER, component);
@@ -106,13 +102,8 @@ public class AppAction implements RequestHandler {
appendComponent(json, component, userSession, session);
appendPermissions(json, component, userSession);
appendMeasures(json, measuresByMetricKey);
-
- } finally {
- MyBatis.closeQuietly(session);
+ json.endObject();
}
-
- json.endObject();
- json.close();
}
private void appendComponent(JsonWriter json, ComponentDto component, UserSession userSession, DbSession session) {
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPayload.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPayload.java
index ffbcbb13270..7ed6b185d41 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPayload.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/webhook/WebhookPayload.java
@@ -56,18 +56,19 @@ public class WebhookPayload {
public static WebhookPayload from(PostProjectAnalysisTask.ProjectAnalysis analysis) {
Writer string = new StringWriter();
- JsonWriter writer = JsonWriter.of(string);
- writer.beginObject();
- writeTask(writer, analysis.getCeTask());
- Optional<Date> analysisDate = analysis.getAnalysisDate();
- if (analysisDate.isPresent()) {
- writer.propDateTime("analysedAt", analysisDate.get());
+ try (JsonWriter writer = JsonWriter.of(string)) {
+ writer.beginObject();
+ writeTask(writer, analysis.getCeTask());
+ Optional<Date> analysisDate = analysis.getAnalysisDate();
+ if (analysisDate.isPresent()) {
+ writer.propDateTime("analysedAt", analysisDate.get());
+ }
+ writeProject(analysis, writer, analysis.getProject());
+ writeQualityGate(writer, analysis.getQualityGate());
+ writeAnalysisProperties(writer, analysis.getScannerContext());
+ writer.endObject().close();
+ return new WebhookPayload(analysis.getProject().getKey(), string.toString());
}
- writeProject(analysis, writer, analysis.getProject());
- writeQualityGate(writer, analysis.getQualityGate());
- writeAnalysisProperties(writer, analysis.getScannerContext());
- writer.endObject().close();
- return new WebhookPayload(analysis.getProject().getKey(), string.toString());
}
private static void writeAnalysisProperties(JsonWriter writer, ScannerContext scannerContext) {
diff --git a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowAction.java
index a1947958968..68aa75f2fbd 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowAction.java
@@ -77,14 +77,15 @@ public class ShowAction implements RequestHandler {
@Override
public void handle(Request request, Response response) {
- try (DbSession dbSession = dbClient.openSession(false)) {
+ try (DbSession dbSession = dbClient.openSession(false);
+ JsonWriter json = response.newJsonWriter()) {
ComponentDto component = componentFinder.getByUuidOrKey(dbSession, request.param("uuid"), request.param("key"), UUID_AND_KEY);
userSession.checkComponentPermission(UserRole.CODEVIEWER, component);
- JsonWriter json = response.newJsonWriter().beginObject();
+ json.beginObject();
String duplications = findDataFromComponent(dbSession, component);
List<DuplicationsParser.Block> blocks = parser.parse(component, duplications, dbSession);
duplicationsJsonWriter.write(blocks, json, dbSession);
- json.endObject().close();
+ json.endObject();
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java
index e932c9dd93f..5870b58e9ca 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java
@@ -43,16 +43,17 @@ public class AuthorsAction implements IssuesWsAction {
String query = request.param(Param.TEXT_QUERY);
int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE);
- JsonWriter json = response.newJsonWriter()
- .beginObject()
- .name("authors")
- .beginArray();
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginObject()
+ .name("authors")
+ .beginArray();
- for (String login : service.listAuthors(query, pageSize)) {
- json.value(login);
- }
+ for (String login : service.listAuthors(query, pageSize)) {
+ json.value(login);
+ }
- json.endArray().endObject().close();
+ json.endArray().endObject();
+ }
}
@Override
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/ComponentTagsAction.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/ComponentTagsAction.java
index 1e6ce038359..9acfa14589b 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/ComponentTagsAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/ComponentTagsAction.java
@@ -83,16 +83,16 @@ public class ComponentTagsAction implements IssuesWsAction {
}
IssueQuery query = queryService.createFromMap(paramBuilder.build());
int pageSize = request.mandatoryParamAsInt(PAGE_SIZE);
- JsonWriter json = response.newJsonWriter().beginObject().name("tags").beginArray();
- for (Map.Entry<String, Long> tag : service.listTagsForComponent(query, pageSize).entrySet()) {
- json.beginObject()
- .prop("key", tag.getKey())
- .prop("value", tag.getValue())
- .endObject();
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginObject().name("tags").beginArray();
+ for (Map.Entry<String, Long> tag : service.listTagsForComponent(query, pageSize).entrySet()) {
+ json.beginObject()
+ .prop("key", tag.getKey())
+ .prop("value", tag.getValue())
+ .endObject();
+ }
+ json.endArray().endObject();
}
- json.endArray()
- .endObject()
- .close();
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QPMeasureData.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QPMeasureData.java
index eaf70841444..b34e04ea12b 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QPMeasureData.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QPMeasureData.java
@@ -62,19 +62,19 @@ public class QPMeasureData {
public static String toJson(QPMeasureData data) {
StringWriter json = new StringWriter();
- JsonWriter writer = JsonWriter.of(json);
- writer.beginArray();
- for (QualityProfile profile : data.getProfiles()) {
- writer
- .beginObject()
- .prop("key", profile.getQpKey())
- .prop("language", profile.getLanguageKey())
- .prop("name", profile.getQpName())
- .prop("rulesUpdatedAt", UtcDateUtils.formatDateTime(profile.getRulesUpdatedAt()))
- .endObject();
+ try (JsonWriter writer = JsonWriter.of(json)) {
+ writer.beginArray();
+ for (QualityProfile profile : data.getProfiles()) {
+ writer
+ .beginObject()
+ .prop("key", profile.getQpKey())
+ .prop("language", profile.getLanguageKey())
+ .prop("name", profile.getQpName())
+ .prop("rulesUpdatedAt", UtcDateUtils.formatDateTime(profile.getRulesUpdatedAt()))
+ .endObject();
+ }
+ writer.endArray();
}
- writer.endArray();
- writer.close();
return json.toString();
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/test/index/TestResultSetIterator.java b/server/sonar-server/src/main/java/org/sonar/server/test/index/TestResultSetIterator.java
index a93dc8c0f46..3a995907b0d 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/test/index/TestResultSetIterator.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/test/index/TestResultSetIterator.java
@@ -105,28 +105,28 @@ public class TestResultSetIterator extends ResultSetIterator<Row> {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
// all the fields must be present, even if value is null
- JsonWriter writer = JsonWriter.of(new OutputStreamWriter(bytes, StandardCharsets.UTF_8)).setSerializeNulls(true);
- writer.beginObject();
- writer.prop(FIELD_PROJECT_UUID, projectUuid);
- writer.prop(FIELD_FILE_UUID, fileUuid);
- writer.prop(FIELD_TEST_UUID, test.getUuid());
- writer.prop(FIELD_NAME, test.getName());
- writer.prop(FIELD_STATUS, test.hasStatus() ? test.getStatus().toString() : null);
- writer.prop(FIELD_DURATION_IN_MS, test.hasExecutionTimeMs() ? test.getExecutionTimeMs() : null);
- writer.prop(FIELD_MESSAGE, test.hasMsg() ? test.getMsg() : null);
- writer.prop(FIELD_STACKTRACE, test.hasStacktrace() ? test.getStacktrace() : null);
- writer.prop(FIELD_UPDATED_AT, EsUtils.formatDateTime(updatedAt));
- writer.name(FIELD_COVERED_FILES);
- writer.beginArray();
- for (DbFileSources.Test.CoveredFile coveredFile : test.getCoveredFileList()) {
+ try (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(bytes, StandardCharsets.UTF_8)).setSerializeNulls(true)) {
writer.beginObject();
- writer.prop(FIELD_COVERED_FILE_UUID, coveredFile.getFileUuid());
- writer.name(FIELD_COVERED_FILE_LINES).valueObject(coveredFile.getCoveredLineList());
+ writer.prop(FIELD_PROJECT_UUID, projectUuid);
+ writer.prop(FIELD_FILE_UUID, fileUuid);
+ writer.prop(FIELD_TEST_UUID, test.getUuid());
+ writer.prop(FIELD_NAME, test.getName());
+ writer.prop(FIELD_STATUS, test.hasStatus() ? test.getStatus().toString() : null);
+ writer.prop(FIELD_DURATION_IN_MS, test.hasExecutionTimeMs() ? test.getExecutionTimeMs() : null);
+ writer.prop(FIELD_MESSAGE, test.hasMsg() ? test.getMsg() : null);
+ writer.prop(FIELD_STACKTRACE, test.hasStacktrace() ? test.getStacktrace() : null);
+ writer.prop(FIELD_UPDATED_AT, EsUtils.formatDateTime(updatedAt));
+ writer.name(FIELD_COVERED_FILES);
+ writer.beginArray();
+ for (DbFileSources.Test.CoveredFile coveredFile : test.getCoveredFileList()) {
+ writer.beginObject();
+ writer.prop(FIELD_COVERED_FILE_UUID, coveredFile.getFileUuid());
+ writer.name(FIELD_COVERED_FILE_LINES).valueObject(coveredFile.getCoveredLineList());
+ writer.endObject();
+ }
+ writer.endArray();
writer.endObject();
}
- writer.endArray();
- writer.endObject().close();
-
// This is an optimization to reduce memory consumption and multiple conversions from Map to JSON.
// UpdateRequest#doc() and #upsert() take the same parameter values, so:
// - passing the same Map would execute two JSON serializations
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/WsUtils.java b/server/sonar-server/src/main/java/org/sonar/server/ws/WsUtils.java
index 91830e2f17f..debfe0a85a8 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/ws/WsUtils.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/ws/WsUtils.java
@@ -51,8 +51,8 @@ public class WsUtils {
msg.writeTo(output);
} else {
response.stream().setMediaType(MediaTypes.JSON);
- try (OutputStreamWriter writer = new OutputStreamWriter(output, StandardCharsets.UTF_8)) {
- ProtobufJsonFormat.write(msg, JsonWriter.of(writer));
+ try (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(output, StandardCharsets.UTF_8))) {
+ ProtobufJsonFormat.write(msg, writer);
}
}
} catch (Exception e) {
diff --git a/sonar-core/src/main/java/org/sonar/core/util/ProtobufJsonFormat.java b/sonar-core/src/main/java/org/sonar/core/util/ProtobufJsonFormat.java
index f681116279f..546c6810c09 100644
--- a/sonar-core/src/main/java/org/sonar/core/util/ProtobufJsonFormat.java
+++ b/sonar-core/src/main/java/org/sonar/core/util/ProtobufJsonFormat.java
@@ -161,8 +161,9 @@ public class ProtobufJsonFormat {
public static String toJson(Message message) {
StringWriter json = new StringWriter();
- JsonWriter jsonWriter = JsonWriter.of(json);
- write(message, jsonWriter);
+ try (JsonWriter jsonWriter = JsonWriter.of(json)) {
+ write(message, jsonWriter);
+ }
return json.toString();
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java
index a098a2388a4..98a84dbc8b4 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java
@@ -32,30 +32,34 @@ import org.sonar.api.utils.DateUtils;
* <p>
* <h3>How to use</h3>
* <pre>
- * StringWriter json = new StringWriter();
- * JsonWriter writer = JsonWriter.of(json);
- * writer
- * .beginObject()
- * .prop("aBoolean", true)
- * .prop("aInt", 123)
- * .prop("aString", "foo")
- * .beginObject().name("aList")
- * .beginArray()
- * .beginObject().prop("key", "ABC").endObject()
- * .beginObject().prop("key", "DEF").endObject()
- * .endArray()
- * .endObject()
- * .close();
+ * try (JsonWriter jsonWriter = JsonWriter.of(writer)) {
+ * jsonWriter
+ * .beginObject()
+ * .prop("aBoolean", true)
+ * .prop("aInt", 123)
+ * .prop("aString", "foo")
+ * .beginObject().name("aList")
+ * .beginArray()
+ * .beginObject().prop("key", "ABC").endObject()
+ * .beginObject().prop("key", "DEF").endObject()
+ * .endArray()
+ * .endObject()
+ * }
* </pre>
*
* <p>
* By default, null objects are not serialized. To enable {@code null} serialization,
* use {@link #setSerializeNulls(boolean)}.
- *
+ * </p>
* <p>
- * By default, emptry strings are serialized. To disable empty string serialization,
+ * By default, empty strings are serialized. To disable empty string serialization,
* use {@link #setSerializeEmptys(boolean)}.
- *
+ * </p>
+ * <p>
+ * {@link JsonWriter} implements {@link AutoCloseable} since version 6.3. The
+ * method {@link #close()} closes the underlying writer.
+ * </p>
+ *
*
* @since 4.2
*/
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java
index 0ed59a44c00..97ce288e814 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java
@@ -43,8 +43,8 @@ public class JsonWriterTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
- StringWriter json = new StringWriter();
- JsonWriter writer = JsonWriter.of(json);
+ private StringWriter json = new StringWriter();
+ private JsonWriter writer = JsonWriter.of(json);
private void expect(String s) {
assertThat(json.toString()).isEqualTo(s);
@@ -170,7 +170,8 @@ public class JsonWriterTest {
.name("anEnum").valueObject(ColorEnum.GREEN)
.name("aMap").valueObject(ImmutableMap.of("hello", "world", "good", "bye"))
.endObject().close();
- expect("{\"aString\":\"stringValue\",\"aBoolean\":true,\"aInt\":42,\"aFloat\":3.14,\"aLong\":42,\"aList\":[\"one\",2,\"three\"],\"anEnum\":\"GREEN\",\"aMap\":{\"hello\":\"world\",\"good\":\"bye\"}}");
+ expect(
+ "{\"aString\":\"stringValue\",\"aBoolean\":true,\"aInt\":42,\"aFloat\":3.14,\"aLong\":42,\"aList\":[\"one\",2,\"three\"],\"anEnum\":\"GREEN\",\"aMap\":{\"hello\":\"world\",\"good\":\"bye\"}}");
}
@Test
@@ -212,7 +213,7 @@ public class JsonWriterTest {
new JsonWriter(gson).beginArray();
}
- private static enum ColorEnum {
+ private enum ColorEnum {
RED, GREEN
}
}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/report/JSONReport.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/report/JSONReport.java
index 68c2c20f9b2..fdaaefa2e19 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/report/JSONReport.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/report/JSONReport.java
@@ -109,8 +109,7 @@ public class JSONReport implements Reporter {
@VisibleForTesting
void writeJson(Writer writer) {
- try {
- JsonWriter json = JsonWriter.of(writer);
+ try (JsonWriter json = JsonWriter.of(writer)) {
json.beginObject();
json.prop("version", server.getVersion());
@@ -120,7 +119,7 @@ public class JSONReport implements Reporter {
writeJsonComponents(json);
writeJsonRules(json, ruleKeys);
writeUsers(json, userLogins);
- json.endObject().close();
+ json.endObject();
} catch (IOException e) {
throw new IllegalStateException("Unable to write JSON report", e);