Initial commit of Login-Redirect plugin

Change-Id: I5ba2676e4816f5b41594f51f58eb2666eaa2fbde
diff --git a/BUCK b/BUCK
new file mode 100644
index 0000000..80e5325
--- /dev/null
+++ b/BUCK
@@ -0,0 +1,17 @@
+include_defs('//bucklets/gerrit_plugin.bucklet')
+
+gerrit_plugin(
+  name = 'login-redirect',
+  srcs = glob(['src/main/java/**/*.java']),
+  resources = glob(['src/main/resources/**/*']),
+  manifest_entries = [
+    'Gerrit-PluginName: login-redirect',
+    'Gerrit-HttpModule: com.googlesource.gerrit.plugins.loginredirect.LoginRedirectModule',
+    'Implementation-Title: Login Redirect plug-in',
+  ]
+)
+
+java_library(
+  name = 'classpath',
+  deps = GERRIT_PLUGIN_API + [':login-redirect__plugin'],
+)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/loginredirect/LoginRedirectFilter.java b/src/main/java/com/googlesource/gerrit/plugins/loginredirect/LoginRedirectFilter.java
new file mode 100644
index 0000000..4042cde
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/loginredirect/LoginRedirectFilter.java
@@ -0,0 +1,75 @@
+// 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.googlesource.gerrit.plugins.loginredirect;
+
+import com.google.gerrit.extensions.registration.DynamicItem;
+import com.google.gerrit.extensions.restapi.Url;
+import com.google.gerrit.httpd.AllRequestFilter;
+import com.google.gerrit.httpd.WebSession;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@Singleton
+public class LoginRedirectFilter extends AllRequestFilter {
+  private static final Logger log =
+      LoggerFactory.getLogger(LoginRedirectFilter.class);
+
+  @Inject
+  private DynamicItem<WebSession> sessionProvider;
+
+  @Override
+  public void doFilter(ServletRequest request, ServletResponse response,
+      FilterChain chain) throws IOException, ServletException {
+    HttpServletRequest httpReq = (HttpServletRequest)request;
+
+    String path = httpReq.getRequestURI();
+    if (!httpReq.getContextPath().isEmpty()) {
+      path = path.substring(httpReq.getContextPath().length());
+    }
+    if (path.startsWith("/login") ||
+        path.startsWith("/a/") ||
+        sessionProvider.get().isSignedIn()) {
+      chain.doFilter(request, response);
+    } else {
+      ((HttpServletResponse) response).sendRedirect(getLoginRedirectUrl(httpReq));
+    }
+  }
+
+  private static String getLoginRedirectUrl(HttpServletRequest req) {
+    String contextPath = req.getContextPath();
+    String loginUrl = contextPath + "/login/";
+    String token = req.getRequestURI();
+    if (!contextPath.isEmpty()) {
+      token = token.substring(contextPath.length());
+    }
+
+    String queryString = req.getQueryString();
+    if (queryString != null && !queryString.isEmpty()) {
+      token = token.concat("?" + queryString);
+    }
+    return (loginUrl + Url.encode(token));
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/loginredirect/LoginRedirectModule.java b/src/main/java/com/googlesource/gerrit/plugins/loginredirect/LoginRedirectModule.java
new file mode 100644
index 0000000..387ba01
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/loginredirect/LoginRedirectModule.java
@@ -0,0 +1,29 @@
+// 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.googlesource.gerrit.plugins.loginredirect;
+
+import com.google.gerrit.extensions.registration.DynamicSet;
+import com.google.gerrit.httpd.AllRequestFilter;
+import com.google.gerrit.httpd.plugins.HttpPluginModule;
+import com.google.inject.Scopes;
+
+
+public class LoginRedirectModule extends HttpPluginModule {
+  @Override
+  protected void configureServlets() {
+    DynamicSet.bind(binder(), AllRequestFilter.class)
+        .to(LoginRedirectFilter.class)
+        .in(Scopes.SINGLETON);
+  }
+}
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
new file mode 100644
index 0000000..f63cabe
--- /dev/null
+++ b/src/main/resources/Documentation/about.md
@@ -0,0 +1 @@
+Redirect anonymous users to login form.
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
new file mode 100644
index 0000000..1086a18
--- /dev/null
+++ b/src/main/resources/Documentation/build.md
@@ -0,0 +1,79 @@
+Build
+=====
+
+This @PLUGIN@ plugin is built with Buck.
+
+Two build modes are supported: Standalone and in Gerrit tree.
+The standalone build mode is recommended, as this mode doesn't require
+the Gerrit tree to exist locally.
+
+#### Build standalone
+
+Clone bucklets library:
+
+```
+  git clone https://gerrit.googlesource.com/bucklets
+
+```
+and link it to @PLUGIN@ plugin directory:
+
+```
+  cd @PLUGIN@ && ln -s ../bucklets .
+```
+
+Add link to the .buckversion file:
+
+```
+  cd @PLUGIN@ && ln -s bucklets/buckversion .buckversion
+```
+
+Add link to the .watchmanconfig file:
+
+```
+  cd @PLUGIN@ && ln -s bucklets/watchmanconfig .watchmanconfig
+```
+
+To build the plugin, issue the following command:
+
+```
+  buck build plugin
+```
+
+The output is created in
+
+```
+  buck-out/gen/@PLUGIN@.jar
+```
+
+This project can be imported into the Eclipse IDE:
+
+```
+  ./bucklets/tools/eclipse.py
+```
+
+#### Build in Gerrit tree
+
+Clone or link this plugin to the plugins directory of Gerrit's source
+tree, and issue the command:
+
+```
+  buck build plugins/@PLUGIN@
+```
+
+in the root of Gerrit's source tree to build
+
+The output is created in
+
+```
+  buck-out/gen/plugins/@PLUGIN@/@PLUGIN@.jar
+```
+
+This project can be imported into the Eclipse IDE:
+
+```
+  ./tools/eclipse/project.py
+```
+
+[Back to @PLUGIN@ documentation index][index]
+
+[index]: index.html