Search in sources :

Example 1 with JschSftpClientException

use of com.axway.ats.core.ssh.exceptions.JschSftpClientException in project ats-framework by Axway.

the class JschSftpClient method makeRemoteDirectories.

/**
     * Create remote directories
     *
     * @param dirPath the directory path
     */
public void makeRemoteDirectories(String dirPath) {
    if (isVerbose) {
        log.debug("Create remote directory " + dirPath);
    }
    try {
        if (!isRemoteFileOrDirectoryExisting(dirPath)) {
            dirPath = IoUtils.normalizeUnixDir(dirPath);
            int dirSeparatorIndex = 0;
            while ((dirSeparatorIndex = dirPath.indexOf('/', dirSeparatorIndex + 1)) > 0) {
                String currentDirPath = dirPath.substring(0, dirSeparatorIndex);
                if (!isRemoteFileOrDirectoryExisting(currentDirPath)) {
                    channel.mkdir(currentDirPath);
                }
            }
        }
    } catch (Exception e) {
        throw new JschSftpClientException(e.getMessage(), e);
    }
}
Also used : JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException) JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException)

Example 2 with JschSftpClientException

use of com.axway.ats.core.ssh.exceptions.JschSftpClientException in project ats-framework by Axway.

the class JschSftpClient method ls.

/**
     *
     * @param directoryPath directory path
     * @return {@link List} of {@link FileEntry} objects corresponding with the target directory
     *  file and folder entries.
     */
public List<FileEntry> ls(String directoryPath) {
    try {
        List<LsEntry> entries = new ArrayList<LsEntry>();
        channel.ls(directoryPath, new LsEntrySelector(entries));
        return getFileEntries(entries, directoryPath);
    } catch (Exception e) {
        throw new JschSftpClientException(e.getMessage(), e);
    }
}
Also used : JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException) ArrayList(java.util.ArrayList) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException)

Example 3 with JschSftpClientException

use of com.axway.ats.core.ssh.exceptions.JschSftpClientException in project ats-framework by Axway.

the class JschSftpClient method connect.

/**
     * Create SFTP session connection
     *
     * @param user the user name
     * @param password the user password
     * @param host the target host
     * @param port the specific port to use
     * @param privateKey private key location. For example: ~/.ssh/id_rsa
     * @param privateKeyPassword private key passphrase (or null if it hasn't)
     */
public void connect(String user, String password, String host, int port, String privateKey, String privateKeyPassword) {
    try {
        // disconnect if needed or stay connected if the host is the same
        if (this.session != null && this.session.isConnected()) {
            if (this.host.equals(host) && this.user.equals(user) && this.port == port) {
                //already connected
                return;
            } else {
                disconnect();
            }
        }
        this.user = user;
        this.host = host;
        this.port = port;
        JSch jsch = new JSch();
        if (privateKey != null) {
            jsch.addIdentity(privateKey, privateKeyPassword);
        }
        if (port > 0) {
            this.session = jsch.getSession(user, host, port);
        } else {
            this.session = jsch.getSession(user, host);
        }
        this.session.setPassword(password);
        // skip checking of known hosts and verifying RSA keys
        this.session.setConfig("StrictHostKeyChecking", "no");
        this.session.connect(CONNECTION_TIMEOUT);
        this.channel = (ChannelSftp) this.session.openChannel("sftp");
        // there is a bug in the other method channel.connect( TIMEOUT );
        this.channel.connect();
    } catch (Exception e) {
        throw new JschSftpClientException(e.getMessage(), e);
    }
}
Also used : JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException) JSch(com.jcraft.jsch.JSch) JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException)

Example 4 with JschSftpClientException

use of com.axway.ats.core.ssh.exceptions.JschSftpClientException in project ats-framework by Axway.

the class JschSftpClient method uploadDirectory.

/**
     * Upload local directory to a the remote file system
     *
     * @param sourceDir source directory path
     * @param destDir destination directory path
     */
public void uploadDirectory(String sourceDir, String destDir, boolean createParentDirs) {
    if (isVerbose) {
        log.debug("Upload directory " + sourceDir + " to " + destDir);
    }
    destDir = IoUtils.normalizeUnixDir(destDir);
    File srcDir = new File(sourceDir);
    if (!srcDir.isDirectory()) {
        throw new JschSftpClientException("The local directory '" + sourceDir + "' is not a Directory or doesn't exist");
    }
    if (createParentDirs) {
        makeRemoteDirectories(destDir);
    } else {
        makeRemoteDirectory(destDir);
    }
    try {
        File[] files = srcDir.listFiles();
        if (files != null) {
            for (File file : files) {
                String remoteFilePath = destDir + file.getName();
                if (file.isDirectory()) {
                    uploadDirectory(file.getCanonicalPath(), remoteFilePath, false);
                } else {
                    uploadFile(file.getCanonicalPath(), remoteFilePath);
                }
            }
        }
    } catch (Exception e) {
        throw new JschSftpClientException(e.getMessage(), e);
    }
}
Also used : JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException) File(java.io.File) JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException)

Example 5 with JschSftpClientException

use of com.axway.ats.core.ssh.exceptions.JschSftpClientException in project ats-framework by Axway.

the class JschSftpClient method isDirectoryEmpty.

/**
     *
     * @param directoryPath directory path
     * @return <code>true</code> if the directory is empty
     */
public boolean isDirectoryEmpty(String directoryPath) {
    try {
        List<LsEntry> entries = new ArrayList<LsEntry>();
        channel.ls(directoryPath, new LsEntrySelector(entries));
        return entries.size() == 0;
    } catch (Exception e) {
        throw new JschSftpClientException(e.getMessage(), e);
    }
}
Also used : JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException) ArrayList(java.util.ArrayList) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException)

Aggregations

JschSftpClientException (com.axway.ats.core.ssh.exceptions.JschSftpClientException)5 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)2 ArrayList (java.util.ArrayList)2 JSch (com.jcraft.jsch.JSch)1 File (java.io.File)1