import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Resources;
+import java.util.Collection;
+import java.util.List;
+import java.util.SortedMap;
+import java.util.regex.Pattern;
+import javax.annotation.Nullable;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService.Param;
import org.sonar.api.utils.text.JsonWriter;
-import javax.annotation.Nullable;
-
-import java.util.Collection;
-import java.util.List;
-import java.util.SortedMap;
-import java.util.regex.Pattern;
-
/**
* @since 5.1
*/
String query = request.param(Param.TEXT_QUERY);
int pageSize = request.mandatoryParamAsInt("ps");
- JsonWriter json = response.newJsonWriter().beginObject().name("languages").beginArray();
- for (Language language : listMatchingLanguages(query, pageSize)) {
- json.beginObject().prop("key", language.getKey()).prop("name", language.getName()).endObject();
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginObject().name("languages").beginArray();
+ for (Language language : listMatchingLanguages(query, pageSize)) {
+ json.beginObject().prop("key", language.getKey()).prop("name", language.getName()).endObject();
+ }
+ json.endArray().endObject();
}
- json.endArray().endObject().close();
}
void define(WebService.NewController controller) {
@Override
public void handle(Request request, Response response) throws Exception {
- JsonWriter json = response.newJsonWriter().beginObject().name("exporters").beginArray();
- for (ProfileExporter exporter : exporters) {
- json.beginObject()
- .prop("key", exporter.getKey())
- .prop("name", exporter.getName());
- json.name("languages").beginArray();
- for (String language : exporter.getSupportedLanguages()) {
- json.value(language);
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginObject().name("exporters").beginArray();
+ for (ProfileExporter exporter : exporters) {
+ json.beginObject()
+ .prop("key", exporter.getKey())
+ .prop("name", exporter.getName());
+ json.name("languages").beginArray();
+ for (String language : exporter.getSupportedLanguages()) {
+ json.value(language);
+ }
+ json.endArray().endObject();
}
json.endArray().endObject();
}
- json.endArray().endObject().close();
}
}
@Override
public void handle(Request request, Response response) throws Exception {
- JsonWriter json = response.newJsonWriter().beginObject().name("importers").beginArray();
- for (ProfileImporter importer : importers) {
- json.beginObject()
- .prop("key", importer.getKey())
- .prop("name", importer.getName())
- .name("languages").beginArray();
- for (String languageKey : importer.getSupportedLanguages()) {
- json.value(languageKey);
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginObject().name("importers").beginArray();
+ for (ProfileImporter importer : importers) {
+ json.beginObject()
+ .prop("key", importer.getKey())
+ .prop("name", importer.getName())
+ .name("languages").beginArray();
+ for (String languageKey : importer.getSupportedLanguages()) {
+ json.value(languageKey);
+ }
+ json.endArray().endObject();
}
json.endArray().endObject();
}
- json.endArray().endObject().close();
}
}
String query = request.param(Param.TEXT_QUERY);
String languageKey = request.param(LANGUAGE);
- JsonWriter json = response.newJsonWriter().beginObject().name("repositories").beginArray();
- for (RuleRepositoryDto repo : listMatchingRepositories(query, languageKey)) {
- json
- .beginObject()
- .prop("key", repo.getKey())
- .prop("name", repo.getName())
- .prop(LANGUAGE, repo.getLanguage())
- .endObject();
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginObject().name("repositories").beginArray();
+ for (RuleRepositoryDto repo : listMatchingRepositories(query, languageKey)) {
+ json
+ .beginObject()
+ .prop("key", repo.getKey())
+ .prop("name", repo.getName())
+ .prop(LANGUAGE, repo.getLanguage())
+ .endObject();
+ }
+ json.endArray().endObject();
}
- json.endArray().endObject().close();
}
private Collection<RuleRepositoryDto> listMatchingRepositories(@Nullable String query, @Nullable String languageKey) {
}
private static void writeResponse(Response response, List<String> tags) {
- JsonWriter json = response.newJsonWriter().beginObject();
- json.name("tags").beginArray();
- tags.forEach(json::value);
- json.endArray().endObject().close();
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginObject().name("tags").beginArray();
+ tags.forEach(json::value);
+ json.endArray().endObject();
+ }
}
}
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++;
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginArray().beginObject();
+ if (lines.isPresent()) {
+ int lineCounter = from;
+ for (String line : lines.get()) {
+ json.prop(String.valueOf(lineCounter), line);
+ lineCounter++;
+ }
}
+ json.endObject().endArray();
}
- json.endObject().endArray().close();
}
}
}
int to = MoreObjects.firstNonNull(request.paramAsInt(PARAM_TO), Integer.MAX_VALUE);
Iterable<DbFileSources.Line> lines = checkFoundWithOptional(sourceService.getLines(dbSession, file.uuid(), from, to), "No source found for file '%s'", file.key());
- JsonWriter json = response.newJsonWriter().beginObject();
- writeSource(lines, json);
- json.endObject().close();
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginObject();
+ writeSource(lines, json);
+ json.endObject();
+ }
}
}
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();
-
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginObject();
+ writeSource(linesHtml, from, json);
+ json.endObject();
+ }
}
}
@Override
public void handle(Request request, Response response) throws Exception {
- JsonWriter json = response.newJsonWriter().beginObject();
- writeActions(json);
- writePages(json);
- writeSettings(json);
- writeDeprecatedLogoProperties(json);
- writeQualifiers(json);
- writeVersion(json);
- writeDatabaseProduction(json);
- writeOrganizationSupport(json);
- json.endObject().close();
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginObject();
+ writeActions(json);
+ writePages(json);
+ writeSettings(json);
+ writeDeprecatedLogoProperties(json);
+ writeQualifiers(json);
+ writeVersion(json);
+ writeDatabaseProduction(json);
+ writeOrganizationSupport(json);
+ json.endObject();
+ }
}
private void writeActions(JsonWriter json) {
public void handle(Request request, Response response) throws Exception {
boolean isSysAdmin = userSession.isSystemAdministrator();
- JsonWriter json = response.newJsonWriter().beginObject();
- json.prop("showUpdateCenter", isSysAdmin && config.getBoolean(WebConstants.SONAR_UPDATECENTER_ACTIVATE).orElse(false));
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginObject();
+ json.prop("showUpdateCenter", isSysAdmin && config.getBoolean(WebConstants.SONAR_UPDATECENTER_ACTIVATE).orElse(false));
- json.name("extensions").beginArray();
- if (isSysAdmin) {
- for (Page page : pageRepository.getGlobalPages(true)) {
- json.beginObject()
- .prop("key", page.getKey())
- .prop("name", page.getName())
- .endObject();
+ json.name("extensions").beginArray();
+ if (isSysAdmin) {
+ for (Page page : pageRepository.getGlobalPages(true)) {
+ json.beginObject()
+ .prop("key", page.getKey())
+ .prop("name", page.getName())
+ .endObject();
+ }
}
+ json.endArray().endObject();
}
- json.endArray();
-
- json.endObject().close();
}
}
// when deactivating user
checkFound(user, "User '%s' doesn't exist", login);
- JsonWriter json = response.newJsonWriter().beginObject();
- json.name("user");
- Set<String> groups = Sets.newHashSet();
- groups.addAll(dbClient.groupMembershipDao().selectGroupsByLogins(dbSession, singletonList(login)).get(login));
- userWriter.write(json, user, groups, UserJsonWriter.FIELDS);
- json.endObject().close();
+ try (JsonWriter json = response.newJsonWriter()) {
+ json.beginObject();
+ json.name("user");
+ Set<String> groups = Sets.newHashSet();
+ groups.addAll(dbClient.groupMembershipDao().selectGroupsByLogins(dbSession, singletonList(login)).get(login));
+ userWriter.write(json, user, groups, UserJsonWriter.FIELDS);
+ json.endObject();
+ }
}
}