Define new annotation to get the plugin URL path
It is handy to inject the full plugin canonical URL excluding the
hostname, protocol and port, which could be hidden behind a reverse
proxy.
This allows to generate all links in a "reverse-proxy-friendly"
fashion throughout all plugin code.
Change-Id: I2b8f8a7c528c5d10eea39a84254ac38c8d213be5
diff --git a/src/main/java/com/googlesource/gerrit/plugins/manager/FirstWebLoginListener.java b/src/main/java/com/googlesource/gerrit/plugins/manager/FirstWebLoginListener.java
index 00f2ace..2c931b3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/manager/FirstWebLoginListener.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/manager/FirstWebLoginListener.java
@@ -14,15 +14,12 @@
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.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
@@ -34,15 +31,15 @@
public class FirstWebLoginListener implements WebLoginListener {
private final Path pluginData;
private final PluginLoader pluginLoader;
- private final String pluginUrl;
+ private final String pluginUrlPath;
@Inject
public FirstWebLoginListener(PluginLoader pluginLoader,
@PluginData Path pluginData,
- @PluginCanonicalWebUrl String pluginUrl) {
+ @PluginCanonicalWebUrlPath String pluginUrlPath) {
this.pluginData = pluginData;
this.pluginLoader = pluginLoader;
- this.pluginUrl = urlPath(pluginUrl);
+ this.pluginUrlPath = pluginUrlPath;
}
@Override
@@ -53,7 +50,7 @@
Path firstLoginFile =
pluginData.resolve("firstLogin." + user.getAccountId().get());
if (!firstLoginFile.toFile().exists()) {
- response.sendRedirect(pluginUrl + "static/intro.html");
+ response.sendRedirect(pluginUrlPath + "static/intro.html");
Files.write(firstLoginFile, new Date().toString().getBytes(),
StandardOpenOption.CREATE);
@@ -65,8 +62,4 @@
public void onLogout(IdentifiedUser user, HttpServletRequest request,
HttpServletResponse response) {
}
-
- private static String urlPath(String url) {
- return URI.create(url).getPath();
- }
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/manager/Module.java b/src/main/java/com/googlesource/gerrit/plugins/manager/Module.java
index fa27964..7d64c38 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/manager/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/manager/Module.java
@@ -28,6 +28,8 @@
@Override
protected void configure() {
+ bind(String.class).annotatedWith(PluginCanonicalWebUrlPath.class).toProvider(PluginCanonicalWebUrlPathProvider.class);
+
DynamicSet.bind(binder(), TopMenu.class).to(PluginManagerTopMenu.class);
DynamicSet.setOf(binder(), PluginsRepository.class);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/manager/PluginCanonicalWebUrlPath.java b/src/main/java/com/googlesource/gerrit/plugins/manager/PluginCanonicalWebUrlPath.java
new file mode 100644
index 0000000..3f17da8
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/manager/PluginCanonicalWebUrlPath.java
@@ -0,0 +1,41 @@
+// 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.manager;
+
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import com.google.inject.BindingAnnotation;
+
+/**
+ * Annotation applied to a String containing the plugin canonical web URL path.
+ *
+ * <p>A plugin or extension may receive this string by Guice injection to discover the canonical web
+ * URL path under which the plugin is available:
+ *
+ * <pre>
+ * {@literal @Inject}
+ * MyType(@PluginCanonicalWebUrlPath String myUrl) {
+ * ...
+ * }
+ * </pre>
+ */
+@Target({ElementType.PARAMETER, ElementType.FIELD})
+@Retention(RUNTIME)
+@BindingAnnotation
+public @interface PluginCanonicalWebUrlPath {}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/manager/PluginCanonicalWebUrlPathProvider.java b/src/main/java/com/googlesource/gerrit/plugins/manager/PluginCanonicalWebUrlPathProvider.java
new file mode 100644
index 0000000..6581f87
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/manager/PluginCanonicalWebUrlPathProvider.java
@@ -0,0 +1,37 @@
+// 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.manager;
+
+import java.net.URI;
+
+import com.google.gerrit.extensions.annotations.PluginCanonicalWebUrl;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+
+@Singleton
+public class PluginCanonicalWebUrlPathProvider implements Provider<String> {
+ private final URI pluginCanonicalUri;
+
+ @Inject
+ PluginCanonicalWebUrlPathProvider(@PluginCanonicalWebUrl String pluginCanonicalUrl) {
+ this.pluginCanonicalUri = URI.create(pluginCanonicalUrl);
+ }
+
+ @Override
+ public String get() {
+ return pluginCanonicalUri.getPath();
+ }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/manager/PluginManagerTopMenu.java b/src/main/java/com/googlesource/gerrit/plugins/manager/PluginManagerTopMenu.java
index ec25d8f..f7dfa39 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/manager/PluginManagerTopMenu.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/manager/PluginManagerTopMenu.java
@@ -14,7 +14,6 @@
package com.googlesource.gerrit.plugins.manager;
-import com.google.gerrit.extensions.annotations.PluginCanonicalWebUrl;
import com.google.gerrit.extensions.client.MenuItem;
import com.google.gerrit.extensions.webui.TopMenu;
import com.google.gerrit.server.CurrentUser;
@@ -35,7 +34,7 @@
private Provider<CurrentUser> userProvider;
@Inject
- public PluginManagerTopMenu(@PluginCanonicalWebUrl String myUrl,
+ public PluginManagerTopMenu(@PluginCanonicalWebUrlPath String myUrl,
PluginLoader loader, Provider<CurrentUser> userProvider) {
this.loader = loader;
this.userProvider = userProvider;