David Ostrovsky
2014-02-26 a3de33e71a22268105714e01d09c1c2e28bfe2c3
Fix command dispatching

DispatchCommand is supposed to be nested:

ssh server gitblit version --verbose --format json

means that first the command that is seen by dispatching process is
"gitblit". Dispatch command look in its commands map for this command
and dispatch the rest of the command and options and arguments to this
command, version in this example.

Change-Id: I8ef8e0e369922c793ca7ad36c1a8f76b0206baa7
2 files modified
51 ■■■■■ changed files
src/main/java/com/gitblit/transport/ssh/SshDaemon.java 7 ●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java 44 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/SshDaemon.java
@@ -102,9 +102,12 @@
        sshd.setFileSystemFactory(new DisabledFilesystemFactory());
        sshd.setForwardingFilter(new NonForwardingFilter());
        DispatchCommand gitblitCmd = new DispatchCommand();
        gitblitCmd.registerCommand(CreateRepository.class);
        gitblitCmd.registerCommand(VersionCommand.class);
        DispatchCommand dispatcher = new DispatchCommand();
        dispatcher.registerCommand(CreateRepository.class);
        dispatcher.registerCommand(VersionCommand.class);
        dispatcher.registerDispatcher("gitblit", gitblitCmd);
        SshCommandFactory commandFactory = new SshCommandFactory(
                new RepositoryResolver<SshSession>(gitblit),
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
@@ -44,9 +44,17 @@
  private Set<Class<? extends Command>> commands;
  private Map<String, Class<? extends Command>> map;
  private Map<String, Command> root;
  public DispatchCommand() {
      commands = new HashSet<Class<? extends Command>>();
  }
  public void registerDispatcher(String name, Command cmd) {
      if (root == null) {
          root = Maps.newHashMap();
      }
      root.put(name, cmd);
  }
  public void registerCommand(Class<? extends Command> cmd) {
@@ -78,20 +86,7 @@
        throw new UnloggedFailure(1, msg.toString());
      }
      final Class<? extends Command> c = getMap().get(commandName);
      if (c == null) {
        String msg =
            (getName().isEmpty() ? "Gitblit" : getName()) + ": "
                + commandName + ": not found";
        throw new UnloggedFailure(1, msg);
      }
      Command cmd = null;
      try {
          cmd = c.newInstance();
      } catch (Exception e) {
          throw new UnloggedFailure(1, MessageFormat.format("Failed to instantiate {0} command", commandName));
      }
      Command cmd = getCommand();
      if (cmd instanceof BaseCommand) {
        BaseCommand bc = (BaseCommand) cmd;
        if (getName().isEmpty()) {
@@ -119,6 +114,27 @@
    }
  }
  private Command getCommand() throws UnloggedFailure {
    if (root != null && root.containsKey(commandName)) {
        return root.get(commandName);
    }
    final Class<? extends Command> c = getMap().get(commandName);
      if (c == null) {
        String msg =
            (getName().isEmpty() ? "Gitblit" : getName()) + ": "
                + commandName + ": not found";
        throw new UnloggedFailure(1, msg);
      }
      Command cmd = null;
      try {
          cmd = c.newInstance();
      } catch (Exception e) {
          throw new UnloggedFailure(1, MessageFormat.format("Failed to instantiate {0} command", commandName));
      }
    return cmd;
  }
  @Override
protected String usage() {
    final StringBuilder usage = new StringBuilder();