Search in sources :

Example 21 with GenericFileOperationFailedException

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

the class FtpOperations method retrieveFileToFileInLocalWorkDirectory.

@SuppressWarnings("unchecked")
private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exchange) throws GenericFileOperationFailedException {
    File temp;
    File local = new File(FileUtil.normalizePath(endpoint.getLocalWorkDirectory()));
    OutputStream os;
    try {
        // use relative filename in local work directory
        GenericFile<FTPFile> target = (GenericFile<FTPFile>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
        ObjectHelper.notNull(target, "Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");
        String relativeName = target.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);
    }
    boolean result;
    try {
        GenericFile<FTPFile> target = (GenericFile<FTPFile>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
        // store the java.io.File handle as the body
        target.setBody(local);
        String remoteName = name;
        String currentDir = null;
        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);
        }
        log.trace("Client retrieveFile: {}", remoteName);
        result = client.retrieveFile(remoteName, os);
        // store client reply information after the operation
        exchange.getIn().setHeader(FtpConstants.FTP_REPLY_CODE, client.getReplyCode());
        exchange.getIn().setHeader(FtpConstants.FTP_REPLY_STRING, client.getReplyString());
        // change back to current directory
        if (endpoint.getConfiguration().isStepwise()) {
            changeCurrentDirectory(currentDir);
        }
    } catch (IOException 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(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
    } finally {
        // need to close the stream before rename it
        IOHelper.close(os, "retrieve: " + name, log);
    }
    log.debug("Retrieve file to local work file result: {}", result);
    if (result) {
        log.trace("Renaming local in progress file from: {} to: {}", temp, local);
        // operation went okay so rename temp to local after we have retrieved the data
        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 result;
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) GenericFile(org.apache.camel.component.file.GenericFile) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) IOException(java.io.IOException) InvalidPayloadException(org.apache.camel.InvalidPayloadException)

Example 22 with GenericFileOperationFailedException

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

the class FtpOperations method disconnect.

public void disconnect() throws GenericFileOperationFailedException {
    // logout before disconnecting
    try {
        log.trace("Client logout");
        client.logout();
    } catch (IOException e) {
        throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
    } finally {
        try {
            log.trace("Client disconnect");
            client.disconnect();
        } catch (IOException e) {
            throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
        }
    }
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) IOException(java.io.IOException)

Example 23 with GenericFileOperationFailedException

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

the class FtpsOperations method connect.

@Override
public boolean connect(RemoteFileConfiguration configuration) throws GenericFileOperationFailedException {
    boolean answer = super.connect(configuration);
    FtpsConfiguration config = (FtpsConfiguration) configuration;
    if (answer) {
        try {
            String execProt = config.getExecProt();
            Long execPbsz = config.getExecPbsz();
            // use default values for prop and pbsz, unless told to not do so
            if (!config.isDisableSecureDataChannelDefaults()) {
                if (ObjectHelper.isEmpty(execProt)) {
                    execProt = "P";
                }
                if (ObjectHelper.isEmpty(execPbsz)) {
                    execPbsz = 0L;
                }
            }
            if (execPbsz != null) {
                log.debug("FTPClient initializing with execPbsz={}", execPbsz);
                getFtpClient().execPBSZ(execPbsz);
            }
            if (execProt != null) {
                log.debug("FTPClient initializing with execProt={}", execProt);
                getFtpClient().execPROT(execProt);
            }
        } catch (SSLException e) {
            throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
        } catch (IOException e) {
            throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
        }
    }
    return answer;
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException)

Example 24 with GenericFileOperationFailedException

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

the class SftpOperations method connect.

public synchronized boolean connect(RemoteFileConfiguration configuration) throws GenericFileOperationFailedException {
    if (isConnected()) {
        // already connected
        return true;
    }
    boolean connected = false;
    int attempt = 0;
    while (!connected) {
        try {
            if (LOG.isTraceEnabled() && attempt > 0) {
                LOG.trace("Reconnect attempt #{} connecting to + {}", attempt, configuration.remoteServerInformation());
            }
            if (channel == null || !channel.isConnected()) {
                if (session == null || !session.isConnected()) {
                    LOG.trace("Session isn't connected, trying to recreate and connect.");
                    session = createSession(configuration);
                    if (endpoint.getConfiguration().getConnectTimeout() > 0) {
                        LOG.trace("Connecting use connectTimeout: " + endpoint.getConfiguration().getConnectTimeout() + " ...");
                        session.connect(endpoint.getConfiguration().getConnectTimeout());
                    } else {
                        LOG.trace("Connecting ...");
                        session.connect();
                    }
                }
                LOG.trace("Channel isn't connected, trying to recreate and connect.");
                channel = (ChannelSftp) session.openChannel("sftp");
                if (endpoint.getConfiguration().getConnectTimeout() > 0) {
                    LOG.trace("Connecting use connectTimeout: " + endpoint.getConfiguration().getConnectTimeout() + " ...");
                    channel.connect(endpoint.getConfiguration().getConnectTimeout());
                } else {
                    LOG.trace("Connecting ...");
                    channel.connect();
                }
                LOG.debug("Connected to " + configuration.remoteServerInformation());
            }
            // yes we could connect
            connected = true;
        } catch (Exception e) {
            // check if we are interrupted so we can break out
            if (Thread.currentThread().isInterrupted()) {
                throw new GenericFileOperationFailedException("Interrupted during connecting", new InterruptedException("Interrupted during connecting"));
            }
            GenericFileOperationFailedException failed = new GenericFileOperationFailedException("Cannot connect to " + configuration.remoteServerInformation(), e);
            LOG.trace("Cannot connect due: {}", failed.getMessage());
            attempt++;
            if (attempt > endpoint.getMaximumReconnectAttempts()) {
                throw failed;
            }
            if (endpoint.getReconnectDelay() > 0) {
                try {
                    Thread.sleep(endpoint.getReconnectDelay());
                } catch (InterruptedException ie) {
                    // we could potentially also be interrupted during sleep
                    Thread.currentThread().interrupt();
                    throw new GenericFileOperationFailedException("Interrupted during sleeping", ie);
                }
            }
        }
    }
    configureBulkRequests();
    return true;
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) GenericFileEndpoint(org.apache.camel.component.file.GenericFileEndpoint) 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 25 with GenericFileOperationFailedException

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

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