Search in sources :

Example 31 with JsonWriter

use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.

the class UsersAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE);
    int page = request.mandatoryParamAsInt(Param.PAGE);
    String queryString = request.param(Param.TEXT_QUERY);
    String selected = request.mandatoryParam(Param.SELECTED);
    try (DbSession dbSession = dbClient.openSession(false)) {
        GroupId group = support.findGroup(dbSession, request);
        userSession.checkPermission(OrganizationPermission.ADMINISTER, group.getOrganizationUuid());
        UserMembershipQuery query = UserMembershipQuery.builder().groupId(group.getId()).memberSearch(queryString).membership(getMembership(selected)).pageIndex(page).pageSize(pageSize).build();
        int total = dbClient.groupMembershipDao().countMembers(dbSession, query);
        Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(total);
        List<UserMembershipDto> users = dbClient.groupMembershipDao().selectMembers(dbSession, query, paging.offset(), paging.pageSize());
        JsonWriter json = response.newJsonWriter().beginObject();
        writeMembers(json, users);
        writePaging(json, paging);
        json.endObject().close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) Paging(org.sonar.api.utils.Paging) UserMembershipQuery(org.sonar.db.user.UserMembershipQuery) UserMembershipDto(org.sonar.db.user.UserMembershipDto) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 32 with JsonWriter

use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.

the class GroupsAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn().checkIsSystemAdministrator();
    String login = request.mandatoryParam(PARAM_LOGIN);
    int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE);
    int page = request.mandatoryParamAsInt(Param.PAGE);
    String queryString = request.param(Param.TEXT_QUERY);
    String selected = request.mandatoryParam(Param.SELECTED);
    GroupMembershipQuery query = GroupMembershipQuery.builder().login(login).groupSearch(queryString).membership(getMembership(selected)).pageIndex(page).pageSize(pageSize).build();
    try (DbSession dbSession = dbClient.openSession(false)) {
        UserDto user = dbClient.userDao().selectByLogin(dbSession, login);
        if (user == null) {
            throw new NotFoundException(String.format("User with login '%s' has not been found", login));
        }
        int total = dbClient.groupMembershipDao().countGroups(dbSession, query, user.getId());
        Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(total);
        List<GroupMembershipDto> groups = dbClient.groupMembershipDao().selectGroups(dbSession, query, user.getId(), paging.offset(), pageSize);
        JsonWriter json = response.newJsonWriter().beginObject();
        writeGroups(json, groups);
        writePaging(json, paging);
        json.endObject().close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) GroupMembershipQuery(org.sonar.db.user.GroupMembershipQuery) GroupMembershipDto(org.sonar.db.user.GroupMembershipDto) UserDto(org.sonar.db.user.UserDto) Paging(org.sonar.api.utils.Paging) NotFoundException(org.sonar.server.exceptions.NotFoundException) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 33 with JsonWriter

use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.

the class SearchAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    SearchOptions options = new SearchOptions().setPage(request.mandatoryParamAsInt(Param.PAGE), request.mandatoryParamAsInt(Param.PAGE_SIZE));
    List<String> fields = request.paramAsStrings(Param.FIELDS);
    SearchResult<UserDoc> result = userIndex.search(request.param(Param.TEXT_QUERY), options);
    try (DbSession dbSession = dbClient.openSession(false)) {
        List<String> logins = Lists.transform(result.getDocs(), UserDocToLogin.INSTANCE);
        Multimap<String, String> groupsByLogin = dbClient.groupMembershipDao().selectGroupsByLogins(dbSession, logins);
        Map<String, Integer> tokenCountsByLogin = dbClient.userTokenDao().countTokensByLogins(dbSession, logins);
        JsonWriter json = response.newJsonWriter().beginObject();
        options.writeJson(json, result.getTotal());
        List<UserDto> userDtos = dbClient.userDao().selectByOrderedLogins(dbSession, logins);
        writeUsers(json, userDtos, groupsByLogin, tokenCountsByLogin, fields);
        json.endObject().close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) UserDto(org.sonar.db.user.UserDto) SearchOptions(org.sonar.server.es.SearchOptions) JsonWriter(org.sonar.api.utils.text.JsonWriter) UserDoc(org.sonar.server.user.index.UserDoc)

Example 34 with JsonWriter

use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.

the class WsUtils method writeProtobuf.

public static void writeProtobuf(Message msg, Request request, Response response) {
    OutputStream output = response.stream().output();
    try {
        if (request.getMediaType().equals(MediaTypes.PROTOBUF)) {
            response.stream().setMediaType(MediaTypes.PROTOBUF);
            msg.writeTo(output);
        } else {
            response.stream().setMediaType(MediaTypes.JSON);
            try (JsonWriter writer = JsonWriter.of(new OutputStreamWriter(output, StandardCharsets.UTF_8))) {
                ProtobufJsonFormat.write(msg, writer);
            }
        }
    } catch (Exception e) {
        throw new IllegalStateException(format("Error while writing protobuf message %s", MessageFormatter.print(msg)), e);
    } finally {
        IOUtils.closeQuietly(output);
    }
}
Also used : OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(org.sonar.api.utils.text.JsonWriter) BadRequestException(org.sonar.server.exceptions.BadRequestException) NotFoundException(org.sonar.server.exceptions.NotFoundException)

Example 35 with JsonWriter

use of org.sonar.api.utils.text.JsonWriter in project sonarqube by SonarSource.

the class DuplicationsJsonWriterTest method test.

private void test(List<DuplicationsParser.Block> blocks, String expected) {
    StringWriter output = new StringWriter();
    JsonWriter jsonWriter = JsonWriter.of(output);
    jsonWriter.beginObject();
    writer.write(blocks, jsonWriter, session);
    jsonWriter.endObject();
    JsonAssert.assertJson(output.toString()).isSimilarTo(expected);
}
Also used : StringWriter(java.io.StringWriter) 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