Search in sources :

Example 11 with GenericFileOperationFailedException

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

the class FtpProducerFileExistFailTest method testFail.

@Test
public void testFail() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello World");
    mock.expectedFileExists(FTP_ROOT_DIR + "/exist/hello.txt", "Hello World");
    try {
        template.sendBodyAndHeader(getFtpUrl(), "Bye World", Exchange.FILE_NAME, "hello.txt");
        fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
        GenericFileOperationFailedException cause = assertIsInstanceOf(GenericFileOperationFailedException.class, e.getCause());
        assertEquals("File already exist: exist/hello.txt. Cannot write new file.", cause.getMessage());
    }
    assertMockEndpointsSatisfied();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) Test(org.junit.Test)

Example 12 with GenericFileOperationFailedException

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

the class GenericFileDeleteProcessStrategyTest method testCannotDeleteFile.

public void testCannotDeleteFile() throws Exception {
    deleteCounter = 0;
    existsCounter = 0;
    @SuppressWarnings("unchecked") GenericFileEndpoint<Object> endpoint = context.getEndpoint("file://target/foo", GenericFileEndpoint.class);
    Exchange exchange = endpoint.createExchange();
    GenericFile<Object> file = new GenericFile<Object>();
    file.setAbsoluteFilePath("target/foo/boom.txt");
    GenericFileDeleteProcessStrategy<Object> strategy = new GenericFileDeleteProcessStrategy<Object>();
    try {
        strategy.commit(new MyGenericFileOperations(), endpoint, exchange, file);
        fail("Should have thrown an exception");
    } catch (GenericFileOperationFailedException e) {
    // expected
    }
    assertEquals("Should have tried to delete file 3 times", 3, deleteCounter);
    assertEquals("Should have tried to delete file 3 times", 3, existsCounter);
}
Also used : Exchange(org.apache.camel.Exchange) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) GenericFile(org.apache.camel.component.file.GenericFile)

Example 13 with GenericFileOperationFailedException

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

the class ScpOperations method storeFile.

@Override
public boolean storeFile(String name, Exchange exchange) throws GenericFileOperationFailedException {
    ObjectHelper.notNull(session, "session");
    ScpConfiguration cfg = endpoint.getConfiguration();
    int timeout = cfg.getConnectTimeout();
    if (LOG.isTraceEnabled()) {
        LOG.trace("Opening channel to {} with {} timeout...", cfg.remoteServerInformation(), timeout > 0 ? (Integer.toString(timeout) + " ms") : "no");
    }
    String file = getRemoteFile(name, cfg);
    InputStream is = null;
    if (exchange.getIn().getBody() == null) {
        // Do an explicit test for a null body and decide what to do
        if (endpoint.isAllowNullBody()) {
            LOG.trace("Writing empty file.");
            is = new ByteArrayInputStream(new byte[] {});
        } else {
            throw new GenericFileOperationFailedException("Cannot write null body to file: " + name);
        }
    }
    try {
        channel = (ChannelExec) session.openChannel("exec");
        channel.setCommand(getScpCommand(cfg, file));
        channel.connect(timeout);
        LOG.trace("Channel connected to {}", cfg.remoteServerInformation());
        try {
            if (is == null) {
                is = exchange.getIn().getMandatoryBody(InputStream.class);
            }
            write(channel, file, is, cfg);
        } catch (InvalidPayloadException e) {
            throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
        } catch (IOException e) {
            throw new GenericFileOperationFailedException("Failed to write file " + file, e);
        } finally {
            // must close stream after usage
            IOHelper.close(is);
        }
    } catch (JSchException e) {
        throw new GenericFileOperationFailedException("Failed to write file " + file, e);
    } finally {
        if (channel != null) {
            LOG.trace("Disconnecting 'exec' scp channel");
            channel.disconnect();
            channel = null;
            LOG.trace("Channel disconnected from {}", cfg.remoteServerInformation());
        }
    }
    return true;
}
Also used : JSchException(com.jcraft.jsch.JSchException) ByteArrayInputStream(java.io.ByteArrayInputStream) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) GenericFileEndpoint(org.apache.camel.component.file.GenericFileEndpoint)

Example 14 with GenericFileOperationFailedException

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

the class TarAggregationStrategy method aggregate.

