Allow any URL shaped scheme with download commands

Permit any URI that JGit recognizes as a URI for Git repositories,
allowing the standard commands to return output even for schemes
that are injected from another plugin.

Change-Id: Ie9959295b6357b500eff4134b47b36074f6b8b43
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/GitDownloadCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/GitDownloadCommand.java
index e7a3a04..a290856 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/download/command/GitDownloadCommand.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/GitDownloadCommand.java
@@ -21,10 +21,11 @@
 import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
 import com.google.gerrit.server.config.DownloadConfig;
 
-import com.googlesource.gerrit.plugins.download.scheme.AnonymousHttpScheme;
-import com.googlesource.gerrit.plugins.download.scheme.GitScheme;
-import com.googlesource.gerrit.plugins.download.scheme.HttpScheme;
-import com.googlesource.gerrit.plugins.download.scheme.SshScheme;
+import com.googlesource.gerrit.plugins.download.scheme.RepoScheme;
+
+import org.eclipse.jgit.transport.URIish;
+
+import java.net.URISyntaxException;
 
 abstract class GitDownloadCommand extends DownloadCommand {
   private final boolean commandAllowed;
@@ -40,7 +41,7 @@
       String ref) {
     if (commandAllowed && isRecognizedScheme(scheme)) {
       String url = scheme.getUrl(project);
-      if (url != null) {
+      if (url != null && isValidUrl(url)) {
         return getCommand(url, ref);
       }
     }
@@ -48,10 +49,16 @@
   }
 
   private static boolean isRecognizedScheme(DownloadScheme scheme) {
-    return scheme instanceof SshScheme
-        || scheme instanceof HttpScheme
-        || scheme instanceof AnonymousHttpScheme
-        || scheme instanceof GitScheme;
+    return !(scheme instanceof RepoScheme);
+  }
+
+  private static boolean isValidUrl(String url) {
+    try {
+      new URIish(url);
+      return true;
+    } catch (URISyntaxException e) {
+      return false;
+    }
   }
 
   abstract String getCommand(String url, String ref);