Package sos.net

Examples of sos.net.SOSFTP


  private void downloadFile() throws Exception {
    // Die aktuelle Version per FTP herunterladen
    boolean isLoggedIn = false;

    SOSFTP ftpClient = null;
    if (ftp_port == -1) {
      
      ftpClient = new SOSFTP(host);
    } else {
      ftpClient = new SOSFTP(host, ftp_port);
    }
   
    if (!ftp_proxy.equals("")) {
        System.getProperties().put( "socksProxyPort", ftp_proxy_port);
          System.getProperties().put( "socksProxyHost" ,ftp_proxy);
    }

    try {
      spooler_log_info_and_state("connected to host " + host + ", port " + ftp_port);
      spooler_job.set_state_text("connected to host " + host + ", port " + ftp_port);
      isLoggedIn = ftpClient.login(user, password);

      if (passiveMode)
        ftpClient.passive();
      if (transferMode.equalsIgnoreCase("ascii")) {
        ftpClient.ascii();
      } else {
        ftpClient.binary();
      }

      if (!remoteDir.equals(""))
        ftpClient.cd(remoteDir);
      ftpClient.cwd(localDir);
      spooler_log_info("get local directory: " + localDir
          + ", remote directory: " + remoteDir + " Filename:"
          + schedulerUpdateAnswer.filename);

      String fileName = schedulerUpdateAnswer.filename;
      transferFile = new File(localDir, fileName);
      spooler_log_info("The new release will be automatically downloaded: "  + schedulerUpdateAnswer.new_release);
      spooler_log_info("File will be saved to: " + transferFile.getAbsolutePath());
      spooler_log_info_and_state("receiving file: " + transferFile.getAbsolutePath() + " with " + ftpClient.size(transferFile.getName()) + " bytes");
     
      ftpClient.getFile(transferFile.getName(), transferFile.getAbsolutePath());

      spooler_log_info_and_state("One file received:" + transferFile.getAbsolutePath() + " with " + ftpClient.size(transferFile.getName()) + " bytes");
        
    } catch (Exception e) {
      transferFile = null;
      spooler_log_warn("could not process file transfer: " + e.getMessage());
    } finally {
      if (ftpClient != null) {
        if (isLoggedIn)
          try {
            ftpClient.logout();
          } catch (Exception e) {} // no error handling
        if (ftpClient.isConnected())
          try {
            ftpClient.disconnect();
          } catch (Exception e) {} // no error handling
      }
    }

  }
View Full Code Here



      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);
View Full Code Here

    if(isLoggedIn && ftpClient != null) {
      if(logtext != null) if(logtext != nulllogtext.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 != nulllogtext.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 != nulllogtext.append("..ftp server reply [login] [user=" + user + "], [account=" + account + "]: " + ftpClient.getReplyString() );           
        } else {

          isLoggedIn = sosftp.login(user, password);
          if(logtext != nulllogtext.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 != 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;
          //try{
          if(logtext != nulllogtext.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 != 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;
        }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 != 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.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());
          }
View Full Code Here

          }
        }

        if (ftpClient instanceof SOSFTP) {
          SOSFTP sosftp = (SOSFTP) ftpClient;
          if (passiveMode) {
            sosftp.passive();
            if (sosftp.getReplyCode() > ERROR_CODE) {
              throw new RuntimeException("..ftp server reply [passive]: " + ftpClient.getReplyString());
            }
            else {
              this.getLogger().debug("..ftp server reply [passive]: " + ftpClient.getReplyString());
            }
            /**
             * this call for "enterLocalPassiveMode" seems to be doubled, because the sosftp.passive()
             * is the same functionality.
             * Unfortunately the "enterLocalPassiveMode" did not reset the ReplyString, therefore
             * a call to this method will result into an error, if the previous command returns a > 500 response.
             *
             */
            sosftp.enterLocalPassiveMode();
            /**
             * JIRA SOSFTP-93
             * this method is not changing the Replystring. Therefore we don't have to check the response.
             */
            // if (sosftp.getReplyCode() > ERROR_CODE) {
            // throw new RuntimeException("..ftp server reply [enterLocalPassiveMode]: " + ftpClient.getReplyString());
            // }
            // else {
            // this.getLogger().debug("..ftp server reply [enterLocalPassiveMode]: " + ftpClient.getReplyString());
            // }

          }

          if (transferMode.equalsIgnoreCase("ascii")) {
            if (sosftp.ascii()) {
              this.getLogger().debug("..using ASCII mode for file transfer");
              this.getLogger().debug("..ftp server reply" + " [ascii]: " + ftpClient.getReplyString());
            }
            else {
              throw new RuntimeException(".. could not switch to ASCII mode for file transfer ..ftp server reply [ascii]: "
                  + ftpClient.getReplyString());
            }
          }
          else {
            if (sosftp.binary()) {
              this.getLogger().debug("using binary mode for file transfers.");
              this.getLogger().debug("..ftp server reply" + " [binary]: " + ftpClient.getReplyString());
            }
            else {
              throw new RuntimeException(".. could not switch to binary mode for file transfer ..ftp server reply [ascii]: "
                  + ftpClient.getReplyString());
            }
          }
          if (strPreFtpCommands.length() > 0) {
            String[] strA = strPreFtpCommands.split(";");
            for (String strCmd : strA) {
              this.getLogger().debug("..try to send [" + strCmd + "] to the Server ");
              sosftp.sendCommand(strCmd);
              this.getLogger().debug(FTP_SERVER_REPLY + strCmd + conClosingBracketWithColon + ftpClient.getReplyString());
            }
          }

          if (strControlEncoding.length() > 0) {
            sosftp.setControlEncoding(strControlEncoding);
            this.getLogger().debug(FTP_SERVER_REPLY + strControlEncoding + conClosingBracketWithColon + ftpClient.getReplyString());
          }
        }

        if (isFilePath) {
View Full Code Here

        else {
          this.getLogger().debug1("reusing connection from previous transfer");
        }

        if (ftpClient instanceof SOSFTP) {
          SOSFTP sosftp = (SOSFTP) ftpClient;

          if (passiveMode) {
            sosftp.enterLocalPassiveMode();
            /**
             * JIRA SOSFTP-93
             * this method is not changing the Replystring. Therefore we don't have to check the response.
             */

//            if (sosftp.getReplyCode() > ERROR_CODE) {
//              throw new Exception("..ftp server reply [enterLocalPassiveMode]: " + ftpClient.getReplyString());
//            }
//            else {
//              this.getLogger().debug("..ftp server reply [enterLocalPassiveMode]: " + ftpClient.getReplyString());
//            }
            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()) {
              this.getLogger().debug("..using ASCII mode for file transfer");
              this.getLogger().debug("..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()) {
              this.getLogger().debug("using binary mode for file transfer");
              this.getLogger().debug("..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());

            }
          }
          if (strPreFtpCommands.length() > 0) {
            String[] strA = strPreFtpCommands.split(";");
            for (String strCmd : strA) {
              sosftp.sendCommand(strCmd);
              this.getLogger().debug("..ftp server reply [" + strCmd + "]: " + ftpClient.getReplyString());
            }
          }
         
          if (strControlEncoding.length() > 0) {
            sosftp.setControlEncoding(strControlEncoding);
            this.getLogger().debug("..ftp server reply [" + strControlEncoding + "]: " + ftpClient.getReplyString());
          }
        }

        if (simpleTransfer) {
View Full Code Here

TOP

Related Classes of sos.net.SOSFTP

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.