summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/google/gerrit/server/ssh/GerritCommandFactory.java
blob: 2c72c46c4b5d4adcec905573708bc07e2db7363b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2008 Google Inc.
//
// 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.google.gerrit.server.ssh;

import org.apache.sshd.server.CommandFactory;

import java.util.HashMap;

/** Creates a command implementation based on the client input. */
class GerritCommandFactory implements CommandFactory {
  private final HashMap<String, Factory> commands;

  GerritCommandFactory() {
    commands = new HashMap<String, Factory>();

    commands.put("gerrit-upload-pack", new Factory() {
      public AbstractCommand create() {
        return new Upload();
      }
    });
    commands.put("gerrit-receive-pack", new Factory() {
      public AbstractCommand create() {
        return new Receive();
      }
    });
    commands.put("gerrit-flush-caches", new Factory() {
      public AbstractCommand create() {
        return new AdminFlushCaches();
      }
    });
    commands.put("gerrit-ls-projects", new Factory() {
      public AbstractCommand create() {
        return new ListProjects();
      }
    });

    alias("gerrit-upload-pack", "git-upload-pack");
    alias("gerrit-receive-pack", "git-receive-pack");
  }

  private void alias(final String from, final String to) {
    commands.put(to, commands.get(from));
  }

  public Command createCommand(final String commandLine) {
    final int sp1 = commandLine.indexOf(' ');
    String cmd, args;
    if (0 < sp1) {
      cmd = commandLine.substring(0, sp1);
      args = commandLine.substring(sp1 + 1);
    } else {
      cmd = commandLine;
      args = "";
    }

    // Support newer-style "git receive-pack" requests by converting
    // to the older-style "git-receive-pack".
    //
    if ("git".equals(cmd) || "gerrit".equals(cmd)) {
      cmd += "-";
      final int sp2 = args.indexOf(' ');
      if (0 < sp2) {
        cmd += args.substring(0, sp2);
        args = args.substring(sp2 + 1);
      } else {
        cmd += args;
        args = "";
      }
    }

    final AbstractCommand c = create(cmd);
    c.parseArguments(cmd, args);
    return c;
  }

  private AbstractCommand create(final String cmd) {
    final Factory f = commands.get(cmd);
    if (f != null) {
      return f.create();
    }
    return new AbstractCommand() {
      @Override
      protected void run(final String[] argv) throws Failure {
        throw new Failure(127, "gerrit: " + getName() + ": not found");
      }
    };
  }

  protected static interface Factory {
    AbstractCommand create();
  }
}