Search in sources :

Example 51 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project gradle by gradle.

the class SftpResourceUploader method upload.

@Override
public void upload(LocalResource resource, URI destination) throws IOException {
    LockableSftpClient client = sftpClientFactory.createSftpClient(destination, credentials);
    try {
        ChannelSftp channel = client.getSftpClient();
        ensureParentDirectoryExists(channel, destination);
        InputStream sourceStream = resource.open();
        try {
            channel.put(sourceStream, destination.getPath());
        } finally {
            sourceStream.close();
        }
    } catch (com.jcraft.jsch.SftpException e) {
        throw ResourceExceptions.putFailed(destination, e);
    } finally {
        sftpClientFactory.releaseSftpClient(client);
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) InputStream(java.io.InputStream)

Example 52 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project hadoop by apache.

the class SFTPConnectionPool method shutdown.

/** Shutdown the connection pool and close all open connections. */
synchronized void shutdown() {
    if (this.con2infoMap == null) {
        // already shutdown in case it is called
        return;
    }
    LOG.info("Inside shutdown, con2infoMap size=" + con2infoMap.size());
    this.maxConnection = 0;
    Set<ChannelSftp> cons = con2infoMap.keySet();
    if (cons != null && cons.size() > 0) {
        // make a copy since we need to modify the underlying Map
        Set<ChannelSftp> copy = new HashSet<ChannelSftp>(cons);
        // Initiate disconnect from all outstanding connections
        for (ChannelSftp con : copy) {
            try {
                disconnect(con);
            } catch (IOException ioe) {
                ConnectionInfo info = con2infoMap.get(con);
                LOG.error("Error encountered while closing connection to " + info.getHost(), ioe);
            }
        }
    }
    // make sure no further connections can be returned.
    this.idleConnections = null;
    this.con2infoMap = null;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 53 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project hadoop by apache.

the class SFTPFileSystem method create.

/**
   * A stream obtained via this call must be closed before using other APIs of
   * this class or else the invocation will block.
   */
@Override
public FSDataOutputStream create(Path f, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
    final ChannelSftp client = connect();
    Path workDir;
    try {
        workDir = new Path(client.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, f);
    if (exists(client, f)) {
        if (overwrite) {
            delete(client, f, false);
        } else {
            disconnect(client);
            throw new IOException(String.format(E_FILE_EXIST, f));
        }
    }
    Path parent = absolute.getParent();
    if (parent == null || !mkdirs(client, parent, FsPermission.getDefault())) {
        parent = (parent == null) ? new Path("/") : parent;
        disconnect(client);
        throw new IOException(String.format(E_CREATE_DIR, parent));
    }
    OutputStream os;
    try {
        client.cd(parent.toUri().getPath());
        os = client.put(f.getName());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    FSDataOutputStream fos = new FSDataOutputStream(os, statistics) {

        @Override
        public void close() throws IOException {
            super.close();
            disconnect(client);
        }
    };
    return fos;
}
Also used : Path(org.apache.hadoop.fs.Path) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpException(com.jcraft.jsch.SftpException) OutputStream(java.io.OutputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) IOException(java.io.IOException) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream)

Example 54 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project GNS by MobilityFirst.

the class SFTPUpload method localFileNewer.

/**
   *
   * @param user
   * @param host
   * @param keyFile
   * @param fileToTransfer
   * @param sftpWorkingDirectory
   * @return true if the local file is newer
   */
public static boolean localFileNewer(String user, String host, File keyFile, String fileToTransfer, String sftpWorkingDirectory) {
    if (verbose) {
        System.out.println("Local File Newer Check " + fileToTransfer + " to " + host + "@" + user + " " + sftpWorkingDirectory);
    }
    try {
        ChannelSftp channelSftp = authenticateSftp(user, host, keyFile);
        Path paths = Paths.get(fileToTransfer);
        String localDir = paths.getParent().toString();
        channelSftp.cd(sftpWorkingDirectory);
        channelSftp.lcd(localDir);
        SftpATTRS remoteAttributes = channelSftp.stat(paths.getFileName().toString());
        long localTime = new File(fileToTransfer).lastModified();
        long remoteTime = remoteAttributes.getMTime() * 1000L;
        if (verbose) {
            System.out.println("L: " + localDir + " R: " + sftpWorkingDirectory + "\n" + "Local time = " + localTime + " Remote time = " + remoteTime);
        }
        if (verbose) {
            System.out.println("Result " + (localTime > remoteTime));
        }
        return localTime > remoteTime;
    } catch (JSchException | SftpException e) {
        System.out.println("Exception while checking for file newer:" + e);
        return false;
    }
}
Also used : Path(java.nio.file.Path) JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException) File(java.io.File)

Example 55 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project GNS by MobilityFirst.

the class SFTPUpload method uploadFile.

/**
   *
   * @param user
   * @param host
   * @param keyFile
   * @param fileToTransfer
   * @param sftpWorkingDirectory
   */
public static void uploadFile(String user, String host, File keyFile, String fileToTransfer, String sftpWorkingDirectory) {
    if (verbose) {
        System.out.println("Upload file from " + fileToTransfer + " to " + host + "@" + user + " " + sftpWorkingDirectory);
    }
    try {
        ChannelSftp channelSftp = authenticateSftp(user, host, keyFile);
        File f = new File(fileToTransfer);
        channelSftp.put(new FileInputStream(f), f.getName());
    } catch (JSchException | SftpException | FileNotFoundException e) {
        System.out.println("Exception while uploading file:" + e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpException(com.jcraft.jsch.SftpException) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

ChannelSftp (com.jcraft.jsch.ChannelSftp)99 SftpException (com.jcraft.jsch.SftpException)61 IOException (java.io.IOException)36 JSchException (com.jcraft.jsch.JSchException)28 Session (com.jcraft.jsch.Session)25 JSch (com.jcraft.jsch.JSch)20 Channel (com.jcraft.jsch.Channel)17 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)17 File (java.io.File)16 Test (org.junit.Test)14 InputStream (java.io.InputStream)12 SftpATTRS (com.jcraft.jsch.SftpATTRS)11 FileNotFoundException (java.io.FileNotFoundException)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 FileInputStream (java.io.FileInputStream)7 OutputStream (java.io.OutputStream)7 Path (org.apache.hadoop.fs.Path)7 CoreException (org.eclipse.core.runtime.CoreException)7 IStatus (org.eclipse.core.runtime.IStatus)7 Status (org.eclipse.core.runtime.Status)7