Merge "Serve Polymer 2 host page, if 'p2' URL parameter is set"
diff --git a/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java b/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java
index 905c013..d7e9e44 100644
--- a/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java
+++ b/java/com/google/gerrit/httpd/raw/IndexHtmlUtil.java
@@ -49,10 +49,13 @@
       String canonicalURL,
       String cdnPath,
       String faviconPath,
+      Map<String, String[]> urlParameterMap,
       Function<String, SanitizedContent> urlInScriptTagOrdainer)
       throws URISyntaxException, RestApiException {
     return ImmutableMap.<String, Object>builder()
-        .putAll(staticTemplateData(canonicalURL, cdnPath, faviconPath, urlInScriptTagOrdainer))
+        .putAll(
+            staticTemplateData(
+                canonicalURL, cdnPath, faviconPath, urlParameterMap, urlInScriptTagOrdainer))
         .putAll(dynamicTemplateData(gerritApi))
         .build();
   }
@@ -94,6 +97,7 @@
       String canonicalURL,
       String cdnPath,
       String faviconPath,
+      Map<String, String[]> urlParameterMap,
       Function<String, SanitizedContent> urlInScriptTagOrdainer)
       throws URISyntaxException {
     String canonicalPath = computeCanonicalPath(canonicalURL);
@@ -116,6 +120,9 @@
     if (faviconPath != null) {
       data.put("faviconPath", faviconPath);
     }
+    if (urlParameterMap.containsKey("p2")) {
+      data.put("polymer2", "true");
+    }
     return data.build();
   }
 
diff --git a/java/com/google/gerrit/httpd/raw/IndexServlet.java b/java/com/google/gerrit/httpd/raw/IndexServlet.java
index 8299bfc..978f3eb 100644
--- a/java/com/google/gerrit/httpd/raw/IndexServlet.java
+++ b/java/com/google/gerrit/httpd/raw/IndexServlet.java
@@ -17,6 +17,7 @@
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static javax.servlet.http.HttpServletResponse.SC_OK;
 
+import com.google.common.collect.ImmutableMap;
 import com.google.common.io.Resources;
 import com.google.gerrit.common.Nullable;
 import com.google.gerrit.extensions.api.GerritApi;
@@ -28,6 +29,7 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.net.URISyntaxException;
+import java.util.Map;
 import java.util.function.Function;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -67,14 +69,16 @@
   protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
     SoyTofu.Renderer renderer;
     try {
+      Map<String, String[]> parameterMap = req.getParameterMap();
       // TODO(hiesel): Remove URL ordainer as parameter once Soy is consistent
+      ImmutableMap<String, Object> templateData =
+          IndexHtmlUtil.templateData(
+              gerritApi, canonicalUrl, cdnPath, faviconPath, parameterMap, urlOrdainer);
       renderer =
           soyTofu
               .newRenderer("com.google.gerrit.httpd.raw.Index")
               .setContentKind(SanitizedContent.ContentKind.HTML)
-              .setData(
-                  IndexHtmlUtil.templateData(
-                      gerritApi, canonicalUrl, cdnPath, faviconPath, urlOrdainer));
+              .setData(templateData);
     } catch (URISyntaxException | RestApiException e) {
       throw new IOException(e);
     }
diff --git a/javatests/com/google/gerrit/httpd/raw/IndexHtmlUtilTest.java b/javatests/com/google/gerrit/httpd/raw/IndexHtmlUtilTest.java
index a4b6ab2..d695c48 100644
--- a/javatests/com/google/gerrit/httpd/raw/IndexHtmlUtilTest.java
+++ b/javatests/com/google/gerrit/httpd/raw/IndexHtmlUtilTest.java
@@ -17,21 +17,42 @@
 import static com.google.common.truth.Truth.assertThat;
 import static com.google.gerrit.httpd.raw.IndexHtmlUtil.staticTemplateData;
 
+import com.google.common.collect.ImmutableMap;
 import com.google.template.soy.data.SanitizedContent;
 import com.google.template.soy.data.UnsafeSanitizedContentOrdainer;
+import java.util.HashMap;
 import org.junit.Test;
 
 public class IndexHtmlUtilTest {
   @Test
+  public void polymer2() throws Exception {
+    assertThat(
+            staticTemplateData(
+                "http://example.com/",
+                null,
+                null,
+                ImmutableMap.of("p2", new String[0]),
+                IndexHtmlUtilTest::ordain))
+        .containsExactly("canonicalPath", "", "polymer2", "true", "staticResourcePath", ordain(""));
+  }
+
+  @Test
   public void noPathAndNoCDN() throws Exception {
-    assertThat(staticTemplateData("http://example.com/", null, null, IndexHtmlUtilTest::ordain))
+    assertThat(
+            staticTemplateData(
+                "http://example.com/", null, null, new HashMap<>(), IndexHtmlUtilTest::ordain))
         .containsExactly("canonicalPath", "", "staticResourcePath", ordain(""));
   }
 
   @Test
   public void pathAndNoCDN() throws Exception {
     assertThat(
-            staticTemplateData("http://example.com/gerrit/", null, null, IndexHtmlUtilTest::ordain))
+            staticTemplateData(
+                "http://example.com/gerrit/",
+                null,
+                null,
+                new HashMap<>(),
+                IndexHtmlUtilTest::ordain))
         .containsExactly("canonicalPath", "/gerrit", "staticResourcePath", ordain("/gerrit"));
   }
 
@@ -42,6 +63,7 @@
                 "http://example.com/",
                 "http://my-cdn.com/foo/bar/",
                 null,
+                new HashMap<>(),
                 IndexHtmlUtilTest::ordain))
         .containsExactly(
             "canonicalPath", "", "staticResourcePath", ordain("http://my-cdn.com/foo/bar/"));
@@ -54,6 +76,7 @@
                 "http://example.com/gerrit",
                 "http://my-cdn.com/foo/bar/",
                 null,
+                new HashMap<>(),
                 IndexHtmlUtilTest::ordain))
         .containsExactly(
             "canonicalPath", "/gerrit", "staticResourcePath", ordain("http://my-cdn.com/foo/bar/"));