Search in sources :

Example 6 with DynamicMap

use of com.google.gerrit.extensions.registration.DynamicMap in project gerrit by GerritCodeReview.

the class RestApiServlet method view.

private ViewData view(RestResource rsrc, RestCollection<RestResource, RestResource> rc, String method, List<IdString> path) throws AmbiguousViewException, RestApiException {
    DynamicMap<RestView<RestResource>> views = rc.views();
    final IdString projection = path.isEmpty() ? IdString.fromUrl("/") : path.remove(0);
    if (!path.isEmpty()) {
        // If there are path components still remaining after this projection
        // is chosen, look for the projection based upon GET as the method as
        // the client thinks it is a nested collection.
        method = "GET";
    } else if ("HEAD".equals(method)) {
        method = "GET";
    }
    List<String> p = splitProjection(projection);
    if (p.size() == 2) {
        String viewname = p.get(1);
        if (Strings.isNullOrEmpty(viewname)) {
            viewname = "/";
        }
        RestView<RestResource> view = views.get(p.get(0), method + "." + viewname);
        if (view != null) {
            return new ViewData(p.get(0), view);
        }
        view = views.get(p.get(0), "GET." + viewname);
        if (view != null) {
            if (view instanceof AcceptsPost && "POST".equals(method)) {
                @SuppressWarnings("unchecked") AcceptsPost<RestResource> ap = (AcceptsPost<RestResource>) view;
                return new ViewData(p.get(0), ap.post(rsrc));
            }
        }
        throw new ResourceNotFoundException(projection);
    }
    String name = method + "." + p.get(0);
    RestView<RestResource> core = views.get("gerrit", name);
    if (core != null) {
        return new ViewData(null, core);
    }
    core = views.get("gerrit", "GET." + p.get(0));
    if (core instanceof AcceptsPost && "POST".equals(method)) {
        @SuppressWarnings("unchecked") AcceptsPost<RestResource> ap = (AcceptsPost<RestResource>) core;
        return new ViewData(null, ap.post(rsrc));
    }
    Map<String, RestView<RestResource>> r = new TreeMap<>();
    for (String plugin : views.plugins()) {
        RestView<RestResource> action = views.get(plugin, name);
        if (action != null) {
            r.put(plugin, action);
        }
    }
    if (r.size() == 1) {
        Map.Entry<String, RestView<RestResource>> entry = Iterables.getOnlyElement(r.entrySet());
        return new ViewData(entry.getKey(), entry.getValue());
    } else if (r.isEmpty()) {
        throw new ResourceNotFoundException(projection);
    } else {
        throw new AmbiguousViewException(String.format("Projection %s is ambiguous: %s", name, r.keySet().stream().map(in -> in + "~" + projection).collect(joining(", "))));
    }
}
Also used : RestResource(com.google.gerrit.extensions.restapi.RestResource) RestView(com.google.gerrit.extensions.restapi.RestView) AcceptsPost(com.google.gerrit.extensions.restapi.AcceptsPost) IdString(com.google.gerrit.extensions.restapi.IdString) TreeMap(java.util.TreeMap) IdString(com.google.gerrit.extensions.restapi.IdString) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) Map(java.util.Map) TreeMap(java.util.TreeMap) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) HashMap(java.util.HashMap)

Aggregations

DynamicMap (com.google.gerrit.extensions.registration.DynamicMap)6 Map (java.util.Map)3 DownloadCommand (com.google.gerrit.extensions.config.DownloadCommand)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 TreeMap (java.util.TreeMap)2 DownloadInfo (com.google.gerrit.extensions.common.DownloadInfo)1 DownloadSchemeInfo (com.google.gerrit.extensions.common.DownloadSchemeInfo)1 CloneCommand (com.google.gerrit.extensions.config.CloneCommand)1 DownloadScheme (com.google.gerrit.extensions.config.DownloadScheme)1 PrivateInternals_DynamicMapImpl (com.google.gerrit.extensions.registration.PrivateInternals_DynamicMapImpl)1 ReloadableRegistrationHandle (com.google.gerrit.extensions.registration.ReloadableRegistrationHandle)1 AcceptsPost (com.google.gerrit.extensions.restapi.AcceptsPost)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 RestResource (com.google.gerrit.extensions.restapi.RestResource)1 RestView (com.google.gerrit.extensions.restapi.RestView)1 ArchiveFormat (com.google.gerrit.server.change.ArchiveFormat)1 PluginConfig (com.google.gerrit.server.config.PluginConfig)1 ProjectConfigEntry (com.google.gerrit.server.config.ProjectConfigEntry)1