Add new optional interface for REST collections to get request params

REST collections decide which REST endpoint is used to handle GET
requests on the collection by returning the implementation of this
REST endpoint from the list() method. Sometimes depending on the
presence of certain URL parameters it makes sense to let different
REST endpoints handle the listing. For this the REST collection needs
to know which URL parameters were specified. By implementing the new
NeedsParams interface the REST collection can now get to know the URL
parameters.

Change-Id: I78a7b7a26324c63bc7ee24e7f0928f907146981d
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/restapi/NeedsParams.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/restapi/NeedsParams.java
new file mode 100644
index 0000000..8eb3413
--- /dev/null
+++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/restapi/NeedsParams.java
@@ -0,0 +1,32 @@
+// Copyright (C) 2017 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.extensions.restapi;
+
+import com.google.common.collect.Multimap;
+
+/**
+ * Optional interface for {@link RestCollection}.
+ * <p>
+ * Collections that implement this interface can get to know about the request
+ * parameters.
+ */
+public interface NeedsParams {
+  /**
+   * Sets the request parameter.
+   *
+   * @param params the request parameter
+   */
+  void setParams(Multimap<String, String> params);
+}
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java
index c1a3eec..e04fc7b 100644
--- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/restapi/RestApiServlet.java
@@ -74,6 +74,7 @@
 import com.google.gerrit.extensions.restapi.ETagView;
 import com.google.gerrit.extensions.restapi.IdString;
 import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
+import com.google.gerrit.extensions.restapi.NeedsParams;
 import com.google.gerrit.extensions.restapi.NotImplementedException;
 import com.google.gerrit.extensions.restapi.PreconditionFailedException;
 import com.google.gerrit.extensions.restapi.RawInput;
@@ -245,6 +246,7 @@
     long responseBytes = -1;
     Object result = null;
     Multimap<String, String> params = LinkedHashMultimap.create();
+    Multimap<String, String> config = LinkedHashMultimap.create();
     Object inputRequestBody = null;
     RestResource rsrc = TopLevelResource.INSTANCE;
     ViewData viewData = null;
@@ -257,6 +259,8 @@
       checkCors(req, res);
       checkUserSession(req);
 
+      ParameterParser.splitQueryString(req.getQueryString(), config, params);
+
       List<IdString> path = splitPath(req);
       RestCollection<RestResource, RestResource> rc = members.get();
       CapabilityUtils.checkRequiresCapability(globals.currentUser,
@@ -265,6 +269,10 @@
       viewData = new ViewData(null, null);
 
       if (path.isEmpty()) {
+        if (rc instanceof NeedsParams) {
+          ((NeedsParams)rc).setParams(params);
+        }
+
         if (isRead(req)) {
           viewData = new ViewData(null, rc.list());
         } else if (rc instanceof AcceptsPost && "POST".equals(req.getMethod())) {
@@ -357,8 +365,6 @@
         return;
       }
 
-      Multimap<String, String> config = LinkedHashMultimap.create();
-      ParameterParser.splitQueryString(req.getQueryString(), config, params);
       if (!globals.paramParser.get().parse(viewData.view, params, req, res)) {
         return;
       }