Search in sources :

Example 96 with SftpException

use of com.jcraft.jsch.SftpException in project n2a by frothga.

the class SshFileSystemProvider method checkAccess.

public void checkAccess(Path path, AccessMode... modes) throws IOException {
    SshPath A = (SshPath) path;
    String name = A.toAbsolutePath().toString();
    try {
        SftpATTRS attributes = A.getSftp().stat(name);
        int permissions = attributes.getPermissions();
        for (AccessMode mode : modes) {
            switch(mode) {
                case READ:
                    if ((permissions & 0444) == 0)
                        throw new AccessDeniedException("READ " + A);
                    break;
                case WRITE:
                    if ((permissions & 0222) == 0)
                        throw new AccessDeniedException("WRITE " + A);
                    break;
                case EXECUTE:
                    if ((permissions & 0111) == 0)
                        throw new AccessDeniedException("EXECUTE " + A);
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
        }
    } catch (SftpException e) {
        if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE)
            throw new NoSuchFileException(name);
        throw new IOException(e);
    }
}
Also used : AccessDeniedException(java.nio.file.AccessDeniedException) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException) NoSuchFileException(java.nio.file.NoSuchFileException) AccessMode(java.nio.file.AccessMode) IOException(java.io.IOException)

Example 97 with SftpException

use of com.jcraft.jsch.SftpException in project n2a by frothga.

the class SshFileSystemProvider method delete.

public void delete(Path path) throws IOException {
    String name = path.toAbsolutePath().toString();
    try {
        WrapperSftp sftp = ((SshPath) path).getSftp();
        // doesn't follow links
        SftpATTRS attributes = sftp.lstat(name);
        if (attributes.isDir())
            sftp.rmdir(name);
        else
            sftp.rm(name);
    } catch (SftpException e) {
        if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE)
            throw new NoSuchFileException(name);
        throw new IOException(e);
    }
}
Also used : SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) WrapperSftp(gov.sandia.n2a.host.SshFileSystem.WrapperSftp)

Example 98 with SftpException

use of com.jcraft.jsch.SftpException in project ats-framework by Axway.

the class SftpClient method performUploadFile.

@Override
protected void performUploadFile(String localFile, String remoteDir, String remoteFile) throws FileTransferException {
    checkConnected("file upload");
    FileInputStream fis = null;
    try {
        String remoteFileAbsPath = null;
        remoteDir = remoteDir.replace("\\", "/");
        remoteFile = remoteFile.replace("\\", "/");
        if (remoteDir.endsWith("/") && remoteFile.endsWith("/")) {
            remoteFileAbsPath = remoteDir.substring(0, remoteDir.length() - 2) + remoteFile;
        } else if (!remoteDir.endsWith("/") && !remoteFile.endsWith("/")) {
            remoteFileAbsPath = remoteDir + "/" + remoteFile;
        } else {
            remoteFileAbsPath = remoteDir + remoteFile;
        }
        // upload the file
        File file = new File(localFile);
        fis = new FileInputStream(file);
        if (synchronizationSftpTransferListener != null) {
            this.channel.put(fis, remoteFileAbsPath, synchronizationSftpTransferListener);
        } else if (isDebugMode() && debugProgressMonitor != null) {
            debugProgressMonitor.setTransferMetadata(localFile, remoteFileAbsPath, file.length());
            this.channel.put(fis, remoteFileAbsPath, debugProgressMonitor);
        } else {
            this.channel.put(fis, remoteFileAbsPath);
        }
    } catch (SftpException e) {
        log.error("Unable to upload file!", e);
        throw new FileTransferException(e);
    } catch (FileNotFoundException e) {
        log.error("Unable to find the file that needs to be uploaded!", e);
        throw new FileTransferException(e);
    } finally {
        // close the file input stream
        IoUtils.closeStream(fis, "Unable to close the file stream after successful upload!");
    }
    if (remoteDir != null && !remoteDir.endsWith("/")) {
        remoteDir += "/";
    }
    log.info("Successfully uploaded '" + localFile + "' to '" + remoteDir + remoteFile + "', host " + this.hostname);
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) SftpException(com.jcraft.jsch.SftpException) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 99 with SftpException

use of com.jcraft.jsch.SftpException in project acceptance-test-harness by jenkinsci.

the class GitRepo method transferToDockerContainer.

/**
 * Zip bare repository, copy to Docker container using sftp, then unzip.
 * The repo is now accessible over "ssh://git@ip:port/home/git/gitRepo.git"
 *
 * @param host IP of Docker container
 * @param port SSH port of Docker container
 */
public void transferToDockerContainer(String host, int port) {
    try {
        Path zipPath = Files.createTempFile("git", "zip");
        File zippedRepo = zipPath.toFile();
        String zippedFilename = zipPath.getFileName().toString();
        ZipUtil.pack(new File(dir.getPath()), zippedRepo);
        Properties props = new Properties();
        props.put("StrictHostKeyChecking", "no");
        JSch jSch = new JSch();
        jSch.addIdentity(privateKey.getAbsolutePath());
        Session session = jSch.getSession("git", host, port);
        session.setConfig(props);
        session.connect();
        ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
        channel.connect();
        channel.cd("/home/git");
        channel.put(new FileInputStream(zippedRepo), zippedFilename);
        ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
        InputStream in = channelExec.getInputStream();
        channelExec.setCommand("unzip " + zippedFilename + " -d " + REPO_NAME);
        channelExec.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        int index = 0;
        while ((line = reader.readLine()) != null) {
            System.out.println(++index + " : " + line);
        }
        channelExec.disconnect();
        channel.disconnect();
        session.disconnect();
        Files.delete(zipPath);
    } catch (IOException | JSchException | SftpException e) {
        throw new AssertionError("Can't transfer git repository to docker container", e);
    }
}
Also used : Path(java.nio.file.Path) JSchException(com.jcraft.jsch.JSchException) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) Properties(java.util.Properties) JSch(com.jcraft.jsch.JSch) FileInputStream(java.io.FileInputStream) ChannelExec(com.jcraft.jsch.ChannelExec) ChannelSftp(com.jcraft.jsch.ChannelSftp) BufferedReader(java.io.BufferedReader) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 100 with SftpException

use of com.jcraft.jsch.SftpException 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)

Aggregations

SftpException (com.jcraft.jsch.SftpException)113 ChannelSftp (com.jcraft.jsch.ChannelSftp)67 IOException (java.io.IOException)49 JSchException (com.jcraft.jsch.JSchException)28 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)24 File (java.io.File)24 SftpATTRS (com.jcraft.jsch.SftpATTRS)16 Path (org.apache.hadoop.fs.Path)15 Session (com.jcraft.jsch.Session)13 OutputStream (java.io.OutputStream)11 JSch (com.jcraft.jsch.JSch)10 FileNotFoundException (java.io.FileNotFoundException)10 InputStream (java.io.InputStream)10 Channel (com.jcraft.jsch.Channel)9 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)9 FileStatus (org.apache.hadoop.fs.FileStatus)9 ArrayList (java.util.ArrayList)8 Vector (java.util.Vector)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 Test (org.junit.Test)8