0
|
1 /*
|
|
2 * ConnectBot: simple, powerful, open-source SSH client for Android
|
|
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey
|
|
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 */
|
|
17
|
|
18 package com.five_ten_sg.connectbot.transport;
|
|
19
|
|
20 import java.util.HashMap;
|
|
21 import java.util.Map;
|
|
22
|
|
23 import com.five_ten_sg.connectbot.bean.HostBean;
|
|
24 import com.five_ten_sg.connectbot.util.HostDatabase;
|
|
25 import android.content.Context;
|
|
26 import android.net.Uri;
|
|
27 import android.util.Log;
|
|
28
|
|
29
|
|
30 /**
|
|
31 * @author Kenny Root
|
|
32 *
|
|
33 */
|
|
34 public class TransportFactory {
|
|
35 private static final String TAG = "ConnectBot.TransportFactory";
|
|
36
|
|
37 private static String[] transportNames = {
|
|
38 SSH.getProtocolName(),
|
11
|
39 TN5250.getProtocolName(),
|
0
|
40 Telnet.getProtocolName(),
|
|
41 Local.getProtocolName(),
|
|
42 };
|
|
43
|
|
44 /**
|
|
45 * @param protocol
|
|
46 * @return
|
|
47 */
|
|
48 public static AbsTransport getTransport(String protocol) {
|
|
49 if (SSH.getProtocolName().equals(protocol)) {
|
|
50 return new SSH();
|
|
51 }
|
11
|
52 else if (TN5250.getProtocolName().equals(protocol)) {
|
|
53 return new TN5250();
|
|
54 }
|
0
|
55 else if (Telnet.getProtocolName().equals(protocol)) {
|
|
56 return new Telnet();
|
|
57 }
|
|
58 else if (Local.getProtocolName().equals(protocol)) {
|
|
59 return new Local();
|
|
60 }
|
|
61 else {
|
|
62 return null;
|
|
63 }
|
|
64 }
|
|
65
|
13
|
66 public static Uri getUri(String protocol, String input) {
|
0
|
67 Log.d("TransportFactory", String.format(
|
13
|
68 "Attempting to discover URI for protocol=%s on input=%s",
|
|
69 protocol, input));
|
11
|
70 AbsTransport t = getTransport(protocol);
|
|
71 if (t == null) return null;
|
|
72 return t.getUri(input);
|
0
|
73 }
|
|
74
|
|
75 public static String[] getTransportNames() {
|
|
76 return transportNames;
|
|
77 }
|
|
78
|
|
79 public static boolean isSameTransportType(AbsTransport a, AbsTransport b) {
|
|
80 if (a == null || b == null)
|
|
81 return false;
|
|
82
|
|
83 return a.getClass().equals(b.getClass());
|
|
84 }
|
|
85
|
|
86 public static boolean canForwardPorts(String protocol) {
|
11
|
87 AbsTransport t = getTransport(protocol);
|
|
88 if (t == null) return false;
|
|
89 return t.canForwardPorts();
|
0
|
90 }
|
|
91
|
|
92 /**
|
|
93 * @param protocol text name of protocol
|
|
94 * @param context
|
|
95 * @return expanded format hint
|
|
96 */
|
|
97 public static String getFormatHint(String protocol, Context context) {
|
11
|
98 AbsTransport t = getTransport(protocol);
|
|
99 if (t == null) return "???";
|
|
100 return t.getFormatHint(context);
|
0
|
101 }
|
|
102
|
|
103 /**
|
|
104 * @param hostdb Handle to HostDatabase
|
|
105 * @param uri URI to target server
|
|
106 * @return HostBean or null
|
|
107 */
|
|
108 public static HostBean findHost(HostDatabase hostdb, Uri uri) {
|
|
109 AbsTransport transport = getTransport(uri.getScheme());
|
|
110 Map<String, String> selection = new HashMap<String, String>();
|
|
111 transport.getSelectionArgs(uri, selection);
|
|
112
|
|
113 if (selection.size() == 0) {
|
|
114 Log.e(TAG, String.format("Transport %s failed to do something useful with URI=%s",
|
|
115 uri.getScheme(), uri.toString()));
|
|
116 throw new IllegalStateException("Failed to get needed selection arguments");
|
|
117 }
|
|
118
|
|
119 return hostdb.findHost(selection);
|
|
120 }
|
|
121 }
|