Search in sources :

Example 36 with GenericFileOperationFailedException

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

the class FtpLoginTest method testBadLogin.

@Test
public void testBadLogin() throws Exception {
    try {
        uploadFile("dummy", "cantremeber");
        fail("Should have thrown a GenericFileOperationFailedException");
    } catch (GenericFileOperationFailedException e) {
        // expected
        assertEquals(530, e.getCode());
    }
    // assert file NOT created
    File file = new File(FTP_ROOT_DIR + "/login/report.txt");
    assertFalse("The file should NOT exists", file.exists());
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) File(java.io.File) Test(org.junit.Test)

Example 37 with GenericFileOperationFailedException

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

the class SftpConsumerAutoCreateTest method testNoAutoCreate.

@Test
public void testNoAutoCreate() throws Exception {
    SftpEndpoint endpoint = (SftpEndpoint) this.getMandatoryEndpoint(getFtpUrl() + "&autoCreate=false");
    endpoint.start();
    try {
        endpoint.getExchanges();
        fail("Should fail with 550 No such directory.");
    } catch (GenericFileOperationFailedException ignored) {
    // ignore
    }
}
Also used : SftpEndpoint(org.apache.camel.component.file.remote.SftpEndpoint) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) Test(org.junit.Test)

Example 38 with GenericFileOperationFailedException

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

the class FtpConsumerDeleteNoWritePermissionTest method testConsumerDeleteNoWritePermission.

@Test
public void testConsumerDeleteNoWritePermission() throws Exception {
    PollingConsumer consumer = context.getEndpoint(getFtpUrl()).createPollingConsumer();
    consumer.start();
    Exchange out = consumer.receive(3000);
    assertNotNull("Should get the file", out);
    try {
        // give consumer time to try to delete the file
        Thread.sleep(1000);
        consumer.stop();
    } catch (GenericFileOperationFailedException fofe) {
    // expected, ignore
    }
}
Also used : Exchange(org.apache.camel.Exchange) PollingConsumer(org.apache.camel.PollingConsumer) GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) Test(org.junit.Test)

Example 39 with GenericFileOperationFailedException

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

the class GenericFileRenameExclusiveReadLockStrategy method acquireExclusiveReadLock.

@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<T> operations, GenericFile<T> file, Exchange exchange) throws Exception {
    LOG.trace("Waiting for exclusive read lock to file: {}", file);
    // the trick is to try to rename the file, if we can rename then we have exclusive read
    // since its a Generic file we cannot use java.nio to get a RW lock
    String newName = file.getFileName() + ".camelExclusiveReadLock";
    // make a copy as result and change its file name
    GenericFile<T> newFile = file.copyFrom(file);
    newFile.changeFileName(newName);
    StopWatch watch = new StopWatch();
    boolean exclusive = false;
    while (!exclusive) {
        // timeout check
        if (timeout > 0) {
            long delta = watch.taken();
            if (delta > timeout) {
                CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file);
                // we could not get the lock within the timeout period, so return false
                return false;
            }
        }
        try {
            exclusive = operations.renameFile(file.getAbsoluteFilePath(), newFile.getAbsoluteFilePath());
        } catch (GenericFileOperationFailedException ex) {
            if (ex.getCause() != null && ex.getCause() instanceof FileNotFoundException) {
                exclusive = false;
            } else {
                throw ex;
            }
        }
        if (exclusive) {
            LOG.trace("Acquired exclusive read lock to file: {}", file);
            // rename it back so we can read it
            operations.renameFile(newFile.getAbsoluteFilePath(), file.getAbsoluteFilePath());
        } else {
            boolean interrupted = sleep();
            if (interrupted) {
                // we were interrupted while sleeping, we are likely being shutdown so return false
                return false;
            }
        }
    }
    return true;
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) FileNotFoundException(java.io.FileNotFoundException) StopWatch(org.apache.camel.util.StopWatch)

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