Search in sources :

Example 36 with SftpException

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

the class SFTPFileSystem method delete.

/**
   * Convenience method, so that we don't open a new connection when using this
   * method from within another method. Otherwise every API invocation incurs
   * the overhead of opening/closing a TCP connection.
   */
private boolean delete(ChannelSftp channel, Path file, boolean recursive) throws IOException {
    Path workDir;
    try {
        workDir = new Path(channel.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, file);
    String pathName = absolute.toUri().getPath();
    FileStatus fileStat = null;
    try {
        fileStat = getFileStatus(channel, absolute);
    } catch (FileNotFoundException e) {
        // file not found, no need to delete, return true
        return false;
    }
    if (!fileStat.isDirectory()) {
        boolean status = true;
        try {
            channel.rm(pathName);
        } catch (SftpException e) {
            status = false;
        }
        return status;
    } else {
        boolean status = true;
        FileStatus[] dirEntries = listStatus(channel, absolute);
        if (dirEntries != null && dirEntries.length > 0) {
            if (!recursive) {
                throw new IOException(String.format(E_DIR_NOTEMPTY, file));
            }
            for (int i = 0; i < dirEntries.length; ++i) {
                delete(channel, new Path(absolute, dirEntries[i].getPath()), recursive);
            }
        }
        try {
            channel.rmdir(pathName);
        } catch (SftpException e) {
            status = false;
        }
        return status;
    }
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SftpException(com.jcraft.jsch.SftpException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 37 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 {
    FileInputStream fis = null;
    try {
        // change to correct remote directory, but save the one that the
        // client was in before that
        String currentDir = this.channel.pwd();
        this.channel.cd(remoteDir);
        // upload the file
        File file = new File(localFile);
        fis = new FileInputStream(file);
        if (synchronizationSftpTransferListener != null) {
            this.channel.put(fis, remoteFile, synchronizationSftpTransferListener);
        } else if (isDebugMode() && debugProgressMonitor != null) {
            debugProgressMonitor.setTransferMetadata(localFile, remoteFile, file.length());
            this.channel.put(fis, remoteFile, debugProgressMonitor);
        } else {
            this.channel.put(fis, remoteFile);
        }
        // go back to the location that the client was before the upload
        // took place
        this.channel.cd(currentDir);
    } 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 38 with SftpException

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

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

Example 40 with SftpException

use of com.jcraft.jsch.SftpException in project azure-tools-for-java by Microsoft.

the class SparkSubmitHelper method sftpFileToEmulator.

public String sftpFileToEmulator(String localFile, String folderPath, IClusterDetail clusterDetail) throws IOException, HDIException, JSchException, SftpException {
    EmulatorClusterDetail emulatorClusterDetail = (EmulatorClusterDetail) clusterDetail;
    final File file = new File(localFile);
    try (FileInputStream fileInputStream = new FileInputStream(file)) {
        try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
            String sshEndpoint = emulatorClusterDetail.getSSHEndpoint();
            URL url = new URL(sshEndpoint);
            String host = url.getHost();
            int port = url.getPort();
            JSch jsch = new JSch();
            Session session = jsch.getSession(emulatorClusterDetail.getHttpUserName(), host, port);
            session.setPassword(emulatorClusterDetail.getHttpPassword());
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            String[] folders = folderPath.split("/");
            for (String folder : folders) {
                if (folder.length() > 0) {
                    try {
                        channel.cd(folder);
                    } catch (SftpException e) {
                        channel.mkdir(folder);
                        channel.cd(folder);
                    }
                }
            }
            channel.put(bufferedInputStream, file.getName());
            channel.disconnect();
            session.disconnect();
            return file.getName();
        }
    }
}
Also used : EmulatorClusterDetail(com.microsoft.azure.hdinsight.sdk.cluster.EmulatorClusterDetail) SftpException(com.jcraft.jsch.SftpException) JSch(com.jcraft.jsch.JSch) FileInputStream(java.io.FileInputStream) URL(java.net.URL) ChannelSftp(com.jcraft.jsch.ChannelSftp) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) Session(com.jcraft.jsch.Session)

Aggregations

SftpException (com.jcraft.jsch.SftpException)54 ChannelSftp (com.jcraft.jsch.ChannelSftp)26 IOException (java.io.IOException)18 Path (org.apache.hadoop.fs.Path)14 File (java.io.File)13 SftpATTRS (com.jcraft.jsch.SftpATTRS)9 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)9 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)8 JSchException (com.jcraft.jsch.JSchException)8 FileStatus (org.apache.hadoop.fs.FileStatus)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 Session (com.jcraft.jsch.Session)7 FileNotFoundException (java.io.FileNotFoundException)7 CoreException (org.eclipse.core.runtime.CoreException)7 IStatus (org.eclipse.core.runtime.IStatus)7 Status (org.eclipse.core.runtime.Status)7 Channel (com.jcraft.jsch.Channel)5 OutputStream (java.io.OutputStream)5 ArrayList (java.util.ArrayList)5 Vector (java.util.Vector)5