1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 package com.sshtools.common.ui;
27
28 import com.sshtools.common.configuration.SshToolsConnectionProfile;
29 import com.sshtools.common.util.PropertyUtil;
30
31 import com.sshtools.j2ssh.authentication.SshAuthenticationClient;
32 import com.sshtools.j2ssh.authentication.SshAuthenticationClientFactory;
33 import com.sshtools.j2ssh.configuration.ConfigurationLoader;
34 import com.sshtools.j2ssh.io.IOUtil;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import java.io.IOException;
40 import java.io.InputStream;
41
42 import java.net.MalformedURLException;
43 import java.net.URL;
44
45
46 /**
47 *
48 *
49 * @author $author$
50 * @version $Revision: 1.13 $
51 */
52 public abstract class SshToolsApplicationClientApplet
53 extends SshToolsApplicationApplet {
54 /** */
55 public final static String[][] CLIENT_PARAMETER_INFO = {
56 {
57 "sshapps.connection.url", "string",
58 "The URL of a connection profile to open"
59 },
60 { "sshapps.connection.host", "string", "The host to connect to" },
61 { "sshapps.connection.userName", "string", "The user to connect as" },
62 {
63 "sshapps.connection.authenticationMethod", "string",
64 "Authentication method. password,publickey etc."
65 },
66 {
67 "sshapps.connection.connectImmediately", "boolean",
68 "Connect immediately."
69 },
70 {
71 "sshapps.connection.showConnectionDialog", "boolean",
72 "Show connection dialog."
73 },
74 {
75 "sshapps.connection.disableHostKeyVerification", "boolean",
76 "Disable the host key verification dialog."
77 }
78 };
79
80 /** */
81 protected Log log = LogFactory.getLog(SshToolsApplicationClientApplet.class);
82
83 // Private instance variables
84 private String connectionProfileLocation;
85
86 /** */
87 protected String authenticationMethod;
88
89 /** */
90 protected String host;
91
92 /** */
93 protected int port;
94
95 /** */
96 protected String user;
97
98 /** */
99 protected boolean connectImmediately;
100
101 /** */
102 protected boolean showConnectionDialog;
103
104 /** */
105 protected boolean disableHostKeyVerification;
106
107 /** */
108 protected SshToolsConnectionProfile profile;
109
110 /**
111 *
112 *
113 * @throws IOException
114 */
115 public void initApplet() throws IOException {
116 super.initApplet();
117 connectionProfileLocation = getParameter("sshapps.connectionProfile.url",
118 "");
119
120 // Get the connection parameters
121 host = getParameter("sshapps.connection.host", "");
122 port = PropertyUtil.stringToInt(getParameter(
123 "sshapps.connection.port", ""), 22);
124 user = getParameter("sshapps.connection.userName",
125 ConfigurationLoader.checkAndGetProperty("user.home", ""));
126 authenticationMethod = getParameter("sshapps.connection.authenticationMethod",
127 "");
128 connectImmediately = getParameter("sshapps.connection.connectImmediately",
129 "false").equalsIgnoreCase("true");
130 showConnectionDialog = getParameter("sshapps.connection.showConnectionDialog",
131 "false").equalsIgnoreCase("true");
132 disableHostKeyVerification = getParameter("sshapps.connection.disableHostKeyVerification",
133 "false").equalsIgnoreCase("true");
134 buildProfile();
135 }
136
137 /**
138 *
139 */
140 public void startApplet() {
141 // Disable the host key verification if requested
142 if (disableHostKeyVerification) {
143 ((SshToolsApplicationClientPanel) applicationPanel).setHostHostVerification(null);
144 ((SshToolsApplicationClientPanel) applicationPanel).application.removeAdditionalOptionsTab(
145 "Hosts");
146 log.debug("Host key verification disabled");
147 } else {
148 log.debug("Host key verification enabled");
149 }
150
151 if (connectImmediately) {
152 loadingPanel.setStatus("Connecting");
153
154 if (showConnectionDialog) {
155 SshToolsConnectionProfile newProfile = ((SshToolsApplicationClientPanel) applicationPanel).newConnectionProfile(profile);
156
157 if (newProfile != null) {
158 profile = newProfile;
159 ((SshToolsApplicationClientPanel) applicationPanel).connect(profile,
160 true);
161 }
162 } else {
163 ((SshToolsApplicationClientPanel) applicationPanel).connect(profile,
164 false);
165 }
166 }
167 }
168
169 /**
170 *
171 *
172 * @throws IOException
173 */
174 protected void buildProfile() throws IOException {
175 profile = new SshToolsConnectionProfile();
176
177 // Load the connection profile if specified
178 if (!connectionProfileLocation.equals("")) {
179 log.info("Loading connection profile " + connectionProfileLocation);
180 loadingPanel.setStatus("Loading connection profile");
181
182 InputStream in = null;
183
184 try {
185 URL u = null;
186
187 try {
188 u = new URL(connectionProfileLocation);
189 } catch (MalformedURLException murle) {
190 u = new URL(getCodeBase() + "/" +
191 connectionProfileLocation);
192 }
193
194 log.info("Full URL of connection profile is " + u);
195 in = u.openStream();
196 profile.open(in);
197 } finally {
198 IOUtil.closeStream(in);
199 }
200 }
201
202 if (!host.equals("")) {
203 log.info("Building connection profile from parameters ");
204 log.debug("Setting host to " + host);
205 profile.setHost(host);
206 log.debug("Setting port to " + port);
207 profile.setPort(port);
208 log.debug("Setting username to " + user);
209 profile.setUsername(user);
210
211 if (!authenticationMethod.equals("")) {
212 try {
213 log.debug("Adding authentication method " +
214 authenticationMethod);
215
216 SshAuthenticationClient authClient = SshAuthenticationClientFactory.newInstance(authenticationMethod);
217 profile.addAuthenticationMethod(authClient);
218 } catch (Exception e) {
219 log.error("Could not add authentication method.", e);
220 }
221 }
222 }
223 }
224
225 /**
226 *
227 */
228 public void destroy() {
229 if (applicationPanel.isConnected()) {
230 ((SshToolsApplicationClientPanel) applicationPanel).closeConnection(true);
231 }
232 }
233
234 /**
235 *
236 *
237 * @return
238 */
239 public String[][] getParameterInfo() {
240 String[][] s = super.getParameterInfo();
241 String[][] p = new String[s.length + CLIENT_PARAMETER_INFO.length][];
242 System.arraycopy(s, 0, p, 0, s.length);
243 System.arraycopy(CLIENT_PARAMETER_INFO, 0, p, s.length,
244 CLIENT_PARAMETER_INFO.length);
245
246 return p;
247 }
248 }