use of com.google.gerrit.extensions.restapi.RestView 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(", "))));
}
}
Aggregations