Search in sources :

Example 26 with GenericFileOperationFailedException

use of org.apache.camel.component.file.GenericFileOperationFailedException 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 27 with GenericFileOperationFailedException

use of org.apache.camel.component.file.GenericFileOperationFailedException 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 28 with GenericFileOperationFailedException

use of org.apache.camel.component.file.GenericFileOperationFailedException 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)

Example 29 with GenericFileOperationFailedException

use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.

the class FtpOperations method deleteFile.

public boolean deleteFile(String name) throws GenericFileOperationFailedException {
    log.debug("Deleting file: {}", name);
    boolean result;
    String target = name;
    String currentDir = null;
    try {
        if (endpoint.getConfiguration().isStepwise()) {
            // remember current directory
            currentDir = getCurrentDirectory();
            target = FileUtil.stripPath(name);
            try {
                changeCurrentDirectory(FileUtil.onlyPath(name));
            } catch (GenericFileOperationFailedException e) {
                // we could not change directory, try to change back before
                changeCurrentDirectory(currentDir);
                throw e;
            }
        }
        // delete the file
        log.trace("Client deleteFile: {}", target);
        result = client.deleteFile(target);
        // change back to previous directory
        if (currentDir != null) {
            changeCurrentDirectory(currentDir);
        }
    } catch (IOException e) {
        throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
    }
    return result;
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) IOException(java.io.IOException)

Example 30 with GenericFileOperationFailedException

use of org.apache.camel.component.file.GenericFileOperationFailedException in project camel by apache.

the class FtpOperations method listFiles.

public List<FTPFile> listFiles(String path) throws GenericFileOperationFailedException {
    log.trace("listFiles({})", path);
    // use current directory if path not given
    if (ObjectHelper.isEmpty(path)) {
        path = ".";
    }
    try {
        final List<FTPFile> list = new ArrayList<FTPFile>();
        FTPFile[] files = client.listFiles(path);
        // can return either null or an empty list depending on FTP servers
        if (files != null) {
            list.addAll(Arrays.asList(files));
        }
        return list;
    } catch (IOException e) {
        throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
    }
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException)

Aggregations

GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)39 IOException (java.io.IOException)22 SftpException (com.jcraft.jsch.SftpException)10 File (java.io.File)8 InvalidPayloadException (org.apache.camel.InvalidPayloadException)7 GenericFile (org.apache.camel.component.file.GenericFile)7 Test (org.junit.Test)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 Exchange (org.apache.camel.Exchange)6 GenericFileEndpoint (org.apache.camel.component.file.GenericFileEndpoint)5 ChannelSftp (com.jcraft.jsch.ChannelSftp)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 FTPFile (org.apache.commons.net.ftp.FTPFile)4 JSchException (com.jcraft.jsch.JSchException)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ArrayList (java.util.ArrayList)3 StopWatch (org.apache.camel.util.StopWatch)3