import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentQuery;
import org.sonar.server.user.UserSession;
-import org.sonarqube.ws.WsComponents.WsSearchResponse;
+import org.sonarqube.ws.WsComponents;
+import org.sonarqube.ws.WsComponents.SearchWsResponse;
+import org.sonarqube.ws.client.component.SearchWsRequest;
import static com.google.common.collect.FluentIterable.from;
import static com.google.common.collect.Ordering.natural;
import static java.lang.String.format;
import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER;
import static org.sonar.server.component.ws.WsComponentsParameters.PARAM_QUALIFIERS;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
import static org.sonar.server.ws.WsUtils.checkRequest;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
public class SearchAction implements ComponentsWsAction {
private static final String QUALIFIER_PROPERTY_PREFIX = "qualifiers.";
@Override
public void handle(Request wsRequest, Response wsResponse) throws Exception {
+ SearchWsResponse searchWsResponse = doHandle(toSearchWsRequest(wsRequest));
+ writeProtobuf(searchWsResponse, wsRequest, wsResponse);
+ }
+
+ private SearchWsResponse doHandle(SearchWsRequest request) {
userSession.checkLoggedIn().checkGlobalPermission(GlobalPermissions.SYSTEM_ADMIN);
- List<String> qualifiers = wsRequest.mandatoryParamAsStrings(PARAM_QUALIFIERS);
+ List<String> qualifiers = request.getQualifiers();
validateQualifiers(qualifiers);
DbSession dbSession = dbClient.openSession(false);
try {
- ComponentQuery query = buildQuery(wsRequest, qualifiers);
- Paging paging = buildPaging(dbSession, wsRequest, query);
+ ComponentQuery query = buildQuery(request, qualifiers);
+ Paging paging = buildPaging(dbSession, request, query);
List<ComponentDto> components = searchComponents(dbSession, query, paging);
- WsSearchResponse response = buildResponse(components, paging);
- writeProtobuf(response, wsRequest, wsResponse);
+ return buildResponse(components, paging);
} finally {
dbClient.closeSession(dbSession);
}
}
+ private static SearchWsRequest toSearchWsRequest(Request request) {
+ return new SearchWsRequest()
+ .setQualifiers(request.mandatoryParamAsStrings(PARAM_QUALIFIERS))
+ .setQuery(request.param(Param.TEXT_QUERY))
+ .setPage(request.mandatoryParamAsInt(Param.PAGE))
+ .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE));
+ }
+
private List<ComponentDto> searchComponents(DbSession dbSession, ComponentQuery query, Paging paging) {
return dbClient.componentDao().selectByQuery(
dbSession,
paging.pageSize());
}
- private WsSearchResponse buildResponse(List<ComponentDto> components, Paging paging) {
- WsSearchResponse.Builder responseBuilder = WsSearchResponse.newBuilder();
+ private SearchWsResponse buildResponse(List<ComponentDto> components, Paging paging) {
+ WsComponents.SearchWsResponse.Builder responseBuilder = SearchWsResponse.newBuilder();
responseBuilder.getPagingBuilder()
.setPageIndex(paging.pageIndex())
.setPageSize(paging.pageSize())
return responseBuilder.build();
}
- private Paging buildPaging(DbSession dbSession, Request wsRequest, ComponentQuery query) {
+ private Paging buildPaging(DbSession dbSession, SearchWsRequest request, ComponentQuery query) {
int total = dbClient.componentDao().countByQuery(dbSession, query);
- return Paging.forPageIndex(wsRequest.mandatoryParamAsInt(Param.PAGE))
- .withPageSize(wsRequest.mandatoryParamAsInt(Param.PAGE_SIZE))
+ return Paging.forPageIndex(request.getPage())
+ .withPageSize(request.getPageSize())
.andTotal(total);
}
- private ComponentQuery buildQuery(Request wsRequest, List<String> qualifiers) {
+ private ComponentQuery buildQuery(SearchWsRequest request, List<String> qualifiers) {
return new ComponentQuery(
- wsRequest.param(Param.TEXT_QUERY),
+ request.getQuery(),
qualifiers.toArray(new String[qualifiers.size()]));
}
return i18n.message(userSession.locale(), QUALIFIER_PROPERTY_PREFIX + qualifier, "");
}
- private enum ComponentDToComponentResponseFunction implements Function<ComponentDto, WsSearchResponse.Component> {
+ private enum ComponentDToComponentResponseFunction implements Function<ComponentDto, WsComponents.SearchWsResponse.Component> {
INSTANCE;
@Override
- public WsSearchResponse.Component apply(@Nonnull ComponentDto dto) {
- return WsSearchResponse.Component.newBuilder()
+ public WsComponents.SearchWsResponse.Component apply(@Nonnull ComponentDto dto) {
+ return SearchWsResponse.Component.newBuilder()
.setId(dto.uuid())
.setKey(dto.key())
.setName(dto.name())
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.i18n.I18nRule;
-import org.sonarqube.ws.MediaTypes;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
-import org.sonarqube.ws.WsComponents.WsSearchResponse;
+import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.WsComponents.SearchWsResponse;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
.setParam(Param.PAGE_SIZE, "3")
.execute()
.getInputStream();
- WsSearchResponse response = WsSearchResponse.parseFrom(responseStream);
+ SearchWsResponse response = SearchWsResponse.parseFrom(responseStream);
assertThat(response.getComponentsCount()).isEqualTo(3);
assertThat(response.getComponentsList()).extracting("id").containsExactly("project-uuid-4", "project-uuid-5", "project-uuid-6");
InputStream responseStream = newRequest(Qualifiers.PROJECT)
.setParam(Param.TEXT_QUERY, "project-_%")
.execute().getInputStream();
- WsSearchResponse response = WsSearchResponse.parseFrom(responseStream);
+ SearchWsResponse response = SearchWsResponse.parseFrom(responseStream);
assertThat(response.getComponentsCount()).isEqualTo(1);
assertThat(response.getComponentsList()).extracting("key").containsExactly("project-_%-key");
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonarqube.ws.client.component;
+
+import org.sonarqube.ws.WsComponents.SearchWsResponse;
+import org.sonarqube.ws.client.WsClient;
+
+import static org.sonarqube.ws.client.WsRequest.newGetRequest;
+
+public class ComponentsWsClient {
+ private static final String ENDPOINT = "api/components/";
+ private final WsClient wsClient;
+
+ public ComponentsWsClient(WsClient wsClient) {
+ this.wsClient = wsClient;
+ }
+
+ public SearchWsResponse search(SearchWsRequest request) {
+ return wsClient.execute(
+ newGetRequest(action("search"))
+ .setParam("qualifiers", request.getQualifiers())
+ .setParam("p", request.getPage())
+ .setParam("ps", request.getPageSize())
+ .setParam("q", request.getQuery()),
+ SearchWsResponse.parser());
+ }
+
+ private static String action(String action) {
+ return ENDPOINT + action;
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonarqube.ws.client.component;
+
+import java.util.List;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+import static java.util.Objects.requireNonNull;
+
+public class SearchWsRequest {
+ private List<String> qualifiers;
+ private Integer page;
+ private Integer pageSize;
+ private String query;
+
+ public List<String> getQualifiers() {
+ return qualifiers;
+ }
+
+ public SearchWsRequest setQualifiers(List<String> qualifiers) {
+ this.qualifiers = requireNonNull(qualifiers);
+ return this;
+ }
+
+ @CheckForNull
+ public Integer getPage() {
+ return page;
+ }
+
+ public SearchWsRequest setPage(int page) {
+ this.page = page;
+ return this;
+ }
+
+ @CheckForNull
+ public Integer getPageSize() {
+ return pageSize;
+ }
+
+ public SearchWsRequest setPageSize(int pageSize) {
+ this.pageSize = pageSize;
+ return this;
+ }
+
+ @CheckForNull
+ public String getQuery() {
+ return query;
+ }
+
+ public SearchWsRequest setQuery(@Nullable String query) {
+ this.query = query;
+ return this;
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonarqube.ws.client.component;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
public SearchProjectPermissionsWsResponse searchProjectPermissions(SearchProjectPermissionsWsRequest request) {
return wsClient.execute(
- newGetRequest("search_project_permissions")
+ newGetRequest(action("search_project_permissions"))
.setParam(PARAM_PROJECT_ID, request.getProjectId())
.setParam(PARAM_PROJECT_KEY, request.getProjectKey())
.setParam("p", request.getPage())
public SearchTemplatesWsResponse searchTemplates(SearchTemplatesWsRequest request) {
return wsClient.execute(
- newGetRequest("search_templates")
+ newGetRequest(action("search_templates"))
.setParam("q", request.getQuery()),
SearchTemplatesWsResponse.parser());
}
public void setDefaultTemplate(SetDefaultTemplateWsRequest request) {
wsClient.execute(
- newPostRequest("set_default_template")
+ newPostRequest(action("set_default_template"))
.setParam(PARAM_QUALIFIER, request.getQualifier())
.setParam(PARAM_TEMPLATE_ID, request.getTemplateId())
.setParam(PARAM_TEMPLATE_NAME, request.getTemplateName()));
public UpdateTemplateWsResponse updateTemplate(UpdateTemplateWsRequest request) {
return wsClient.execute(
- newPostRequest("update_template")
+ newPostRequest(action("update_template"))
.setParam(PARAM_DESCRIPTION, request.getDescription())
.setParam(PARAM_ID, request.getId())
.setParam(PARAM_NAME, request.getName())
public UsersWsResponse users(UsersWsRequest request) {
return wsClient.execute(
- newGetRequest("users")
+ newGetRequest(action("users"))
.setParam(PARAM_PERMISSION, request.getPermission())
.setParam(PARAM_PROJECT_ID, request.getProjectId())
.setParam(PARAM_PROJECT_KEY, request.getProjectKey())
public SearchWsResponse search(SearchWsRequest request) {
return wsClient.execute(
- newGetRequest("search")
+ newGetRequest(action("search"))
.setParam("defaults", request.getDefaults())
.setParam("language", request.getLanguage())
.setParam("profileName", request.getProfileName())
.setParam("projectKey", request.getProjectKey()),
SearchWsResponse.parser());
}
+
+ private static String action(String action) {
+ return "api/qualityprofiles/" + action;
+ }
}
option optimize_for = SPEED;
// WS api/components/search
-message WsSearchResponse {
+message SearchWsResponse {
message Component {
optional string id = 1;
optional string key = 2;
@Test
public void return_protobuf_response() throws Exception {
server.doReturnBody(
- WsComponents.WsSearchResponse
+ WsComponents.SearchWsResponse
.newBuilder()
- .addComponents(WsComponents.WsSearchResponse.Component.getDefaultInstance())
+ .addComponents(WsComponents.SearchWsResponse.Component.getDefaultInstance())
.build()
.toByteArray());
server.doReturnStatus(HTTP_OK);
server.doReturnContentType(MediaTypes.PROTOBUF);
- WsComponents.WsSearchResponse response = underTest.execute(
+ WsComponents.SearchWsResponse response = underTest.execute(
newGetRequest("api/components/search")
.setMediaType(WsRequest.MediaType.PROTOBUF),
- WsComponents.WsSearchResponse.parser());
+ WsComponents.SearchWsResponse.parser());
assertThat(response.getComponentsCount()).isEqualTo(1);
assertThat(server.requestHeaders().get(HttpHeaders.ACCEPT))