Search in sources :

Example 91 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project keepass2android by PhilippC.

the class SftpStorage method delete.

@Override
public void delete(String path) throws Exception {
    ChannelSftp c = init(path);
    delete(path, c);
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp)

Example 92 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project keepass2android by PhilippC.

the class SftpStorage method openFileForRead.

@Override
public InputStream openFileForRead(String path) throws Exception {
    ChannelSftp c = init(path);
    try {
        byte[] buff = new byte[8000];
        int bytesRead = 0;
        InputStream in = c.get(extractSessionPath(path));
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        while ((bytesRead = in.read(buff)) != -1) {
            bao.write(buff, 0, bytesRead);
        }
        byte[] data = bao.toByteArray();
        ByteArrayInputStream bin = new ByteArrayInputStream(data);
        c.getSession().disconnect();
        return bin;
    } catch (Exception e) {
        tryDisconnect(c);
        throw convertException(e);
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileNotFoundException(java.io.FileNotFoundException) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 93 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project keepass2android by PhilippC.

the class SftpStorage method uploadFile.

@Override
public void uploadFile(String path, byte[] data, boolean writeTransactional) throws Exception {
    ChannelSftp c = init(path);
    try {
        InputStream in = new ByteArrayInputStream(data);
        String targetPath = extractSessionPath(path);
        if (writeTransactional) {
            // upload to temporary location:
            String tmpPath = targetPath + ".tmp";
            c.put(in, tmpPath);
            // remove previous file:
            try {
                c.rm(targetPath);
            } catch (SftpException e) {
            // ignore. Can happen if file didn't exist before
            }
            // rename tmp to target path:
            c.rename(tmpPath, targetPath);
        } else {
            c.put(in, targetPath);
        }
        tryDisconnect(c);
    } catch (Exception e) {
        tryDisconnect(c);
        throw e;
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SftpException(com.jcraft.jsch.SftpException) FileNotFoundException(java.io.FileNotFoundException) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 94 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project ant-ivy by apache.

the class SFTPRepository method getSftpChannel.

/**
 * Establish the connection to the server if not yet connected, and listen to ivy events for
 * closing connection when resolve is finished. Not meant to be used in multi threaded
 * environment.
 *
 * @return the ChannelSftp with which a connection is established
 * @throws IOException
 *             if any connection problem occurs
 */
private ChannelSftp getSftpChannel(String pathOrUri) throws IOException {
    Session session = getSession(pathOrUri);
    String host = session.getHost();
    ChannelSftp channel = SshCache.getInstance().getChannelSftp(session);
    if (channel == null) {
        try {
            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            Message.verbose(":: SFTP :: connected to " + host + "!");
            SshCache.getInstance().attachChannelSftp(session, channel);
        } catch (JSchException e) {
            throw new IOException(e.getMessage(), e);
        }
    }
    return channel;
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) IOException(java.io.IOException) Session(com.jcraft.jsch.Session)

Example 95 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project ant-ivy by apache.

the class SshCache method getChannelSftp.

/**
 * retrieves an sftp channel from the cache
 *
 * @param session
 *            to connect to
 * @return channelSftp or null if not successful (channel not existent or dead)
 * @throws IOException should never happen
 */
public ChannelSftp getChannelSftp(Session session) throws IOException {
    ChannelSftp channel = null;
    Entry entry = getCacheEntry(session);
    if (entry != null) {
        channel = entry.getChannelSftp();
        if (channel != null && !channel.isConnected()) {
            entry.releaseChannelSftp();
            channel = null;
        }
    }
    return channel;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp)

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