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();
}
}
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();
}
}
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();
}
}
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);
}
}
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);
}
Aggregations