// Copyright (C) 2013 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.download.command;

import static com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadCommand.DEFAULT_DOWNLOADS;

import com.google.gerrit.extensions.config.DownloadCommand;
import com.google.gerrit.extensions.config.DownloadScheme;
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;

abstract class GitDownloadCommand extends DownloadCommand {
  private final boolean commandAllowed;

  GitDownloadCommand(
      DownloadConfig downloadConfig, AccountGeneralPreferences.DownloadCommand cmd) {
    this.commandAllowed = downloadConfig.getDownloadCommands().contains(cmd)
        || downloadConfig.getDownloadCommands().contains(DEFAULT_DOWNLOADS);
  }

  @Override
  public final String getCommand(DownloadScheme scheme, String project,
      String ref) {
    if (commandAllowed && isRecognizedScheme(scheme)) {
      String url = scheme.getUrl(project);
      if (url != null) {
        return getCommand(url, ref);
      }
    }
    return null;
  }

  private static boolean isRecognizedScheme(DownloadScheme scheme) {
    return scheme instanceof SshScheme
        || scheme instanceof HttpScheme
        || scheme instanceof AnonymousHttpScheme
        || scheme instanceof GitScheme;
  }

  abstract String getCommand(String url, String ref);
}

@@ -1073,29 +1073,20 @@
 eme.
-AnonymousHttp
+Repo
 Scheme;%0A
 impo
@@ -1085,205 +1085,86 @@
 me;%0A
+%0A
 import 
-com.googlesource.gerrit.plugins.download.scheme.GitScheme;%0Aimport com.googlesource.gerrit.plugins.download.scheme.HttpScheme;%0Aimport com.googlesource.gerrit.plugins.download.scheme.SshScheme
+org.eclipse.jgit.transport.URIish;%0A%0Aimport java.net.URISyntaxException
 ;%0A%0Aa
@@ -1754,20 +1754,39 @@
  != null
+ && isValidUrl(url)
 ) %7B%0A
-
         
@@ -1930,16 +1930,18 @@
  return 
+!(
 scheme i
@@ -1954,146 +1954,190 @@
 eof 
-Ssh
+Repo
 Scheme
+);
 %0A  
-      %7C%7C scheme instanceof HttpScheme%0A        %7C%7C scheme instanceof AnonymousHttpScheme%0A        %7C%7C scheme instanceof GitScheme;
+%7D%0A%0A  private static boolean isValidUrl(String url) %7B%0A    try %7B%0A      new URIish(url);%0A      return true;%0A    %7D catch (URISyntaxException e) %7B%0A      return false;%0A    %7D
 %0A  %7D

Allow any URL shaped scheme with download commands