Search in sources :

Example 21 with JsonWriter

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();
}
Also used : JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 22 with JsonWriter

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();
    }
}
Also used : DbSession(org.sonar.db.DbSession) ComponentDto(org.sonar.db.component.ComponentDto) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 23 with JsonWriter

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;
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) OutputStreamWriter(java.io.OutputStreamWriter) Row(org.sonar.server.source.index.FileSourcesUpdaterHelper.Row) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DbFileSources(org.sonar.db.protobuf.DbFileSources) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 24 with JsonWriter

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();
    }
}
Also used : DbSession(org.sonar.db.DbSession) ComponentDto(org.sonar.db.component.ComponentDto) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 25 with JsonWriter

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();
    }
}
Also used : DbSession(org.sonar.db.DbSession) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Aggregations

JsonWriter (org.sonar.api.utils.text.JsonWriter)69 DbSession (org.sonar.db.DbSession)24 ComponentDto (org.sonar.db.component.ComponentDto)12 UserDto (org.sonar.db.user.UserDto)6 MetricDto (org.sonar.db.metric.MetricDto)5 OrganizationDto (org.sonar.db.organization.OrganizationDto)5 SearchOptions (org.sonar.server.es.SearchOptions)5 StringWriter (java.io.StringWriter)4 QualityGateDto (org.sonar.db.qualitygate.QualityGateDto)4 OutputStreamWriter (java.io.OutputStreamWriter)3 Language (org.sonar.api.resources.Language)3 Paging (org.sonar.api.utils.Paging)2 PluginInfo (org.sonar.core.platform.PluginInfo)2 CustomMeasureDto (org.sonar.db.measure.custom.CustomMeasureDto)2 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)2 BadRequestException (org.sonar.server.exceptions.BadRequestException)2 NotFoundException (org.sonar.server.exceptions.NotFoundException)2 DatabaseVersion (org.sonar.server.platform.db.migration.version.DatabaseVersion)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1