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(),
|
|
39 Telnet.getProtocolName(),
|
|
40 Local.getProtocolName(),
|
|
41 };
|
|
42
|
|
43 /**
|
|
44 * @param protocol
|
|
45 * @return
|
|
46 */
|
|
47 public static AbsTransport getTransport(String protocol) {
|
|
48 if (SSH.getProtocolName().equals(protocol)) {
|
|
49 return new SSH();
|
|
50 }
|
|
51 else if (Telnet.getProtocolName().equals(protocol)) {
|
|
52 return new Telnet();
|
|
53 }
|
|
54 else if (Local.getProtocolName().equals(protocol)) {
|
|
55 return new Local();
|
|
56 }
|
|
57 else {
|
|
58 return null;
|
|
59 }
|
|
60 }
|
|
61
|
|
62 public static Uri getUri(String scheme, String input) {
|
|
63 Log.d("TransportFactory", String.format(
|
|
64 "Attempting to discover URI for scheme=%s on input=%s", scheme,
|
|
65 input));
|
|
66
|
|
67 if (SSH.getProtocolName().equals(scheme))
|
|
68 return SSH.getUri(input);
|
|
69 else if (Telnet.getProtocolName().equals(scheme))
|
|
70 return Telnet.getUri(input);
|
|
71 else if (Local.getProtocolName().equals(scheme)) {
|
|
72 Log.d("TransportFactory", "Got to the local parsing area");
|
|
73 return Local.getUri(input);
|
|
74 }
|
|
75 else
|
|
76 return null;
|
|
77 }
|
|
78
|
|
79 public static String[] getTransportNames() {
|
|
80 return transportNames;
|
|
81 }
|
|
82
|
|
83 public static boolean isSameTransportType(AbsTransport a, AbsTransport b) {
|
|
84 if (a == null || b == null)
|
|
85 return false;
|
|
86
|
|
87 return a.getClass().equals(b.getClass());
|
|
88 }
|
|
89
|
|
90 public static boolean canForwardPorts(String protocol) {
|
|
91 // TODO uh, make this have less knowledge about its children
|
|
92 if (SSH.getProtocolName().equals(protocol)) {
|
|
93 return true;
|
|
94 }
|
|
95 else {
|
|
96 return false;
|
|
97 }
|
|
98 }
|
|
99
|
|
100 /**
|
|
101 * @param protocol text name of protocol
|
|
102 * @param context
|
|
103 * @return expanded format hint
|
|
104 */
|
|
105 public static String getFormatHint(String protocol, Context context) {
|
|
106 if (SSH.getProtocolName().equals(protocol)) {
|
|
107 return SSH.getFormatHint(context);
|
|
108 }
|
|
109 else if (Telnet.getProtocolName().equals(protocol)) {
|
|
110 return Telnet.getFormatHint(context);
|
|
111 }
|
|
112 else if (Local.getProtocolName().equals(protocol)) {
|
|
113 return Local.getFormatHint(context);
|
|
114 }
|
|
115 else {
|
|
116 return AbsTransport.getFormatHint(context);
|
|
117 }
|
|
118 }
|
|
119
|
|
120 /**
|
|
121 * @param hostdb Handle to HostDatabase
|
|
122 * @param uri URI to target server
|
|
123 * @return HostBean or null
|
|
124 */
|
|
125 public static HostBean findHost(HostDatabase hostdb, Uri uri) {
|
|
126 AbsTransport transport = getTransport(uri.getScheme());
|
|
127 Map<String, String> selection = new HashMap<String, String>();
|
|
128 transport.getSelectionArgs(uri, selection);
|
|
129
|
|
130 if (selection.size() == 0) {
|
|
131 Log.e(TAG, String.format("Transport %s failed to do something useful with URI=%s",
|
|
132 uri.getScheme(), uri.toString()));
|
|
133 throw new IllegalStateException("Failed to get needed selection arguments");
|
|
134 }
|
|
135
|
|
136 return hostdb.findHost(selection);
|
|
137 }
|
|
138 }
|