@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    File tarFile;
    Exchange answer = oldExchange;
    // Guard against empty new exchanges
    if (newExchange == null) {
        return oldExchange;
    }
    // First time for this aggregation
    if (oldExchange == null) {
        try {
            tarFile = FileUtil.createTempFile(this.filePrefix, this.fileSuffix, parentDir);
            LOG.trace("Created temporary file: {}", tarFile);
        } catch (IOException e) {
            throw new GenericFileOperationFailedException(e.getMessage(), e);
        }
        answer = newExchange;
        answer.addOnCompletion(new DeleteTarFileOnCompletion(tarFile));
    } else {
        tarFile = oldExchange.getIn().getBody(File.class);
    }
    Object body = newExchange.getIn().getBody();
    if (body instanceof WrappedFile) {
        body = ((WrappedFile) body).getFile();
    }
    if (body instanceof File) {
        try {
            File appendFile = (File) body;
            // do not try to append empty files
            if (appendFile.length() > 0) {
                String entryName = preserveFolderStructure ? newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class) : newExchange.getIn().getMessageId();
                addFileToTar(tarFile, appendFile, this.preserveFolderStructure ? entryName : null);
                GenericFile<File> genericFile = FileConsumer.asGenericFile(tarFile.getParent(), tarFile, Charset.defaultCharset().toString(), false);
                genericFile.bindToExchange(answer);
            }
        } catch (Exception e) {
            throw new GenericFileOperationFailedException(e.getMessage(), e);
        }
    } else {
        // Handle all other messages
        try {
            byte[] buffer = newExchange.getIn().getMandatoryBody(byte[].class);
            // do not try to append empty data
            if (buffer.length > 0) {
                String entryName = useFilenameHeader ? newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class) : newExchange.getIn().getMessageId();
                addEntryToTar(tarFile, entryName, buffer, buffer.length);
                GenericFile<File> genericFile = FileConsumer.asGenericFile(tarFile.getParent(), tarFile, Charset.defaultCharset().toString(), false);
                genericFile.bindToExchange(answer);
            }
        } catch (Exception e) {
            throw new GenericFileOperationFailedException(e.getMessage(), e);
        }
    }
    return answer;
}
Also used : Exchange(org.apache.camel.Exchange) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) WrappedFile(org.apache.camel.WrappedFile) IOException(java.io.IOException) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) IOException(java.io.IOException)

Example 15 with GenericFileOperationFailedException

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

the class ZipAggregationStrategy method aggregate.

@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    File zipFile;
    Exchange answer = oldExchange;
    // Guard against empty new exchanges
    if (newExchange == null) {
        return oldExchange;
    }
    // First time for this aggregation
    if (oldExchange == null) {
        try {
            zipFile = FileUtil.createTempFile(this.filePrefix, this.fileSuffix);
        } catch (IOException e) {
            throw new GenericFileOperationFailedException(e.getMessage(), e);
        }
        answer = newExchange;
        answer.addOnCompletion(new DeleteZipFileOnCompletion(zipFile));
    } else {
        zipFile = oldExchange.getIn().getBody(File.class);
    }
    Object body = newExchange.getIn().getBody();
    if (body instanceof WrappedFile) {
        body = ((WrappedFile) body).getFile();
    }
    if (body instanceof File) {
        try {
            File appendFile = (File) body;
            // do not try to append empty files
            if (appendFile.length() > 0) {
                String entryName = preserveFolderStructure ? newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class) : newExchange.getIn().getMessageId();
                addFileToZip(zipFile, appendFile, this.preserveFolderStructure ? entryName : null);
                GenericFile<File> genericFile = FileConsumer.asGenericFile(zipFile.getParent(), zipFile, Charset.defaultCharset().toString(), false);
                genericFile.bindToExchange(answer);
            }
        } catch (Exception e) {
            throw new GenericFileOperationFailedException(e.getMessage(), e);
        }
    } else {
        // Handle all other messages
        try {
            byte[] buffer = newExchange.getIn().getMandatoryBody(byte[].class);
            // do not try to append empty data
            if (buffer.length > 0) {
                String entryName = useFilenameHeader ? newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class) : newExchange.getIn().getMessageId();
                addEntryToZip(zipFile, entryName, buffer, buffer.length);
                GenericFile<File> genericFile = FileConsumer.asGenericFile(zipFile.getParent(), zipFile, Charset.defaultCharset().toString(), false);
                genericFile.bindToExchange(answer);
            }
        } catch (Exception e) {
            throw new GenericFileOperationFailedException(e.getMessage(), e);
        }
    }
    return answer;
}
Also used : Exchange(org.apache.camel.Exchange) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) WrappedFile(org.apache.camel.WrappedFile) IOException(java.io.IOException) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) 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