if(isLoggedIn && ftpClient != null) {
if(logtext != null) if(logtext != null) logtext.append("..ftp is connected");
return;
}
SOSFTP sosftp = null;
Properties prop = (Properties)getProfiles().get(profile);
String protocol = "ftp";
String host = "";
int port = 22;
isLoggedIn = false;
String user = "";
String password = "";
/** The FTP server will always reply the ftp error codes,
* see http://www.the-eggman.com/seminars/ftp_error_codes.html */
String account= "";
String authenticationFilename = "";
String authenticationMethod = "publickey";
String proxyHost = "";
String proxyPassword = "";
String proxyUser= "";
//boolean passiveMode = false;
String transferMode = "binary";
int proxyPort = 21;
try {
protocol = sosString.parseToString(prop.get("protocol")).length() > 0 ? sosString.parseToString(prop.get("protocol")) : "ftp";
host = sosString.parseToString(prop.get("host"));
user = sosString.parseToString(prop.get("user"));
authenticationMethod = sosString.parseToString(prop.get("auth_method")).length() > 0 ? sosString.parseToString(prop.get("auth_method")) : "publickey";
String sPort = sosString.parseToString(prop.get("port"));
if(sPort.length() > 0)
port = Integer.parseInt(sosString.parseToString(prop.get("port")));
if(authenticationMethod.length() > 0 && authenticationMethod.equals("both"))
authenticationMethod = "publickey";
authenticationFilename = sosString.parseToString(prop.get("auth_file")).length() > 0 ? sosString.parseToString(prop.get("auth_file")) : "";
if (host == null || host.length() == 0) throw new Exception("no host was specified");
if (user == null || user.length() == 0) throw new Exception("no user was specified");
password = sosString.parseToString(prop.get("password"));
try {
String key = Options.getProperty("profile.timestamp." + profile);
//Options.setProperty("profile.timestamp." + profilename, pass);
//Options.saveProperties();
if(key != null && key.length() > 8) {
key = key.substring(key.length()-8);
}
if(password.length() > 0 && sosString.parseToString(key).length() > 0) {
password = SOSCrypt.decrypt(key, password);
}
} catch(Exception e) {
new ErrorLog("error in " + sos.util.SOSClassUtil.getMethodName() + " ; ..could not encrypt.", e);
throw e;
}
if(tryAgain) {
authenticationMethod = "password";
authenticationFilename = "";
}
if(password.length() == 0 && !protocol.equalsIgnoreCase("sftp") ||
(tryAgain && sosString.parseToString(password).length() == 0)) {
Shell shell = new Shell();
shell.pack();
Dialog dialog = new Dialog(shell);
dialog.setText("Password");
dialog.open(this);
while (!shell.isDisposed()) {
if (!shell.getDisplay().readAndDispatch())
shell.getDisplay().sleep();
}
//shell.getDisplay().dispose();
//shell.dispose();
password = getPassword();
}
if(prop.get("transfermode") != null)
transferMode = sosString.parseToString(prop.get("transfermode"));
if (protocol.equalsIgnoreCase("ftp")){
//SOSFTP sosftp = new SOSFTP(host, port);
sosftp = new SOSFTP(host);
ftpClient = sosftp;
if(logtext != null) logtext.append("..ftp server reply [init] [host=" + host + "], [port="+ port + "]: " + ftpClient.getReplyString() );
if (account != null && account.length() > 0) {
isLoggedIn = sosftp.login(user, password, account);
if(logtext != null) logtext.append("..ftp server reply [login] [user=" + user + "], [account=" + account + "]: " + ftpClient.getReplyString() );
} else {
isLoggedIn = sosftp.login(user, password);
if(logtext != null) logtext.append("..ftp server reply [login] [user=" + user + "]: " + ftpClient.getReplyString());
}
if (!isLoggedIn || sosftp.getReplyCode() > ERROR_CODE) {
throw new Exception("..ftp server reply [login failed] [user=" + user + "], [account=" + account + "]: " + ftpClient.getReplyString() );
}
} else if (protocol.equalsIgnoreCase("sftp")) {
try {
Class sftpClass;
try{
//sftpClass = Class.forName("sos.stacks.ganymed.SOSSFTP");
sftpClass = Class.forName("sos.net.SOSSFTP");
Constructor con = sftpClass.getConstructor(new Class[]{String.class,int.class});
ftpClient = (SOSFileTransfer) con.newInstance(new Object[]{host, new Integer(port)});
} catch (Exception e){
//if(logtext != null) logtext.append("Failed to initialize SOSSFTP class, need recent sos.stacks.jar and trilead jar. "+e);
//throw new Exception("Failed to initialize SOSSFTP class, need recent sos.stacks.jar and trilead jar. "+e,e);
if(logtext != null) logtext.append("Failed to initialize SOSSFTP class, need recent sos.net.jar and trilead jar. "+e);
throw new Exception("Failed to initialize SOSSFTP class, need recent sos.net.jar and trilead jar. "+e,e);
}
Class[] stringParam = new Class[]{String.class};
Method method= sftpClass.getMethod("setAuthenticationFilename", stringParam);
method.invoke(ftpClient, new Object[]{authenticationFilename});
method= sftpClass.getMethod("setAuthenticationMethod", stringParam);
method.invoke(ftpClient, new Object[]{authenticationMethod});
method= sftpClass.getMethod("setPassword", stringParam);
method.invoke(ftpClient, new Object[]{password});
method= sftpClass.getMethod("setProxyHost", stringParam);
method.invoke(ftpClient, new Object[]{proxyHost});
method= sftpClass.getMethod("setProxyPassword", stringParam);
method.invoke(ftpClient, new Object[]{proxyPassword});
method= sftpClass.getMethod("setProxyPort", new Class[]{int.class});
method.invoke(ftpClient, new Object[]{new Integer(proxyPort)});
method= sftpClass.getMethod("setProxyUser", stringParam);
method.invoke(ftpClient, new Object[]{proxyUser});
method= sftpClass.getMethod("setUser", stringParam);
method.invoke(ftpClient, new Object[]{user});
method= sftpClass.getMethod("connect", new Class[]{});
method.invoke(ftpClient, new Object[]{});
isLoggedIn = true;
//try{
if(logtext != null) logtext.append("..sftp server logged in [user=" + user + "], [host=" + host + "]" );
//}catch (Exception e){
// throw new Exception("..sftp server login failed [user=" + user + "], [host=" + host + "]: " + e );
//}
//System.out.println("sftp hat geklappt?" + ftpClient.isConnected());
} catch(Exception e1){
//System.out.println("sftp hat nicht geklappt?, weil: " + e1.toString());
if(sosString.parseToString(prop.get("auth_method")).equalsIgnoreCase("both") && tryAgain == false)
tryAgain = true;
else
tryAgain = false;
throw new Exception("..sftp server login failed [user=" + user + "], [host=" + host + "]: " + e1, e1 );
}
} else if (protocol.equalsIgnoreCase("ftps")){
try{
if ( proxyHost != null && proxyPort != 0) {
System.getProperties().setProperty("proxyHost", proxyHost);
System.getProperties().setProperty("proxyPort", String.valueOf(proxyPort) );
System.getProperties().setProperty("proxySet", "true");
}
SOSFTPS sosftps = new SOSFTPS(host, port);
ftpClient = sosftps;
if(logtext != null) logtext.append("..ftp server reply [init] [host=" + host + "], [port="+ port + "]: " + ftpClient.getReplyString() );
isLoggedIn = sosftps.login(user, password);
if(logtext != null) logtext.append("..ftp server reply [login] [user=" + user + "]: " + ftpClient.getReplyString());
if (!isLoggedIn || sosftps.getReplyCode() > ERROR_CODE) {
throw new Exception("..ftp server reply [login failed] [user=" + user + "], [account=" + account + "]: " + ftpClient.getReplyString() );
}
isLoggedIn = true;
}catch (Exception e){
throw new Exception("..ftps server login failed [user=" + user + "], [host=" + host + "]: " + e );
}
} else{
throw new Exception("Unknown protocol: "+protocol);
}
if (ftpClient instanceof SOSFTP){
sosftp = (SOSFTP) ftpClient;
/*if (passiveMode) {
sosftp.passive();
if (sosftp.getReplyCode() > ERROR_CODE) {
throw new Exception("..ftp server reply [passive]: " + ftpClient.getReplyString());
} else {
this.getLogger().debug("..ftp server reply [passive]: " + ftpClient.getReplyString());
}
} */
if (transferMode.equalsIgnoreCase("ascii")) {
if (sosftp.ascii()) {
if(logtext != null) logtext.append("..using ASCII mode for file transfer");
if(logtext != null) logtext.append("..ftp server reply [ascii]: " + ftpClient.getReplyString());
} else {
throw new Exception(".. could not switch to ASCII mode for file transfer ..ftp server reply [ascii]: " + ftpClient.getReplyString());
}
} else {
if (sosftp.binary()) {
if(logtext != null) logtext.append("using binary mode for file transfers.");
if(logtext != null) logtext.append("..ftp server reply [binary]: " + ftpClient.getReplyString());
} else {
throw new Exception(".. could not switch to binary mode for file transfer ..ftp server reply [ascii]: " + ftpClient.getReplyString());
}