
org.sonar.server.component.ws.TreeAction Maven / Gradle / Ivy
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program 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.
*
* This program 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.sonar.server.component.ws;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.collect.FluentIterable.from;
import static com.google.common.collect.Sets.newHashSet;
import static java.lang.String.format;
import static java.util.Collections.emptyMap;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
import static org.sonar.server.component.ComponentFinder.ParamNames.BASE_COMPONENT_ID_AND_KEY;
import static org.sonar.server.component.ws.ComponentDtoToWsComponent.componentDtoToWsComponent;
import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter;
import static org.sonar.server.ws.WsUtils.checkRequest;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.ACTION_TREE;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_BASE_COMPONENT_ID;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_BASE_COMPONENT_KEY;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_QUALIFIERS;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_STRATEGY;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import org.sonar.api.i18n.I18n;
import org.sonar.api.resources.ResourceTypes;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.WebService.Param;
import org.sonar.api.utils.Paging;
import org.sonar.api.web.UserRole;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTreeQuery;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.WsComponents.TreeWsResponse;
import org.sonarqube.ws.client.component.TreeWsRequest;
public class TreeAction implements ComponentsWsAction {
private static final int MAX_SIZE = 500;
private static final int QUERY_MINIMUM_LENGTH = 3;
private static final String ALL_STRATEGY = "all";
private static final String CHILDREN_STRATEGY = "children";
private static final String LEAVES_STRATEGY = "leaves";
private static final Set STRATEGIES = ImmutableSortedSet.of(ALL_STRATEGY, CHILDREN_STRATEGY, LEAVES_STRATEGY);
private static final String NAME_SORT = "name";
private static final String PATH_SORT = "path";
private static final String QUALIFIER_SORT = "qualifier";
private static final Set SORTS = ImmutableSortedSet.of(NAME_SORT, PATH_SORT, QUALIFIER_SORT);
private final DbClient dbClient;
private final ComponentFinder componentFinder;
private final ResourceTypes resourceTypes;
private final UserSession userSession;
private final I18n i18n;
public TreeAction(DbClient dbClient, ComponentFinder componentFinder, ResourceTypes resourceTypes, UserSession userSession, I18n i18n) {
this.dbClient = dbClient;
this.componentFinder = componentFinder;
this.resourceTypes = resourceTypes;
this.userSession = userSession;
this.i18n = i18n;
}
@Override
public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction(ACTION_TREE)
.setDescription(format("Navigate through components based on the chosen strategy. The %s or the %s parameter must be provided.
" +
"Requires one of the following permissions:" +
"" +
"- 'Administer System'
" +
"- 'Administer' rights on the specified project
" +
"- 'Browse' on the specified project
" +
"
" +
"When limiting search with the %s parameter, directories are not returned.",
PARAM_BASE_COMPONENT_ID, PARAM_BASE_COMPONENT_KEY, Param.TEXT_QUERY))
.setSince("5.4")
.setResponseExample(getClass().getResource("tree-example.json"))
.setHandler(this)
.addPagingParams(100, MAX_SIZE);
action.createParam(PARAM_BASE_COMPONENT_ID)
.setDescription("Base component id. The search is based on this component.")
.setExampleValue(UUID_EXAMPLE_02);
action.createParam(PARAM_BASE_COMPONENT_KEY)
.setDescription("Base component key.The search is based on this component.")
.setExampleValue(KEY_PROJECT_EXAMPLE_001);
action.createSortParams(SORTS, NAME_SORT, true)
.setDescription("Comma-separated list of sort fields")
.setExampleValue(NAME_SORT + ", " + PATH_SORT);
action.createParam(Param.TEXT_QUERY)
.setDescription(format("Limit search to: " +
"- component names that contain the supplied string
" +
"- component keys that are exactly the same as the supplied string
" +
"
" +
"Must have at least %d characters", QUERY_MINIMUM_LENGTH))
.setExampleValue("FILE_NAM");
createQualifiersParameter(action, newQualifierParameterContext(i18n, resourceTypes));
action.createParam(PARAM_STRATEGY)
.setDescription("Strategy to search for base component descendants:" +
"" +
"- children: return the children components of the base component. Grandchildren components are not returned
" +
"- all: return all the descendants components of the base component. Grandchildren are returned.
" +
"- leaves: return all the descendant components (files, in general) which don't have other children. They are the leaves of the component tree.
" +
"
")
.setPossibleValues(STRATEGIES)
.setDefaultValue(ALL_STRATEGY);
}
@Override
public void handle(Request request, Response response) throws Exception {
TreeWsResponse treeWsResponse = doHandle(toTreeWsRequest(request));
writeProtobuf(treeWsResponse, request, response);
}
private TreeWsResponse doHandle(TreeWsRequest treeWsRequest) {
DbSession dbSession = dbClient.openSession(false);
try {
ComponentDto baseComponent = componentFinder.getByUuidOrKey(dbSession, treeWsRequest.getBaseComponentId(), treeWsRequest.getBaseComponentKey(), BASE_COMPONENT_ID_AND_KEY);
checkPermissions(baseComponent);
ComponentTreeQuery query = toComponentTreeQuery(treeWsRequest, baseComponent);
List components;
int total;
switch (treeWsRequest.getStrategy()) {
case CHILDREN_STRATEGY:
components = dbClient.componentDao().selectChildren(dbSession, query);
total = dbClient.componentDao().countChildren(dbSession, query);
break;
case LEAVES_STRATEGY:
case ALL_STRATEGY:
components = dbClient.componentDao().selectDescendants(dbSession, query);
total = dbClient.componentDao().countDescendants(dbSession, query);
break;
default:
throw new IllegalStateException("Unknown component tree strategy");
}
Map referenceComponentsByUuid = searchReferenceComponentsByUuid(dbSession, components);
return buildResponse(baseComponent, components, referenceComponentsByUuid,
Paging.forPageIndex(query.getPage()).withPageSize(query.getPageSize()).andTotal(total));
} finally {
dbClient.closeSession(dbSession);
}
}
private Map searchReferenceComponentsByUuid(DbSession dbSession, List components) {
List referenceComponentIds = from(components)
.transform(ComponentDto::getCopyResourceUuid)
.filter(Predicates.notNull())
.toList();
if (referenceComponentIds.isEmpty()) {
return emptyMap();
}
return from(dbClient.componentDao().selectByUuids(dbSession, referenceComponentIds))
.uniqueIndex(ComponentDto::uuid);
}
private void checkPermissions(ComponentDto baseComponent) {
String projectUuid = firstNonNull(baseComponent.projectUuid(), baseComponent.uuid());
if (!userSession.hasPermission(GlobalPermissions.SYSTEM_ADMIN) &&
!userSession.hasComponentUuidPermission(UserRole.ADMIN, projectUuid) &&
!userSession.hasComponentUuidPermission(UserRole.USER, projectUuid)) {
throw insufficientPrivilegesException();
}
}
private static TreeWsResponse buildResponse(ComponentDto baseComponent, List components,
Map referenceComponentsByUuid, Paging paging) {
TreeWsResponse.Builder response = TreeWsResponse.newBuilder();
response.getPagingBuilder()
.setPageIndex(paging.pageIndex())
.setPageSize(paging.pageSize())
.setTotal(paging.total())
.build();
response.setBaseComponent(componentDtoToWsComponent(baseComponent, referenceComponentsByUuid));
for (ComponentDto dto : components) {
response.addComponents(componentDtoToWsComponent(dto, referenceComponentsByUuid));
}
return response.build();
}
private ComponentTreeQuery toComponentTreeQuery(TreeWsRequest request, ComponentDto baseComponent) {
List childrenQualifiers = childrenQualifiers(request, baseComponent.qualifier());
ComponentTreeQuery.Builder query = ComponentTreeQuery.builder()
.setBaseUuid(baseComponent.uuid())
.setPage(request.getPage())
.setPageSize(request.getPageSize())
.setSortFields(request.getSort())
.setAsc(request.getAsc());
if (request.getQuery() != null) {
query.setNameOrKeyQuery(request.getQuery());
}
if (childrenQualifiers != null) {
query.setQualifiers(childrenQualifiers);
}
return query.build();
}
@CheckForNull
private List childrenQualifiers(TreeWsRequest request, String baseQualifier) {
List requestQualifiers = request.getQualifiers();
List childrenQualifiers = null;
if (LEAVES_STRATEGY.equals(request.getStrategy())) {
childrenQualifiers = resourceTypes.getLeavesQualifiers(baseQualifier);
}
if (requestQualifiers == null) {
return childrenQualifiers;
}
if (childrenQualifiers == null) {
return requestQualifiers;
}
Sets.SetView qualifiersIntersection = Sets.intersection(newHashSet(childrenQualifiers), newHashSet(requestQualifiers));
return new ArrayList<>(qualifiersIntersection);
}
private static TreeWsRequest toTreeWsRequest(Request request) {
TreeWsRequest treeWsRequest = new TreeWsRequest()
.setBaseComponentId(request.param(PARAM_BASE_COMPONENT_ID))
.setBaseComponentKey(request.param(PARAM_BASE_COMPONENT_KEY))
.setStrategy(request.param(PARAM_STRATEGY))
.setQuery(request.param(Param.TEXT_QUERY))
.setQualifiers(request.paramAsStrings(PARAM_QUALIFIERS))
.setSort(request.mandatoryParamAsStrings(Param.SORT))
.setAsc(request.mandatoryParamAsBoolean(Param.ASCENDING))
.setPage(request.mandatoryParamAsInt(Param.PAGE))
.setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE));
checkRequest(treeWsRequest.getPageSize() <= MAX_SIZE, "The '%s' parameter must be less than %d", Param.PAGE_SIZE, MAX_SIZE);
String searchQuery = treeWsRequest.getQuery();
checkRequest(searchQuery == null || searchQuery.length() >= QUERY_MINIMUM_LENGTH,
"The '%s' parameter must have at least %d characters", Param.TEXT_QUERY, QUERY_MINIMUM_LENGTH);
return treeWsRequest;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy