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 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.component.ws;
  21. import com.google.common.collect.ImmutableSet;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.stream.Collectors;
  26. import javax.annotation.CheckForNull;
  27. import javax.annotation.Nullable;
  28. import org.sonar.api.resources.ResourceTypes;
  29. import org.sonar.api.server.ws.Change;
  30. import org.sonar.api.server.ws.Response;
  31. import org.sonar.api.server.ws.WebService;
  32. import org.sonar.api.server.ws.WebService.Param;
  33. import org.sonar.api.utils.Paging;
  34. import org.sonar.core.i18n.I18n;
  35. import org.sonar.db.DbClient;
  36. import org.sonar.db.DbSession;
  37. import org.sonar.db.entity.EntityDto;
  38. import org.sonar.server.component.index.ComponentIndex;
  39. import org.sonar.server.component.index.ComponentQuery;
  40. import org.sonar.server.es.SearchIdResult;
  41. import org.sonar.server.es.SearchOptions;
  42. import org.sonar.server.es.newindex.DefaultIndexSettings;
  43. import org.sonarqube.ws.Components;
  44. import org.sonarqube.ws.Components.SearchWsResponse;
  45. import static java.util.Objects.requireNonNull;
  46. import static java.util.stream.Collectors.toMap;
  47. import static org.sonar.api.resources.Qualifiers.APP;
  48. import static org.sonar.api.resources.Qualifiers.PROJECT;
  49. import static org.sonar.api.resources.Qualifiers.SUBVIEW;
  50. import static org.sonar.api.resources.Qualifiers.VIEW;
  51. import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE;
  52. import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter;
  53. import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
  54. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  55. import static org.sonarqube.ws.client.component.ComponentsWsParameters.ACTION_SEARCH;
  56. import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_QUALIFIERS;
  57. public class SearchAction implements ComponentsWsAction {
  58. private static final ImmutableSet<String> VALID_QUALIFIERS = ImmutableSet.<String>builder()
  59. .add(APP, PROJECT, VIEW, SUBVIEW)
  60. .build();
  61. private final ComponentIndex componentIndex;
  62. private final DbClient dbClient;
  63. private final ResourceTypes resourceTypes;
  64. private final I18n i18n;
  65. public SearchAction(ComponentIndex componentIndex, DbClient dbClient, ResourceTypes resourceTypes, I18n i18n) {
  66. this.componentIndex = componentIndex;
  67. this.dbClient = dbClient;
  68. this.resourceTypes = resourceTypes;
  69. this.i18n = i18n;
  70. }
  71. @Override
  72. public void define(WebService.NewController context) {
  73. WebService.NewAction action = context.createAction(ACTION_SEARCH)
  74. .setSince("6.3")
  75. .setDescription("Search for components")
  76. .addPagingParams(100, MAX_PAGE_SIZE)
  77. .setChangelog(
  78. new Change("8.4", "Param 'language' has been removed"),
  79. new Change("8.4", String.format("The use of 'DIR','FIL','UTS' and 'BRC' as values for parameter '%s' is no longer supported", PARAM_QUALIFIERS)),
  80. new Change("8.0", "Field 'id' from response has been removed"),
  81. new Change("7.6", String.format("The use of 'BRC' as value for parameter '%s' is deprecated", PARAM_QUALIFIERS)))
  82. .setResponseExample(getClass().getResource("search-components-example.json"))
  83. .setHandler(this);
  84. action.createParam(Param.TEXT_QUERY)
  85. .setDescription("Limit search to: <ul>" +
  86. "<li>component names that contain the supplied string</li>" +
  87. "<li>component keys that are exactly the same as the supplied string</li>" +
  88. "</ul><br>" +
  89. "The value length of the param must be between " + DefaultIndexSettings.MINIMUM_NGRAM_LENGTH + " and " +
  90. DefaultIndexSettings.MAXIMUM_NGRAM_LENGTH + " (inclusive) characters. In case longer value is provided it will be truncated.")
  91. .setExampleValue("sonar");
  92. createQualifiersParameter(action, newQualifierParameterContext(i18n, resourceTypes), VALID_QUALIFIERS)
  93. .setRequired(true);
  94. }
  95. @Override
  96. public void handle(org.sonar.api.server.ws.Request wsRequest, Response wsResponse) throws Exception {
  97. SearchWsResponse searchWsResponse = doHandle(toSearchWsRequest(wsRequest));
  98. writeProtobuf(searchWsResponse, wsRequest, wsResponse);
  99. }
  100. private static SearchRequest toSearchWsRequest(org.sonar.api.server.ws.Request request) {
  101. return new SearchRequest()
  102. .setQualifiers(request.mandatoryParamAsStrings(PARAM_QUALIFIERS))
  103. .setQuery(request.param(Param.TEXT_QUERY))
  104. .setPage(request.mandatoryParamAsInt(Param.PAGE))
  105. .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE));
  106. }
  107. private SearchWsResponse doHandle(SearchRequest request) {
  108. try (DbSession dbSession = dbClient.openSession(false)) {
  109. ComponentQuery esQuery = buildEsQuery(request);
  110. SearchIdResult<String> results = componentIndex.search(esQuery, new SearchOptions().setPage(request.getPage(), request.getPageSize()));
  111. List<EntityDto> components = dbClient.entityDao().selectByUuids(dbSession, results.getUuids());
  112. Map<String, String> projectKeysByUuids = searchProjectsKeysByUuids(dbSession, components);
  113. return buildResponse(components, projectKeysByUuids,
  114. Paging.forPageIndex(request.getPage()).withPageSize(request.getPageSize()).andTotal((int) results.getTotal()));
  115. }
  116. }
  117. private Map<String, String> searchProjectsKeysByUuids(DbSession dbSession, List<EntityDto> entities) {
  118. Set<String> projectUuidsToSearch = entities.stream()
  119. .map(EntityDto::getAuthUuid)
  120. .collect(Collectors.toSet());
  121. List<EntityDto> projects = dbClient.entityDao().selectByUuids(dbSession, projectUuidsToSearch);
  122. return projects.stream().collect(toMap(EntityDto::getUuid, EntityDto::getKey));
  123. }
  124. private static ComponentQuery buildEsQuery(SearchRequest request) {
  125. return ComponentQuery.builder()
  126. .setQuery(request.getQuery())
  127. .setQualifiers(request.getQualifiers())
  128. .build();
  129. }
  130. private static SearchWsResponse buildResponse(List<EntityDto> components, Map<String, String> projectKeysByUuids, Paging paging) {
  131. SearchWsResponse.Builder responseBuilder = SearchWsResponse.newBuilder();
  132. responseBuilder.getPagingBuilder()
  133. .setPageIndex(paging.pageIndex())
  134. .setPageSize(paging.pageSize())
  135. .setTotal(paging.total())
  136. .build();
  137. components.stream()
  138. .map(dto -> dtoToComponent(dto, projectKeysByUuids.get(dto.getAuthUuid())))
  139. .forEach(responseBuilder::addComponents);
  140. return responseBuilder.build();
  141. }
  142. private static Components.Component dtoToComponent(EntityDto dto, String projectKey) {
  143. Components.Component.Builder builder = Components.Component.newBuilder()
  144. .setKey(dto.getKey())
  145. .setProject(projectKey)
  146. .setName(dto.getName())
  147. .setQualifier(dto.getQualifier());
  148. return builder.build();
  149. }
  150. static class SearchRequest {
  151. private List<String> qualifiers;
  152. private Integer page;
  153. private Integer pageSize;
  154. private String query;
  155. public List<String> getQualifiers() {
  156. return qualifiers;
  157. }
  158. public SearchRequest setQualifiers(List<String> qualifiers) {
  159. this.qualifiers = requireNonNull(qualifiers);
  160. return this;
  161. }
  162. @CheckForNull
  163. public Integer getPage() {
  164. return page;
  165. }
  166. public SearchRequest setPage(int page) {
  167. this.page = page;
  168. return this;
  169. }
  170. @CheckForNull
  171. public Integer getPageSize() {
  172. return pageSize;
  173. }
  174. public SearchRequest setPageSize(int pageSize) {
  175. this.pageSize = pageSize;
  176. return this;
  177. }
  178. @CheckForNull
  179. public String getQuery() {
  180. return query;
  181. }
  182. public SearchRequest setQuery(@Nullable String query) {
  183. this.query = query;
  184. return this;
  185. }
  186. }
  187. }