Show plugins install screen at first login

When Gerrit administrator logs in for the very first time, he is
redirected to the plugin manager screen, allowing to discover
and install the desired plugins.

Change-Id: I57cbc91427e9f3a7ee2273f21ec4e8ad3352c83a
diff --git a/src/main/java/com/googlesource/gerrit/plugins/manager/FirstWebLoginListener.java b/src/main/java/com/googlesource/gerrit/plugins/manager/FirstWebLoginListener.java
new file mode 100644
index 0000000..0d18bd6
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/manager/FirstWebLoginListener.java
@@ -0,0 +1,67 @@
+// Copyright (C) 2016 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.googlesource.gerrit.plugins.manager;
+
+import com.google.gerrit.extensions.annotations.PluginCanonicalWebUrl;
+import com.google.gerrit.extensions.annotations.PluginData;
+import com.google.gerrit.httpd.WebLoginListener;
+import com.google.gerrit.server.IdentifiedUser;
+import com.google.gerrit.server.plugins.PluginLoader;
+import com.google.inject.Inject;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+import java.util.Date;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class FirstWebLoginListener implements WebLoginListener {
+  private final Path pluginData;
+  private final PluginLoader pluginLoader;
+  private final String pluginUrl;
+
+  @Inject
+  public FirstWebLoginListener(PluginLoader pluginLoader,
+      @PluginData Path pluginData,
+      @PluginCanonicalWebUrl String pluginUrl) {
+    this.pluginData = pluginData;
+    this.pluginLoader = pluginLoader;
+    this.pluginUrl = pluginUrl;
+  }
+
+  @Override
+  public void onLogin(IdentifiedUser user, HttpServletRequest request,
+      HttpServletResponse response) throws IOException {
+    if (pluginLoader.isRemoteAdminEnabled()
+        && user.getCapabilities().canAdministrateServer()) {
+      Path firstLoginFile =
+          pluginData.resolve("firstLogin." + user.getAccountId().get());
+      if (!firstLoginFile.toFile().exists()) {
+        response.sendRedirect(pluginUrl + "static/index.html");
+
+        Files.write(firstLoginFile, new Date().toString().getBytes(),
+            StandardOpenOption.CREATE);
+      }
+    }
+  }
+
+  @Override
+  public void onLogout(IdentifiedUser user, HttpServletRequest request,
+      HttpServletResponse response) {
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/manager/WebModule.java b/src/main/java/com/googlesource/gerrit/plugins/manager/WebModule.java
index 177372b..0cbc9c8 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/manager/WebModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/manager/WebModule.java
@@ -13,6 +13,8 @@
 // limitations under the License.
 package com.googlesource.gerrit.plugins.manager;
 
+import com.google.gerrit.extensions.registration.DynamicSet;
+import com.google.gerrit.httpd.WebLoginListener;
 import com.google.inject.servlet.ServletModule;
 
 public class WebModule extends ServletModule {
@@ -20,6 +22,8 @@
   @Override
   protected void configureServlets() {
     bind(AvailablePluginsCollection.class);
+    DynamicSet.bind(binder(), WebLoginListener.class)
+      .to(FirstWebLoginListener.class);
 
     serve("/available*").with(PluginManagerRestApiServlet.class);
 
diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html
index 3868747..1368fe6 100644
--- a/src/main/resources/static/index.html
+++ b/src/main/resources/static/index.html
@@ -18,7 +18,12 @@
   <nav class="navbar navbar-inverse navbar-fixed-top">
     <div class="container">
       <div class="navbar-header">
-        <a class="navbar-brand" href="#">Gerrit Plugin Manager</a>
+        <a class="navbar-brand">Gerrit Plugins</a>
+      </div>
+      <div class="navbar-collapse collapse">
+        <ul class="nav navbar-nav navbar-right">
+          <li><a href="/">&gt; Go to Gerrit</a></li>
+        </ul>
       </div>
     </div>
   </nav>