Line data Source code
1 : // Copyright (C) 2017 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.restapi.group; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.common.collect.Lists; 19 : import com.google.gerrit.entities.InternalGroup; 20 : import com.google.gerrit.extensions.client.ListGroupsOption; 21 : import com.google.gerrit.extensions.client.ListOption; 22 : import com.google.gerrit.extensions.common.GroupInfo; 23 : import com.google.gerrit.extensions.restapi.BadRequestException; 24 : import com.google.gerrit.extensions.restapi.MethodNotAllowedException; 25 : import com.google.gerrit.extensions.restapi.Response; 26 : import com.google.gerrit.extensions.restapi.RestReadView; 27 : import com.google.gerrit.extensions.restapi.TopLevelResource; 28 : import com.google.gerrit.index.query.QueryParseException; 29 : import com.google.gerrit.index.query.QueryResult; 30 : import com.google.gerrit.server.group.InternalGroupDescription; 31 : import com.google.gerrit.server.permissions.PermissionBackendException; 32 : import com.google.gerrit.server.query.group.GroupQueryBuilder; 33 : import com.google.gerrit.server.query.group.GroupQueryProcessor; 34 : import com.google.inject.Inject; 35 : import com.google.inject.Provider; 36 : import java.util.ArrayList; 37 : import java.util.EnumSet; 38 : import java.util.List; 39 : import org.kohsuke.args4j.Option; 40 : 41 : public class QueryGroups implements RestReadView<TopLevelResource> { 42 : private final GroupQueryBuilder queryBuilder; 43 : private final Provider<GroupQueryProcessor> queryProcessorProvider; 44 : private final GroupJson json; 45 : 46 : private String query; 47 : private int limit; 48 : private int start; 49 12 : private EnumSet<ListGroupsOption> options = EnumSet.noneOf(ListGroupsOption.class); 50 : 51 : @Option( 52 : name = "--query", 53 : aliases = {"-q"}, 54 : usage = "group query") 55 : public void setQuery(String query) { 56 12 : this.query = query; 57 12 : } 58 : 59 : @Option( 60 : name = "--limit", 61 : aliases = {"-n"}, 62 : metaVar = "CNT", 63 : usage = "maximum number of groups to list") 64 : public void setLimit(int limit) { 65 12 : this.limit = limit; 66 12 : } 67 : 68 : @Option( 69 : name = "--start", 70 : aliases = {"-S"}, 71 : metaVar = "CNT", 72 : usage = "number of groups to skip") 73 : public void setStart(int start) { 74 12 : this.start = start; 75 12 : } 76 : 77 : @Option(name = "-o", usage = "Output options per group") 78 : public void addOption(ListGroupsOption o) { 79 1 : options.add(o); 80 1 : } 81 : 82 : @Option(name = "-O", usage = "Output option flags, in hex") 83 : public void setOptionFlagsHex(String hex) throws BadRequestException { 84 0 : options.addAll(ListOption.fromHexString(ListGroupsOption.class, hex)); 85 0 : } 86 : 87 : @Inject 88 : protected QueryGroups( 89 : GroupQueryBuilder queryBuilder, 90 : Provider<GroupQueryProcessor> queryProcessorProvider, 91 12 : GroupJson json) { 92 12 : this.queryBuilder = queryBuilder; 93 12 : this.queryProcessorProvider = queryProcessorProvider; 94 12 : this.json = json; 95 12 : } 96 : 97 : @Override 98 : public Response<List<GroupInfo>> apply(TopLevelResource resource) 99 : throws BadRequestException, MethodNotAllowedException, PermissionBackendException { 100 12 : if (Strings.isNullOrEmpty(query)) { 101 0 : throw new BadRequestException("missing query field"); 102 : } 103 : 104 12 : GroupQueryProcessor queryProcessor = queryProcessorProvider.get(); 105 : 106 12 : if (queryProcessor.isDisabled()) { 107 0 : throw new MethodNotAllowedException("query disabled"); 108 : } 109 : 110 12 : if (start < 0) { 111 2 : throw new BadRequestException("'start' parameter cannot be less than zero"); 112 : } 113 : 114 12 : if (start != 0) { 115 2 : queryProcessor.setStart(start); 116 : } 117 : 118 12 : if (limit != 0) { 119 2 : queryProcessor.setUserProvidedLimit(limit); 120 : } 121 : 122 : try { 123 12 : QueryResult<InternalGroup> result = queryProcessor.query(queryBuilder.parse(query)); 124 12 : List<InternalGroup> groups = result.entities(); 125 : 126 12 : ArrayList<GroupInfo> groupInfos = Lists.newArrayListWithCapacity(groups.size()); 127 12 : json.addOptions(options); 128 12 : for (InternalGroup group : groups) { 129 12 : groupInfos.add(json.format(new InternalGroupDescription(group))); 130 12 : } 131 12 : if (!groupInfos.isEmpty() && result.more()) { 132 2 : groupInfos.get(groupInfos.size() - 1)._moreGroups = true; 133 : } 134 12 : return Response.ok(groupInfos); 135 2 : } catch (QueryParseException e) { 136 2 : throw new BadRequestException(e.getMessage()); 137 : } 138 : } 139 : }