package com.googlesource.gerrit.plugins.download.command;

import com.google.gerrit.extensions.config.DownloadCommand;
import com.google.gerrit.extensions.config.DownloadScheme;
import com.google.gerrit.server.config.DownloadConfig;

import com.googlesource.gerrit.plugins.download.scheme.AnonymousGitScheme;
import com.googlesource.gerrit.plugins.download.scheme.AnonymousHttpScheme;
import com.googlesource.gerrit.plugins.download.scheme.HttpScheme;
import com.googlesource.gerrit.plugins.download.scheme.SshScheme;

public abstract class GitDownloadCommand extends DownloadCommand {
  private final com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadCommand cmd;
  private final boolean commandAllowed;

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

  @Override
  public String getName() {
    return upperUnderscoreToUpperHyphen(cmd.name());
  }

  /** Converts UPPER_UNDERSCORE to Upper-Hyphen */
  private static String upperUnderscoreToUpperHyphen(String upperUnderscore) {
    StringBuilder upperHyphen = new StringBuilder(upperUnderscore.length());
    String[] words = upperUnderscore.split("_");
    for (int i = 0, l = words.length; i < l; ++i) {
      if (i > 0) {
        upperHyphen.append("-");
      }
      upperHyphen.append(Character.toUpperCase(words[i].charAt(0))).append(
          words[i].substring(1).toLowerCase());

    }
    return upperHyphen.toString();
  }

  @Override
  public final String getCommand(DownloadScheme scheme, String project) {
    if (!commandAllowed) {
      return null;
    }

    if (scheme instanceof SshScheme
        || scheme instanceof HttpScheme
        || scheme instanceof AnonymousHttpScheme
        || scheme instanceof AnonymousGitScheme) {
      return getCommand(scheme.getUrl(project));
    } else {
      return null;
    }
  }

  public abstract String getCommand(String url);
}

@@ -1,8 +1,617 @@
+// Copyright (C) 2013 The Android Open Source Project%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A// http://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A%0A
 package 

Add missing copyright header