You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GhostsAction.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.project.ws;
  21. import com.google.common.io.Resources;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.Set;
  25. import javax.annotation.Nullable;
  26. import org.sonar.api.server.ws.Change;
  27. import org.sonar.api.server.ws.Request;
  28. import org.sonar.api.server.ws.Response;
  29. import org.sonar.api.server.ws.WebService;
  30. import org.sonar.api.server.ws.WebService.Param;
  31. import org.sonar.api.utils.text.JsonWriter;
  32. import org.sonar.db.DbClient;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.component.ComponentDto;
  35. import org.sonar.db.organization.OrganizationDto;
  36. import org.sonar.server.es.SearchOptions;
  37. import org.sonar.server.organization.DefaultOrganizationProvider;
  38. import org.sonar.server.user.UserSession;
  39. import static com.google.common.collect.Sets.newHashSet;
  40. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
  41. import static org.sonar.server.es.SearchOptions.MAX_LIMIT;
  42. import static org.sonar.server.project.Visibility.PRIVATE;
  43. import static org.sonar.server.project.Visibility.PUBLIC;
  44. import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
  45. public class GhostsAction implements ProjectsWsAction {
  46. private static final String PARAM_ORGANIZATION = "organization";
  47. private static final String ACTION = "ghosts";
  48. private static final Set<String> POSSIBLE_FIELDS = newHashSet("uuid", "key", "name", "creationDate", "visibility");
  49. private final DbClient dbClient;
  50. private final UserSession userSession;
  51. private final DefaultOrganizationProvider defaultOrganizationProvider;
  52. public GhostsAction(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider defaultOrganizationProvider) {
  53. this.dbClient = dbClient;
  54. this.userSession = userSession;
  55. this.defaultOrganizationProvider = defaultOrganizationProvider;
  56. }
  57. @Override
  58. public void define(WebService.NewController context) {
  59. WebService.NewAction action = context.createAction(ACTION);
  60. action.setChangelog(new Change("6.4", "The 'uuid' field is deprecated in the response"));
  61. action
  62. .setDescription("List ghost projects.<br> " +
  63. "With the current architecture, it's no more possible to have invisible ghost projects. Therefore, the web service is deprecated.<br> " +
  64. "Requires 'Administer System' permission.")
  65. .setResponseExample(Resources.getResource(getClass(), "ghosts-example.json"))
  66. .setSince("5.2")
  67. .setDeprecatedSince("6.6")
  68. .addPagingParams(100, MAX_LIMIT)
  69. .addFieldsParam(POSSIBLE_FIELDS)
  70. .addSearchQuery("sonar", "names", "keys")
  71. .setHandler(this);
  72. action.createParam(PARAM_ORGANIZATION)
  73. .setDescription("Organization key")
  74. .setRequired(false)
  75. .setInternal(true)
  76. .setSince("6.3");
  77. }
  78. @Override
  79. public void handle(Request request, Response response) throws Exception {
  80. userSession.checkLoggedIn();
  81. SearchOptions searchOptions = new SearchOptions()
  82. .setPage(request.mandatoryParamAsInt(Param.PAGE),
  83. request.mandatoryParamAsInt(Param.PAGE_SIZE));
  84. Set<String> desiredFields = fieldsToReturn(request.paramAsStrings(Param.FIELDS));
  85. String query = request.param(Param.TEXT_QUERY);
  86. try (DbSession dbSession = dbClient.openSession(false)) {
  87. OrganizationDto organization = getOrganization(dbSession, request);
  88. userSession.checkPermission(ADMINISTER, organization);
  89. long nbOfProjects = dbClient.componentDao().countGhostProjects(dbSession, organization.getUuid(), query);
  90. List<ComponentDto> projects = dbClient.componentDao().selectGhostProjects(dbSession, organization.getUuid(), query,
  91. searchOptions.getOffset(), searchOptions.getLimit());
  92. try (JsonWriter json = response.newJsonWriter()) {
  93. json.beginObject();
  94. writeProjects(json, projects, desiredFields);
  95. searchOptions.writeJson(json, nbOfProjects);
  96. json.endObject();
  97. }
  98. }
  99. }
  100. private OrganizationDto getOrganization(DbSession dbSession, Request request) {
  101. String organizationKey = request.getParam(PARAM_ORGANIZATION)
  102. .or(defaultOrganizationProvider.get()::getKey);
  103. return checkFoundWithOptional(
  104. dbClient.organizationDao().selectByKey(dbSession, organizationKey),
  105. "No organization for key '%s'", organizationKey);
  106. }
  107. private static void writeProjects(JsonWriter json, List<ComponentDto> projects, Set<String> fieldsToReturn) {
  108. json.name("projects");
  109. json.beginArray();
  110. for (ComponentDto project : projects) {
  111. json.beginObject();
  112. json.prop("uuid", project.uuid());
  113. writeIfWished(json, "key", project.getDbKey(), fieldsToReturn);
  114. writeIfWished(json, "name", project.name(), fieldsToReturn);
  115. writeIfWished(json, "creationDate", project.getCreatedAt(), fieldsToReturn);
  116. writeIfWished(json, "visibility", project.isPrivate() ? PRIVATE.getLabel() : PUBLIC.getLabel(), fieldsToReturn);
  117. json.endObject();
  118. }
  119. json.endArray();
  120. }
  121. private static void writeIfWished(JsonWriter json, String key, String value, Set<String> fieldsToReturn) {
  122. if (fieldsToReturn.contains(key)) {
  123. json.prop(key, value);
  124. }
  125. }
  126. private static void writeIfWished(JsonWriter json, String key, Date value, Set<String> desiredFields) {
  127. if (desiredFields.contains(key)) {
  128. json.propDateTime(key, value);
  129. }
  130. }
  131. private static Set<String> fieldsToReturn(@Nullable List<String> desiredFieldsFromRequest) {
  132. if (desiredFieldsFromRequest == null) {
  133. return POSSIBLE_FIELDS;
  134. }
  135. return newHashSet(desiredFieldsFromRequest);
  136. }
  137. }