Package sos.net

Examples of sos.net.SOSFileTransfer


     
      if(logtext != null) if(logtext != nulllogtext.append("..ftp is connected");
      return null;
    }

    SOSFileTransfer sosftp = null;

    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= "";
   
    String       transferMode                    = "binary";

    int proxyPort = 21;
    try {
     
      protocol               = sosString.parseToString(getProtocol()).length() > 0 ?   sosString.parseToString(getProtocol()) : "ftp";
      host                   = sosString.parseToString(getHost());
      user                   = sosString.parseToString(getUser());
      authenticationMethod   = sosString.parseToString(getAuthMethod()).length() > 0 ?   sosString.parseToString(getAuthMethod()) : "publickey";
      String sPort           = sosString.parseToString(getPort());
           
      if(sPort.length() > 0)
        port = Integer.parseInt(sosString.parseToString(sPort));
     
      if(authenticationMethod.length() > 0 && authenticationMethod.equals("both"))
        authenticationMethod = "publickey";

      authenticationFilename = sosString.parseToString(getAuthFile()).length() > 0 ?   sosString.parseToString(getAuthFile()) : "";

      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(getPassword());
     
      //entschl�sseln der Password
      try {
       
        String key = PROFILE_PREFIX;
        try {
          if ( Class.forName("sos.scheduler.editor.app.Options") != null) {
            Class options = Class.forName("sos.scheduler.editor.app.Options");
            Method method = options.getMethod("getProperty", new Class[]{String.class});
            Object okey = method.invoke(ftpClient, new Object[]{"profile.timestamp." + profilename});
            if(okey != null) {
              key = okey.toString();
              if(key.length() > 8)
                key = key.substring(key.length()-8);
            }

          }
        } catch (java.lang.ClassNotFoundException c) {
          //ignore error         
        }
       
        if(password.length() > 0 && sosString.parseToString(key).length() > 0 && password.endsWith("=")) {
            password = SOSProfileCrypt.decrypt(key, password);         
        }
       
      } catch(Exception e) {             
        throw new Exception("could not decrypt password, cause: " + e.toString());
      }
     
      if(tryAgain) {
       
        authenticationMethod = "password"
        authenticationFilename = "";
      }

      //Verbindung konnte nicht hergestellt werden. Es wird nochmals versucht. Wenn
      //Password nicht angegeben wurde, dann kommt ein Dialog, indem der Password geschrieben werden kann
      if(password.length() == 0 && !protocol.equalsIgnoreCase("sftp") ||
          (tryAgain && sosString.parseToString(password).length() == 0)) {
        Shell shell = new Shell();

        shell.pack();         
        FTPPopUpDialog fTPPopUpDialog = new FTPPopUpDialog(shell);
        fTPPopUpDialog.setText("Password");
        fTPPopUpDialog.open(this);
        while (!shell.isDisposed()) {
          if (!shell.getDisplay().readAndDispatch())
            shell.getDisplay().sleep();
        }

        password = getPassword()
      }


      if(getTransfermode() != null)
        transferMode  = sosString.parseToString(getTransfermode());
      if (protocol.equalsIgnoreCase("ftp")){
        sosftp = new SOSFTP(host);
        ftpClient = sosftp;
        if(logtext != nulllogtext.append("..ftp server reply [init] [host=" + host + "], [port="+ port + "]: " + ftpClient.getReplyString() );

        if (account != null && account.length() > 0) {
          isLoggedIn = ((SOSFTP)sosftp).login(user, password, account);
          if(logtext != nulllogtext.append("..ftp server reply [login] [user=" + user + "], [account=" + account + "]: " + ftpClient.getReplyString() );
        } else {
          isLoggedIn = ((SOSFTP)sosftp).login(user, password);
          if(logtext != nulllogtext.append("..ftp server reply [login] [user=" + user + "]: " + ftpClient.getReplyString());
        }
        if (!isLoggedIn || ((SOSFTP)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.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 != nulllogtext.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;
          if(logtext != nulllogtext.append("..sftp server logged in [user=" + user + "], [host=" + host + "]" );
          sosftp = (sos.net.SOSSFTP) ftpClient;
                 
        } catch(Exception e1){   
          if(sosString.parseToString(getAuthMethod()).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 != nulllogtext.append("..ftp server reply [init] [host=" + host + "], [port="+ port + "]: " + ftpClient.getReplyString() );
          isLoggedIn = sosftps.login(user, password);
          if(logtext != nulllogtext.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;
          sosftp = (sos.net.SOSFTPS) ftpClient;
        }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 (transferMode.equalsIgnoreCase("ascii")) {
          if (((SOSFTP)sosftp).ascii()) {
            if(logtext != nulllogtext.append("..using ASCII mode for file transfer");
            if(logtext != nulllogtext.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)sosftp).binary()) {               
            if(logtext != nulllogtext.append("using binary mode for file transfers.");
            if(logtext != nulllogtext.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());
          }
        }
      }
      saveLastProfile(profilename);

     
        if(getRoot() != null &&
            getRoot().length() > 0) {
          changeDirectory(getRoot());
        }
     

    } catch (Exception ex) {
      isLoggedIn = false;
      hasError = true;
      log("..error in ftp server init with [host=" + host + "], [port="+ port + "], cause: " + FTPDialogListener.getErrorMessage(ex), SOSLogger.WARN);
      if(logtext != nulllogtext.append("..error in ftp server init with [host=" + host + "], [port="+ port + "], cause: " + FTPDialogListener.getErrorMessage(ex) + "\n");
      if(tryAgain)  {
        if(logtext != nulllogtext.append("..try connect with Authentication Method=password." + "\n");
        connect();
        tryAgain = false;
      }
      FTPProfileDialog.message( "..error in ftp server init with [host=" + host + "], [port="+ port + "], cause: " + FTPDialogListener.getErrorMessage(ex), SWT.ICON_ERROR );
      try {
        if(sosftp != null)
          sosftp.disconnect();
      } catch (Exception e) {}
    }
   
    return sosftp;
  }
View Full Code Here


      if (!appendFiles && !overwriteFiles && new File(localFile).exists()) {
        this.getLogger().info("..ftp transfer skipped for file [no overwrite]: " + localFile);
        return false;
      }

      SOSFileTransfer ftpClient = initFTPServer();

      long currentBytesSent = ftpClient.getFile(targetFile, localFile, appendFiles);
      this.getLogger().debug("..ftp server reply [getFile] [" + localFile + ", size=" + currentBytesSent + conClosingBracketWithColon + ftpClient.getReplyString());
      this.getLogger().debug("1 file received " + localFile);
      getLogger().info("receiving file: " + localFile + " " + ftpClient.size(targetFile) + " bytes");
      transferFileList.add(new File(filePath));
      filelist = new Vector<String>();
      filelist.add(filePath);
      arguments.put("successful_transfers", "1");
View Full Code Here

TOP

Related Classes of sos.net.SOSFileTransfer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.