Search in sources :

Example 66 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 67 with SftpException

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

the class SftpOperations method retrieveFileToFileInLocalWorkDirectory.

@SuppressWarnings("unchecked")
private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exchange) throws GenericFileOperationFailedException {
    File temp;
    File local = new File(endpoint.getLocalWorkDirectory());
    OutputStream os;
    GenericFile<ChannelSftp.LsEntry> file = (GenericFile<ChannelSftp.LsEntry>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
    ObjectHelper.notNull(file, "Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");
    try {
        // use relative filename in local work directory
        String relativeName = file.getRelativeFilePath();
        temp = new File(local, relativeName + ".inprogress");
        local = new File(local, relativeName);
        // create directory to local work file
        local.mkdirs();
        // delete any existing files
        if (temp.exists()) {
            if (!FileUtil.deleteFile(temp)) {
                throw new GenericFileOperationFailedException("Cannot delete existing local work file: " + temp);
            }
        }
        if (local.exists()) {
            if (!FileUtil.deleteFile(local)) {
                throw new GenericFileOperationFailedException("Cannot delete existing local work file: " + local);
            }
        }
        // create new temp local work file
        if (!temp.createNewFile()) {
            throw new GenericFileOperationFailedException("Cannot create new local work file: " + temp);
        }
        // store content as a file in the local work directory in the temp handle
        os = new FileOutputStream(temp);
        // set header with the path to the local work file
        exchange.getIn().setHeader(Exchange.FILE_LOCAL_WORK_PATH, local.getPath());
    } catch (Exception e) {
        throw new GenericFileOperationFailedException("Cannot create new local work file: " + local);
    }
    String currentDir = null;
    try {
        // store the java.io.File handle as the body
        file.setBody(local);
        String remoteName = name;
        if (endpoint.getConfiguration().isStepwise()) {
            // remember current directory
            currentDir = getCurrentDirectory();
            // change directory to path where the file is to be retrieved
            // (must do this as some FTP servers cannot retrieve using absolute path)
            String path = FileUtil.onlyPath(name);
            if (path != null) {
                changeCurrentDirectory(path);
            }
            // remote name is now only the file name as we just changed directory
            remoteName = FileUtil.stripPath(name);
        }
        channel.get(remoteName, os);
    } catch (SftpException e) {
        LOG.trace("Error occurred during retrieving file: {} to local directory. Deleting local work file: {}", name, temp);
        // failed to retrieve the file so we need to close streams and delete in progress file
        // must close stream before deleting file
        IOHelper.close(os, "retrieve: " + name, LOG);
        boolean deleted = FileUtil.deleteFile(temp);
        if (!deleted) {
            LOG.warn("Error occurred during retrieving file: " + name + " to local directory. Cannot delete local work file: " + temp);
        }
        throw new GenericFileOperationFailedException("Cannot retrieve file: " + name, e);
    } finally {
        IOHelper.close(os, "retrieve: " + name, LOG);
        // change back to current directory if we changed directory
        if (currentDir != null) {
            changeCurrentDirectory(currentDir);
        }
    }
    LOG.debug("Retrieve file to local work file result: true");
    // operation went okay so rename temp to local after we have retrieved the data
    LOG.trace("Renaming local in progress file from: {} to: {}", temp, local);
    try {
        if (!FileUtil.renameFile(temp, local, false)) {
            throw new GenericFileOperationFailedException("Cannot rename local work file from: " + temp + " to: " + local);
        }
    } catch (IOException e) {
        throw new GenericFileOperationFailedException("Cannot rename local work file from: " + temp + " to: " + local, e);
    }
    return true;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) GenericFile(org.apache.camel.component.file.GenericFile) SftpException(com.jcraft.jsch.SftpException) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) IOException(java.io.IOException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) JSchException(com.jcraft.jsch.JSchException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 68 with SftpException

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

the class SftpOperations method buildDirectory.

public synchronized boolean buildDirectory(String directory, boolean absolute) throws GenericFileOperationFailedException {
    // must normalize directory first
    directory = endpoint.getConfiguration().normalizePath(directory);
    LOG.trace("buildDirectory({},{})", directory, absolute);
    // ignore absolute as all dirs are relative with FTP
    boolean success = false;
    String originalDirectory = getCurrentDirectory();
    try {
        // maybe the full directory already exists
        try {
            channel.cd(directory);
            success = true;
        } catch (SftpException e) {
        // ignore, we could not change directory so try to create it instead
        }
        if (!success) {
            LOG.debug("Trying to build remote directory: {}", directory);
            try {
                channel.mkdir(directory);
                success = true;
            } catch (SftpException e) {
                // we are here if the server side doesn't create intermediate folders
                // so create the folder one by one
                success = buildDirectoryChunks(directory);
            }
        }
    } catch (IOException e) {
        throw new GenericFileOperationFailedException("Cannot build directory: " + directory, e);
    } catch (SftpException e) {
        throw new GenericFileOperationFailedException("Cannot build directory: " + directory, e);
    } finally {
        // change back to original directory
        if (originalDirectory != null) {
            changeCurrentDirectory(originalDirectory);
        }
    }
    return success;
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException)

Example 69 with SftpException

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

the class SftpOperations method renameFile.

public synchronized boolean renameFile(String from, String to) throws GenericFileOperationFailedException {
    LOG.debug("Renaming file: {} to: {}", from, to);
    try {
        reconnectIfNecessary();
        channel.rename(from, to);
        return true;
    } catch (SftpException e) {
        LOG.debug("Cannot rename file from: " + from + " to: " + to, e);
        throw new GenericFileOperationFailedException("Cannot rename file from: " + from + " to: " + to, e);
    }
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) SftpException(com.jcraft.jsch.SftpException)

Example 70 with SftpException

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

the class SftpOperations method retrieveFileToStreamInBody.

@SuppressWarnings("unchecked")
private boolean retrieveFileToStreamInBody(String name, Exchange exchange) throws GenericFileOperationFailedException {
    OutputStream os = null;
    String currentDir = null;
    try {
        GenericFile<ChannelSftp.LsEntry> target = (GenericFile<ChannelSftp.LsEntry>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
        ObjectHelper.notNull(target, "Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");
        String remoteName = name;
        if (endpoint.getConfiguration().isStepwise()) {
            // remember current directory
            currentDir = getCurrentDirectory();
            // change directory to path where the file is to be retrieved
            // (must do this as some FTP servers cannot retrieve using absolute path)
            String path = FileUtil.onlyPath(name);
            if (path != null) {
                changeCurrentDirectory(path);
            }
            // remote name is now only the file name as we just changed directory
            remoteName = FileUtil.stripPath(name);
        }
        // use input stream which works with Apache SSHD used for testing
        InputStream is = channel.get(remoteName);
        if (endpoint.getConfiguration().isStreamDownload()) {
            target.setBody(is);
            exchange.getIn().setHeader(RemoteFileComponent.REMOTE_FILE_INPUT_STREAM, is);
        } else {
            os = new ByteArrayOutputStream();
            target.setBody(os);
            IOHelper.copyAndCloseInput(is, os);
        }
        return true;
    } catch (IOException e) {
        throw new GenericFileOperationFailedException("Cannot retrieve file: " + name, e);
    } catch (SftpException e) {
        throw new GenericFileOperationFailedException("Cannot retrieve file: " + name, e);
    } finally {
        IOHelper.close(os, "retrieve: " + name, LOG);
        // change back to current directory if we changed directory
        if (currentDir != null) {
            changeCurrentDirectory(currentDir);
        }
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SftpException(com.jcraft.jsch.SftpException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GenericFile(org.apache.camel.component.file.GenericFile)

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