Search in sources :

Example 1 with SFTPException

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

the class SFTPClientImpl method get.

public File get(String remote, String local) throws NoSuchFileSFTPException, SFTPException {
    if (channel == null) {
        throw new SFTPException("Connection is not established!");
    }
    try {
        if (local == null) {
            local = lpwd().getPath();
        }
        channel.get(remote, local);
        File remoteFile = new File(remote);
        File localFile = new File(local);
        return localFile.isDirectory() ? new File(localFile, remoteFile.getName()) : localFile;
    } catch (SftpException e) {
        if (e.id == ERROR_ID_NO_SUCH_FILE) {
            throw new NoSuchFileSFTPException("It failed to get! remote=" + remote + ", local=" + local, e);
        }
        throw new SFTPException("It failed to get! remote=" + remote + ", local=" + local, 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)

Example 2 with SFTPException

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

the class SFTPClientImpl method mput.

public void mput(String local, String remoteDir, String mode) throws SFTPException {
    if (channel == null) {
        throw new SFTPException("Connection is not established!");
    }
    if (remoteDir == null) {
        remoteDir = "./";
    } else if (!remoteDir.endsWith("/")) {
        remoteDir += "/";
    }
    RecurciveSearchFile rsf = new RecurciveSearchFile(lpwd().getPath());
    File[] localFiles = rsf.listAllTreeFiles(local);
    for (int i = 0; i < localFiles.length; i++) {
        if (mode == null) {
            put(localFiles[i].getAbsolutePath(), remoteDir + localFiles[i].getName());
        } else {
            put(localFiles[i].getAbsolutePath(), remoteDir + localFiles[i].getName(), mode);
        }
    }
}
Also used : RecurciveSearchFile(jp.ossc.nimbus.io.RecurciveSearchFile) File(java.io.File) RecurciveSearchFile(jp.ossc.nimbus.io.RecurciveSearchFile) SFTPException(jp.ossc.nimbus.service.sftp.SFTPException)

Example 3 with SFTPException

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

the class SFTPClientImpl method ls.

public String[] ls(String path) throws NoSuchFileSFTPException, SFTPException {
    if (channel == null) {
        throw new SFTPException("Connection is not established!");
    }
    try {
        List entries = channel.ls(path);
        String[] fileNames = new String[entries.size()];
        for (int i = 0; i < entries.size(); i++) {
            ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) entries.get(i);
            fileNames[i] = entry.getFilename();
        }
        return fileNames;
    } catch (SftpException e) {
        if (e.id == ERROR_ID_NO_SUCH_FILE) {
            throw new NoSuchFileSFTPException("It failed to ls! path=" + path, e);
        }
        throw new SFTPException("It failed to ls! path=" + path, e);
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpException(com.jcraft.jsch.SftpException) List(java.util.List) SFTPException(jp.ossc.nimbus.service.sftp.SFTPException)

Example 4 with SFTPException

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

the class SFTPClientImpl method lsFile.

public SFTPFile[] lsFile(String path) throws SFTPException {
    if (channel == null) {
        throw new SFTPException("Connection is not established!");
    }
    try {
        List entries = channel.ls(path);
        SFTPFile[] files = new SFTPFile[entries.size()];
        for (int i = 0; i < entries.size(); i++) {
            ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) entries.get(i);
            files[i] = new SFTPFileImpl(entry);
        }
        return files;
    } catch (SftpException e) {
        if (e.id == ERROR_ID_NO_SUCH_FILE) {
            throw new NoSuchFileSFTPException("It failed to ls! path=" + path, e);
        }
        throw new SFTPException("It failed to ls! path=" + path, e);
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpException(com.jcraft.jsch.SftpException) List(java.util.List) SFTPException(jp.ossc.nimbus.service.sftp.SFTPException)

Example 5 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, String password) throws SFTPException {
    if (jsch != null) {
        throw new SFTPException("It is already connected!");
    }
    jsch = new JSch();
    try {
        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);
        }
        if (password != null) {
            session.setPassword(password);
        }
        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)

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