Search in sources :

Example 6 with SFTPException

use of jp.ossc.nimbus.service.sftp.SFTPException in project nimbus by nimbus-org.

the class SFTPClientImpl method connect.

public void connect(String user, String host, int port, File pemFile, String passphrase) throws SFTPException {
    if (jsch != null) {
        throw new SFTPException("It is already connected!");
    }
    jsch = new JSch();
    try {
        jsch.addIdentity(pemFile.getAbsolutePath(), passphrase);
        session = jsch.getSession(user, host, port);
        if (configProperties != null) {
            session.setConfig(configProperties);
        }
        if (proxy != null) {
            session.setProxy(proxy);
        }
        if (timeout >= 0) {
            session.setTimeout(timeout);
        }
        if (serverAliveInterval >= 0) {
            session.setServerAliveInterval(serverAliveInterval);
        }
        if (serverAliveCountMax >= 0) {
            session.setServerAliveCountMax(serverAliveCountMax);
        }
        session.connect();
        channel = (ChannelSftp) session.openChannel("sftp");
        if (fileNameEncoding != null) {
            channel.setFilenameEncoding(fileNameEncoding);
        }
        channel.connect();
        if (homeDir != null) {
            channel.lcd(homeDir.getPath());
        }
    } catch (JSchException e) {
        if (channel != null) {
            channel.disconnect();
            channel = null;
        }
        if (session != null) {
            session.disconnect();
            session = null;
        }
        jsch = null;
        throw new SFTPException("It failed to connect!", e);
    } catch (SftpException e) {
        if (channel != null) {
            channel.disconnect();
            channel = null;
        }
        if (session != null) {
            session.disconnect();
            session = null;
        }
        jsch = null;
        throw new SFTPException("It failed to connect!", e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) SftpException(com.jcraft.jsch.SftpException) JSch(com.jcraft.jsch.JSch) SFTPException(jp.ossc.nimbus.service.sftp.SFTPException)

Example 7 with SFTPException

use of jp.ossc.nimbus.service.sftp.SFTPException in project nimbus by nimbus-org.

the class SFTPClientImpl method mget.

public File[] mget(String remote, String localDir) throws NoSuchFileSFTPException, SFTPException {
    if (channel == null) {
        throw new SFTPException("Connection is not established!");
    }
    try {
        String[] fileNames = ls(remote);
        if (fileNames.length == 0) {
            return new File[0];
        }
        if (localDir == null) {
            localDir = lpwd().getPath();
        }
        channel.get(remote, localDir);
        File[] files = new File[fileNames.length];
        for (int i = 0; i < fileNames.length; i++) {
            files[i] = new File(localDir, fileNames[i]);
        }
        return files;
    } catch (SftpException e) {
        if (e.id == ERROR_ID_NO_SUCH_FILE) {
            throw new NoSuchFileSFTPException("It failed to mget! remote=" + remote + ", localDir=" + localDir, e);
        }
        throw new SFTPException("It failed to mget! remote=" + remote + ", localDir=" + localDir, e);
    }
}
Also used : SftpException(com.jcraft.jsch.SftpException) File(java.io.File) RecurciveSearchFile(jp.ossc.nimbus.io.RecurciveSearchFile) SFTPException(jp.ossc.nimbus.service.sftp.SFTPException)

Aggregations

SFTPException (jp.ossc.nimbus.service.sftp.SFTPException)7 SftpException (com.jcraft.jsch.SftpException)6 File (java.io.File)3 RecurciveSearchFile (jp.ossc.nimbus.io.RecurciveSearchFile)3 ChannelSftp (com.jcraft.jsch.ChannelSftp)2 JSch (com.jcraft.jsch.JSch)2 JSchException (com.jcraft.jsch.JSchException)2 List (java.util.List)2