Search in sources :

Example 1 with SftpFileTransferProgressMonitor

use of com.axway.ats.core.filetransfer.model.ftp.SftpFileTransferProgressMonitor in project ats-framework by Axway.

the class SftpClient method doConnect.

private void doConnect(String publicKeyAlias) throws FileTransferException {
    // make new SFTP object for every new connection
    disconnect();
    this.jsch = new JSch();
    /* NOTE: Due to logging being global (static), if one thread enables debug mode, all threads will log messages,
         * and if another thread disables it, logging will be stopped for all threads,
         * until at least one thread enables it again
         */
    if (isDebugMode()) {
        JSch.setLogger(new SftpListener());
        debugProgressMonitor = new SftpFileTransferProgressMonitor();
    } else {
        debugProgressMonitor = null;
    }
    try {
        if (username != null) {
            this.session = jsch.getSession(this.username, this.hostname, this.port);
            this.session.setPassword(this.password);
        } else {
            this.jsch.addIdentity(this.keystoreFile, this.publicKeyAlias, this.keystorePassword.getBytes());
            this.session = this.jsch.getSession(this.username, this.hostname, this.port);
        }
        // skip checking of known hosts and verifying RSA keys
        this.session.setConfig("StrictHostKeyChecking", "no");
        // make keyboard-interactive last authentication method
        this.session.setConfig("PreferredAuthentications", "publickey,password,keyboard-interactive");
        this.session.setTimeout(this.timeout);
        if (this.ciphers != null && this.ciphers.size() > 0) {
            StringBuilder ciphers = new StringBuilder();
            for (SshCipher cipher : this.ciphers) {
                ciphers.append(cipher.getSshAlgorithmName() + ",");
            }
            this.session.setConfig("cipher.c2s", ciphers.toString());
            this.session.setConfig("cipher.s2c", ciphers.toString());
            this.session.setConfig("CheckCiphers", ciphers.toString());
        }
        if (this.listener != null) {
            this.listener.setResponses(new ArrayList<String>());
            JSch.setLogger((com.jcraft.jsch.Logger) this.listener);
        }
        /* SFTP reference implementation does not support transfer mode.
               Due to that, JSch does not support it either.
               For now setTransferMode() has no effect. It may be left like that,
               implemented to throw new FileTransferException( "Not implemented" )
               or log warning, that SFTP protocol does not support transfer mode change.
            */
        this.session.connect();
        this.channel = (ChannelSftp) this.session.openChannel("sftp");
        this.channel.connect();
    } catch (JSchException e) {
        throw new FileTransferException("Unable to connect!", e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) SshCipher(com.axway.ats.common.filetransfer.SshCipher) SftpListener(com.axway.ats.core.filetransfer.model.ftp.SftpListener) JSch(com.jcraft.jsch.JSch) SftpFileTransferProgressMonitor(com.axway.ats.core.filetransfer.model.ftp.SftpFileTransferProgressMonitor)

Aggregations

FileTransferException (com.axway.ats.common.filetransfer.FileTransferException)1 SshCipher (com.axway.ats.common.filetransfer.SshCipher)1 SftpFileTransferProgressMonitor (com.axway.ats.core.filetransfer.model.ftp.SftpFileTransferProgressMonitor)1 SftpListener (com.axway.ats.core.filetransfer.model.ftp.SftpListener)1 JSch (com.jcraft.jsch.JSch)1 JSchException (com.jcraft.jsch.JSchException)1