001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.ui;
027:
028: import com.sshtools.common.configuration.SshToolsConnectionProfile;
029: import com.sshtools.common.util.PropertyUtil;
030:
031: import com.sshtools.j2ssh.authentication.SshAuthenticationClient;
032: import com.sshtools.j2ssh.authentication.SshAuthenticationClientFactory;
033: import com.sshtools.j2ssh.configuration.ConfigurationLoader;
034: import com.sshtools.j2ssh.io.IOUtil;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: import java.io.IOException;
040: import java.io.InputStream;
041:
042: import java.net.MalformedURLException;
043: import java.net.URL;
044:
045: /**
046: *
047: *
048: * @author $author$
049: * @version $Revision: 1.13 $
050: */
051: public abstract class SshToolsApplicationClientApplet extends
052: SshToolsApplicationApplet {
053: /** */
054: public final static String[][] CLIENT_PARAMETER_INFO = {
055: { "sshapps.connection.url", "string",
056: "The URL of a connection profile to open" },
057: { "sshapps.connection.host", "string",
058: "The host to connect to" },
059: { "sshapps.connection.userName", "string",
060: "The user to connect as" },
061: { "sshapps.connection.authenticationMethod", "string",
062: "Authentication method. password,publickey etc." },
063: { "sshapps.connection.connectImmediately", "boolean",
064: "Connect immediately." },
065: { "sshapps.connection.showConnectionDialog", "boolean",
066: "Show connection dialog." },
067: { "sshapps.connection.disableHostKeyVerification",
068: "boolean",
069: "Disable the host key verification dialog." } };
070:
071: /** */
072: protected Log log = LogFactory
073: .getLog(SshToolsApplicationClientApplet.class);
074:
075: // Private instance variables
076: private String connectionProfileLocation;
077:
078: /** */
079: protected String authenticationMethod;
080:
081: /** */
082: protected String host;
083:
084: /** */
085: protected int port;
086:
087: /** */
088: protected String user;
089:
090: /** */
091: protected boolean connectImmediately;
092:
093: /** */
094: protected boolean showConnectionDialog;
095:
096: /** */
097: protected boolean disableHostKeyVerification;
098:
099: /** */
100: protected SshToolsConnectionProfile profile;
101:
102: /**
103: *
104: *
105: * @throws IOException
106: */
107: public void initApplet() throws IOException {
108: super .initApplet();
109: connectionProfileLocation = getParameter(
110: "sshapps.connectionProfile.url", "");
111:
112: // Get the connection parameters
113: host = getParameter("sshapps.connection.host", "");
114: port = PropertyUtil.stringToInt(getParameter(
115: "sshapps.connection.port", ""), 22);
116: user = getParameter("sshapps.connection.userName",
117: ConfigurationLoader
118: .checkAndGetProperty("user.home", ""));
119: authenticationMethod = getParameter(
120: "sshapps.connection.authenticationMethod", "");
121: connectImmediately = getParameter(
122: "sshapps.connection.connectImmediately", "false")
123: .equalsIgnoreCase("true");
124: showConnectionDialog = getParameter(
125: "sshapps.connection.showConnectionDialog", "false")
126: .equalsIgnoreCase("true");
127: disableHostKeyVerification = getParameter(
128: "sshapps.connection.disableHostKeyVerification",
129: "false").equalsIgnoreCase("true");
130: buildProfile();
131: }
132:
133: /**
134: *
135: */
136: public void startApplet() {
137: // Disable the host key verification if requested
138: if (disableHostKeyVerification) {
139: ((SshToolsApplicationClientPanel) applicationPanel)
140: .setHostHostVerification(null);
141: ((SshToolsApplicationClientPanel) applicationPanel).application
142: .removeAdditionalOptionsTab("Hosts");
143: log.debug("Host key verification disabled");
144: } else {
145: log.debug("Host key verification enabled");
146: }
147:
148: if (connectImmediately) {
149: loadingPanel.setStatus("Connecting");
150:
151: if (showConnectionDialog) {
152: SshToolsConnectionProfile newProfile = ((SshToolsApplicationClientPanel) applicationPanel)
153: .newConnectionProfile(profile);
154:
155: if (newProfile != null) {
156: profile = newProfile;
157: ((SshToolsApplicationClientPanel) applicationPanel)
158: .connect(profile, true);
159: }
160: } else {
161: ((SshToolsApplicationClientPanel) applicationPanel)
162: .connect(profile, false);
163: }
164: }
165: }
166:
167: /**
168: *
169: *
170: * @throws IOException
171: */
172: protected void buildProfile() throws IOException {
173: profile = new SshToolsConnectionProfile();
174:
175: // Load the connection profile if specified
176: if (!connectionProfileLocation.equals("")) {
177: log.info("Loading connection profile "
178: + connectionProfileLocation);
179: loadingPanel.setStatus("Loading connection profile");
180:
181: InputStream in = null;
182:
183: try {
184: URL u = null;
185:
186: try {
187: u = new URL(connectionProfileLocation);
188: } catch (MalformedURLException murle) {
189: u = new URL(getCodeBase() + "/"
190: + connectionProfileLocation);
191: }
192:
193: log.info("Full URL of connection profile is " + u);
194: in = u.openStream();
195: profile.open(in);
196: } finally {
197: IOUtil.closeStream(in);
198: }
199: }
200:
201: if (!host.equals("")) {
202: log.info("Building connection profile from parameters ");
203: log.debug("Setting host to " + host);
204: profile.setHost(host);
205: log.debug("Setting port to " + port);
206: profile.setPort(port);
207: log.debug("Setting username to " + user);
208: profile.setUsername(user);
209:
210: if (!authenticationMethod.equals("")) {
211: try {
212: log.debug("Adding authentication method "
213: + authenticationMethod);
214:
215: SshAuthenticationClient authClient = SshAuthenticationClientFactory
216: .newInstance(authenticationMethod);
217: profile.addAuthenticationMethod(authClient);
218: } catch (Exception e) {
219: log
220: .error(
221: "Could not add authentication method.",
222: e);
223: }
224: }
225: }
226: }
227:
228: /**
229: *
230: */
231: public void destroy() {
232: if (applicationPanel.isConnected()) {
233: ((SshToolsApplicationClientPanel) applicationPanel)
234: .closeConnection(true);
235: }
236: }
237:
238: /**
239: *
240: *
241: * @return
242: */
243: public String[][] getParameterInfo() {
244: String[][] s = super .getParameterInfo();
245: String[][] p = new String[s.length
246: + CLIENT_PARAMETER_INFO.length][];
247: System.arraycopy(s, 0, p, 0, s.length);
248: System.arraycopy(CLIENT_PARAMETER_INFO, 0, p, s.length,
249: CLIENT_PARAMETER_INFO.length);
250:
251: return p;
252: }
253: }
|