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.

SearchAction.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.component.ws;
  21. import com.google.common.collect.Sets;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.Set;
  25. import org.sonar.api.server.ws.Request;
  26. import org.sonar.api.server.ws.RequestHandler;
  27. import org.sonar.api.server.ws.Response;
  28. import org.sonar.api.server.ws.WebService;
  29. import org.sonar.api.server.ws.WebService.Param;
  30. import org.sonar.api.utils.text.JsonWriter;
  31. import org.sonar.api.web.UserRole;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.MyBatis;
  35. import org.sonar.server.db.DbClient;
  36. import org.sonar.server.es.SearchOptions;
  37. import org.sonar.server.user.UserSession;
  38. import static com.google.common.collect.Sets.newLinkedHashSet;
  39. import static org.sonar.api.server.ws.WebService.Param.PAGE;
  40. import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
  41. public class SearchAction implements RequestHandler {
  42. private static final short MINIMUM_SEARCH_CHARACTERS = 2;
  43. private static final String PARAM_COMPONENT_UUID = "componentUuid";
  44. private final DbClient dbClient;
  45. private final UserSession userSession;
  46. public SearchAction(DbClient dbClient, UserSession userSession) {
  47. this.dbClient = dbClient;
  48. this.userSession = userSession;
  49. }
  50. void define(WebService.NewController controller) {
  51. WebService.NewAction action = controller.createAction("search")
  52. .setDescription("Search for components. Currently limited to projects in a view or a sub-view")
  53. .setSince("5.1")
  54. .setInternal(true)
  55. .setHandler(this);
  56. action
  57. .createParam(PARAM_COMPONENT_UUID)
  58. .setRequired(true)
  59. .setDescription("View or sub view UUID")
  60. .setExampleValue("d6d9e1e5-5e13-44fa-ab82-3ec29efa8935");
  61. action
  62. .createParam(Param.TEXT_QUERY)
  63. .setRequired(true)
  64. .setDescription("UTF-8 search query")
  65. .setExampleValue("sonar");
  66. action.addPagingParams(10);
  67. }
  68. @Override
  69. public void handle(Request request, Response response) {
  70. String query = request.mandatoryParam(Param.TEXT_QUERY);
  71. if (query.length() < MINIMUM_SEARCH_CHARACTERS) {
  72. throw new IllegalArgumentException(String.format("Minimum search is %s characters", MINIMUM_SEARCH_CHARACTERS));
  73. }
  74. String viewOrSubUuid = request.mandatoryParam(PARAM_COMPONENT_UUID);
  75. JsonWriter json = response.newJsonWriter();
  76. json.beginObject();
  77. DbSession session = dbClient.openSession(false);
  78. try {
  79. ComponentDto componentDto = dbClient.componentDao().selectByUuid(session, viewOrSubUuid);
  80. userSession.checkProjectUuidPermission(UserRole.USER, componentDto.projectUuid());
  81. Set<Long> projectIds = newLinkedHashSet(dbClient.componentIndexDao().selectProjectIdsFromQueryAndViewOrSubViewUuid(session, query, componentDto.uuid()));
  82. Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(session, projectIds, userSession.getUserId(), UserRole.USER);
  83. SearchOptions options = new SearchOptions();
  84. options.setPage(request.mandatoryParamAsInt(PAGE), request.mandatoryParamAsInt(PAGE_SIZE));
  85. Set<Long> pagedProjectIds = pagedProjectIds(authorizedProjectIds, options);
  86. List<ComponentDto> projects = dbClient.componentDao().selectByIds(session, pagedProjectIds);
  87. options.writeJson(json, authorizedProjectIds.size());
  88. json.name("components").beginArray();
  89. for (ComponentDto project : projects) {
  90. json.beginObject();
  91. json.prop("uuid", project.uuid());
  92. json.prop("name", project.name());
  93. json.endObject();
  94. }
  95. json.endArray();
  96. } finally {
  97. MyBatis.closeQuietly(session);
  98. }
  99. json.endObject();
  100. json.close();
  101. }
  102. private static Set<Long> pagedProjectIds(Collection<Long> projectIds, SearchOptions options) {
  103. Set<Long> results = Sets.newLinkedHashSet();
  104. int index = 0;
  105. for (Long projectId : projectIds) {
  106. if (index >= options.getOffset() && results.size() < options.getLimit()) {
  107. results.add(projectId);
  108. } else if (results.size() >= options.getLimit()) {
  109. break;
  110. }
  111. index++;
  112. }
  113. return results;
  114. }
  115. }