commit | author | age
|
56b3f3
|
1 |
/* |
JM |
2 |
* Copyright (C) 2012 The Android Open Source Project |
|
3 |
* Copyright 2014 gitblit.com. |
|
4 |
* |
|
5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 |
* you may not use this file except in compliance with the License. |
|
7 |
* You may obtain a copy of the License at |
|
8 |
* |
|
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10 |
* |
|
11 |
* Unless required by applicable law or agreed to in writing, software |
|
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 |
* See the License for the specific language governing permissions and |
|
15 |
* limitations under the License. |
|
16 |
*/ |
7613df
|
17 |
package com.gitblit.transport.ssh.commands; |
DO |
18 |
|
|
19 |
import java.io.IOException; |
|
20 |
import java.io.PrintWriter; |
a9be3d
|
21 |
import java.net.MalformedURLException; |
JM |
22 |
import java.net.URL; |
|
23 |
import java.text.MessageFormat; |
7613df
|
24 |
|
DO |
25 |
import org.apache.sshd.server.Environment; |
a9be3d
|
26 |
import org.eclipse.jgit.util.SystemReader; |
617909
|
27 |
import org.slf4j.Logger; |
JM |
28 |
import org.slf4j.LoggerFactory; |
a9be3d
|
29 |
|
b3aabb
|
30 |
import com.gitblit.IStoredSettings; |
a9be3d
|
31 |
import com.gitblit.Keys; |
JM |
32 |
import com.gitblit.manager.IGitblit; |
|
33 |
import com.gitblit.utils.StringUtils; |
7613df
|
34 |
|
991857
|
35 |
public abstract class SshCommand extends BaseCommand { |
617909
|
36 |
|
JM |
37 |
protected Logger log = LoggerFactory.getLogger(getClass()); |
503a85
|
38 |
protected PrintWriter stdout; |
JM |
39 |
protected PrintWriter stderr; |
7613df
|
40 |
|
503a85
|
41 |
@Override |
JM |
42 |
public void start(Environment env) throws IOException { |
|
43 |
startThread(new CommandRunnable() { |
|
44 |
@Override |
|
45 |
public void run() throws Exception { |
|
46 |
parseCommandLine(); |
|
47 |
stdout = toPrintWriter(out); |
|
48 |
stderr = toPrintWriter(err); |
|
49 |
try { |
|
50 |
SshCommand.this.run(); |
|
51 |
} finally { |
|
52 |
stdout.flush(); |
|
53 |
stderr.flush(); |
|
54 |
} |
|
55 |
} |
|
56 |
}); |
|
57 |
} |
7613df
|
58 |
|
a9be3d
|
59 |
protected String getHostname() { |
JM |
60 |
IGitblit gitblit = getContext().getGitblit(); |
|
61 |
String host = null; |
|
62 |
String url = gitblit.getSettings().getString(Keys.web.canonicalUrl, "https://localhost:8443"); |
|
63 |
if (url != null) { |
|
64 |
try { |
|
65 |
host = new URL(url).getHost(); |
|
66 |
} catch (MalformedURLException e) { |
|
67 |
} |
|
68 |
} |
|
69 |
if (StringUtils.isEmpty(host)) { |
|
70 |
host = SystemReader.getInstance().getHostname(); |
|
71 |
} |
|
72 |
return host; |
|
73 |
} |
|
74 |
|
|
75 |
protected String getRepositoryUrl(String repository) { |
|
76 |
String username = getContext().getClient().getUsername(); |
182312
|
77 |
IStoredSettings settings = getContext().getGitblit().getSettings(); |
b3aabb
|
78 |
String displayHostname = settings.getString(Keys.git.sshAdvertisedHost, ""); |
182312
|
79 |
if(displayHostname.isEmpty()) { |
MB |
80 |
displayHostname = getHostname(); |
|
81 |
} |
|
82 |
int port = settings.getInteger(Keys.git.sshPort, 0); |
b3aabb
|
83 |
int displayPort = settings.getInteger(Keys.git.sshAdvertisedPort, port); |
182312
|
84 |
if (displayPort == 22) { |
a9be3d
|
85 |
// standard port |
182312
|
86 |
return MessageFormat.format("{0}@{1}/{2}.git", username, displayHostname, repository); |
a9be3d
|
87 |
} else { |
JM |
88 |
// non-standard port |
|
89 |
return MessageFormat.format("ssh://{0}@{1}:{2,number,0}/{3}", |
182312
|
90 |
username, displayHostname, displayPort, repository); |
a9be3d
|
91 |
} |
JM |
92 |
} |
|
93 |
|
fa205f
|
94 |
protected abstract void run() throws Failure, Exception; |
7613df
|
95 |
} |