Search in sources :

Example 1 with TopLevelResource

use of com.google.gerrit.extensions.restapi.TopLevelResource in project gerrit by GerritCodeReview.

the class GroupsImpl method list.

private SortedMap<String, GroupInfo> list(ListRequest req) throws RestApiException {
    TopLevelResource tlr = TopLevelResource.INSTANCE;
    ListGroups list = listGroups.get();
    list.setOptions(req.getOptions());
    for (String project : req.getProjects()) {
        try {
            ProjectResource rsrc = projects.parse(tlr, IdString.fromDecoded(project));
            list.addProject(rsrc.getProjectState());
        } catch (Exception e) {
            throw asRestApiException("Error looking up project " + project, e);
        }
    }
    for (String group : req.getGroups()) {
        list.addGroup(groupResolver.parse(group).getGroupUUID());
    }
    list.setVisibleToAll(req.getVisibleToAll());
    if (req.getOwnedBy() != null) {
        list.setOwnedBy(req.getOwnedBy());
    }
    if (req.getUser() != null) {
        try {
            list.setUser(accountResolver.resolve(req.getUser()).asUnique().account().id());
        } catch (Exception e) {
            throw asRestApiException("Error looking up user " + req.getUser(), e);
        }
    }
    list.setOwned(req.getOwned());
    list.setLimit(req.getLimit());
    list.setStart(req.getStart());
    list.setMatchSubstring(req.getSubstring());
    list.setMatchRegex(req.getRegex());
    list.setSuggest(req.getSuggest());
    try {
        return list.apply(tlr).value();
    } catch (Exception e) {
        throw asRestApiException("Cannot list groups", e);
    }
}
Also used : TopLevelResource(com.google.gerrit.extensions.restapi.TopLevelResource) ListGroups(com.google.gerrit.server.restapi.group.ListGroups) ProjectResource(com.google.gerrit.server.project.ProjectResource) IdString(com.google.gerrit.extensions.restapi.IdString) ApiUtil.asRestApiException(com.google.gerrit.server.api.ApiUtil.asRestApiException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 2 with TopLevelResource

use of com.google.gerrit.extensions.restapi.TopLevelResource in project gerrit by GerritCodeReview.

the class ListPlugins method apply.

@Override
public Response<SortedMap<String, PluginInfo>> apply(TopLevelResource resource) throws BadRequestException {
    Stream<Plugin> s = Streams.stream(pluginLoader.getPlugins(all));
    if (matchPrefix != null) {
        checkMatchOptions(matchSubstring == null && matchRegex == null);
        s = s.filter(p -> p.getName().startsWith(matchPrefix));
    } else if (matchSubstring != null) {
        checkMatchOptions(matchPrefix == null && matchRegex == null);
        String substring = matchSubstring.toLowerCase(Locale.US);
        s = s.filter(p -> p.getName().toLowerCase(Locale.US).contains(substring));
    } else if (matchRegex != null) {
        checkMatchOptions(matchPrefix == null && matchSubstring == null);
        Pattern pattern = Pattern.compile(matchRegex);
        s = s.filter(p -> pattern.matcher(p.getName()).matches());
    }
    s = s.sorted(comparing(Plugin::getName));
    if (start > 0) {
        s = s.skip(start);
    }
    if (limit > 0) {
        s = s.limit(limit);
    }
    return Response.ok(new TreeMap<>(s.collect(toMap(Plugin::getName, ListPlugins::toPluginInfo))));
}
Also used : RestReadView(com.google.gerrit.extensions.restapi.RestReadView) GlobalCapability(com.google.gerrit.common.data.GlobalCapability) RequiresCapability(com.google.gerrit.extensions.annotations.RequiresCapability) Inject(com.google.inject.Inject) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Option(org.kohsuke.args4j.Option) Streams(com.google.common.collect.Streams) Response(com.google.gerrit.extensions.restapi.Response) TopLevelResource(com.google.gerrit.extensions.restapi.TopLevelResource) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) TreeMap(java.util.TreeMap) Locale(java.util.Locale) Plugins(com.google.gerrit.extensions.api.plugins.Plugins) Url(com.google.gerrit.extensions.restapi.Url) PluginInfo(com.google.gerrit.extensions.common.PluginInfo) Comparator.comparing(java.util.Comparator.comparing) Pattern(java.util.regex.Pattern) SortedMap(java.util.SortedMap) Pattern(java.util.regex.Pattern)

Aggregations

BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 TopLevelResource (com.google.gerrit.extensions.restapi.TopLevelResource)2 Streams (com.google.common.collect.Streams)1 GlobalCapability (com.google.gerrit.common.data.GlobalCapability)1 RequiresCapability (com.google.gerrit.extensions.annotations.RequiresCapability)1 Plugins (com.google.gerrit.extensions.api.plugins.Plugins)1 PluginInfo (com.google.gerrit.extensions.common.PluginInfo)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 Response (com.google.gerrit.extensions.restapi.Response)1 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)1 RestReadView (com.google.gerrit.extensions.restapi.RestReadView)1 Url (com.google.gerrit.extensions.restapi.Url)1 ApiUtil.asRestApiException (com.google.gerrit.server.api.ApiUtil.asRestApiException)1 ProjectResource (com.google.gerrit.server.project.ProjectResource)1 ListGroups (com.google.gerrit.server.restapi.group.ListGroups)1 Inject (com.google.inject.Inject)1 Comparator.comparing (java.util.Comparator.comparing)1 Locale (java.util.Locale)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1