use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.
the class CurrentAction method writeResponse.
private void writeResponse(Response response, Optional<UserDto> user, Collection<String> groups) {
JsonWriter json = response.newJsonWriter().beginObject();
writeUserDetails(json, user, groups);
json.endObject().close();
}
use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.
the class IndexAction method handle.
@Override
public void handle(Request request, Response response) {
String fileKey = request.mandatoryParam("resource");
int from = request.mandatoryParamAsInt("from");
Integer to = request.paramAsInt("to");
try (DbSession session = dbClient.openSession(false)) {
ComponentDto component = componentFinder.getByKey(session, fileKey);
userSession.checkComponentPermission(UserRole.CODEVIEWER, component);
Optional<Iterable<String>> lines = sourceService.getLinesAsRawText(session, component.uuid(), from, to == null ? Integer.MAX_VALUE : to - 1);
JsonWriter json = response.newJsonWriter().beginArray().beginObject();
if (lines.isPresent()) {
int lineCounter = from;
for (String line : lines.get()) {
json.prop(String.valueOf(lineCounter), line);
lineCounter++;
}
}
json.endObject().endArray().close();
}
}
use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.
the class TestResultSetIterator method toRow.
/**
* Convert protobuf message to tests required for Elasticsearch indexing
*/
public static Row toRow(String projectUuid, String fileUuid, Date updatedAt, List<DbFileSources.Test> tests) {
Row result = new Row(projectUuid, fileUuid, updatedAt.getTime());
for (DbFileSources.Test test : tests) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
// all the fields must be present, even if value is null
try (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()) {
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();
}
// 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
// - Map is a useless temporarily structure: read JDBC result set -> convert to map -> convert to JSON. Generating
// directly JSON from result set is more efficient.
byte[] jsonDoc = bytes.toByteArray();
UpdateRequest updateRequest = new UpdateRequest(INDEX_TYPE_TEST.getIndex(), INDEX_TYPE_TEST.getType(), test.getUuid()).routing(projectUuid).doc(jsonDoc).upsert(jsonDoc);
result.getUpdateRequests().add(updateRequest);
}
return result;
}
use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.
the class ShowAction method handle.
@Override
public void handle(Request request, Response response) {
String fileKey = request.mandatoryParam("key");
int from = Math.max(request.paramAsInt("from"), 1);
int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
try (DbSession dbSession = dbClient.openSession(false)) {
ComponentDto file = componentFinder.getByKey(dbSession, fileKey);
userSession.checkComponentPermission(UserRole.CODEVIEWER, file);
Iterable<String> linesHtml = checkFoundWithOptional(sourceService.getLinesAsHtml(dbSession, file.uuid(), from, to), "No source found for file '%s'", fileKey);
JsonWriter json = response.newJsonWriter().beginObject();
writeSource(linesHtml, from, json);
json.endObject().close();
}
}
use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.
the class AppAction method handle.
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
JsonWriter json = response.newJsonWriter();
json.beginObject();
addPermissions(json);
addProfiles(json, dbSession);
addLanguages(json);
addRuleRepositories(json, dbSession);
addStatuses(json);
json.endObject().close();
}
}
Aggregations