Search in sources :

Example 6 with GenericFileOperationFailedException

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

the class SftpOperations method deleteFile.

public synchronized boolean deleteFile(String name) throws GenericFileOperationFailedException {
    LOG.debug("Deleting file: {}", name);
    try {
        reconnectIfNecessary();
        channel.rm(name);
        return true;
    } catch (SftpException e) {
        LOG.debug("Cannot delete file: " + name, e);
        throw new GenericFileOperationFailedException("Cannot delete file: " + name, e);
    }
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) SftpException(com.jcraft.jsch.SftpException)

Example 7 with GenericFileOperationFailedException

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

the class FtpOperations method doChangeDirectory.

private void doChangeDirectory(String path) {
    if (path == null || ".".equals(path) || ObjectHelper.isEmpty(path)) {
        return;
    }
    log.trace("Changing directory: {}", path);
    boolean success;
    try {
        if ("..".equals(path)) {
            changeToParentDirectory();
            success = true;
        } else {
            success = client.changeWorkingDirectory(path);
        }
    } catch (IOException e) {
        throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
    }
    if (!success) {
        throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), "Cannot change directory to: " + path);
    }
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) IOException(java.io.IOException)

Example 8 with GenericFileOperationFailedException

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

the class FtpOperations method doStoreFile.

private boolean doStoreFile(String name, String targetName, Exchange exchange) throws GenericFileOperationFailedException {
    log.trace("doStoreFile({})", targetName);
    // if an existing file already exists what should we do?
    if (endpoint.getFileExist() == GenericFileExist.Ignore || endpoint.getFileExist() == GenericFileExist.Fail || endpoint.getFileExist() == GenericFileExist.Move) {
        boolean existFile = existsFile(targetName);
        if (existFile && endpoint.getFileExist() == GenericFileExist.Ignore) {
            // ignore but indicate that the file was written
            log.trace("An existing file already exists: {}. Ignore and do not override it.", name);
            return true;
        } else if (existFile && endpoint.getFileExist() == GenericFileExist.Fail) {
            throw new GenericFileOperationFailedException("File already exist: " + name + ". Cannot write new file.");
        } else if (existFile && endpoint.getFileExist() == GenericFileExist.Move) {
            // move any existing file first
            doMoveExistingFile(name, targetName);
        }
    }
    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 {
        if (is == null) {
            String charset = endpoint.getCharset();
            if (charset != null) {
                // charset configured so we must convert to the desired
                // charset so we can write with encoding
                is = new ByteArrayInputStream(exchange.getIn().getMandatoryBody(String.class).getBytes(charset));
                log.trace("Using InputStream {} with charset {}.", is, charset);
            } else {
                is = exchange.getIn().getMandatoryBody(InputStream.class);
            }
        }
        final StopWatch watch = new StopWatch();
        boolean answer;
        log.debug("About to store file: {} using stream: {}", targetName, is);
        if (endpoint.getFileExist() == GenericFileExist.Append) {
            log.trace("Client appendFile: {}", targetName);
            answer = client.appendFile(targetName, is);
        } else {
            log.trace("Client storeFile: {}", targetName);
            answer = client.storeFile(targetName, is);
        }
        watch.stop();
        if (log.isDebugEnabled()) {
            log.debug("Took {} ({} millis) to store file: {} and FTP client returned: {}", new Object[] { TimeUtils.printDuration(watch.taken()), watch.taken(), targetName, answer });
        }
        // 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());
        // after storing file, we may set chmod on the file
        String chmod = ((FtpConfiguration) endpoint.getConfiguration()).getChmod();
        if (ObjectHelper.isNotEmpty(chmod)) {
            log.debug("Setting chmod: {} on file: {}", chmod, targetName);
            String command = "chmod " + chmod + " " + targetName;
            log.trace("Client sendSiteCommand: {}", command);
            boolean success = client.sendSiteCommand(command);
            log.trace("Client sendSiteCommand successful: {}", success);
        }
        return answer;
    } catch (IOException e) {
        throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
    } catch (InvalidPayloadException e) {
        throw new GenericFileOperationFailedException("Cannot store file: " + name, e);
    } finally {
        IOHelper.close(is, "store: " + name, log);
    }
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) StopWatch(org.apache.camel.util.StopWatch)

Example 9 with GenericFileOperationFailedException

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

the class FtpLoginNoRetryTest 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 10 with GenericFileOperationFailedException

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

the class GenericFileDeleteProcessStrategy method commit.

@Override
public void commit(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
    // special for file lock strategy as we must release that lock first before we can delete the file
    boolean releaseEager = exclusiveReadLockStrategy instanceof FileLockExclusiveReadLockStrategy;
    if (releaseEager) {
        exclusiveReadLockStrategy.releaseExclusiveReadLockOnCommit(operations, file, exchange);
    }
    try {
        deleteLocalWorkFile(exchange);
        operations.releaseRetreivedFileResources(exchange);
        int retries = 3;
        boolean deleted = false;
        while (retries > 0 && !deleted) {
            retries--;
            if (operations.deleteFile(file.getAbsoluteFilePath())) {
                // file is deleted
                deleted = true;
                break;
            }
            // some OS can report false when deleting but the file is still deleted
            // use exists to check instead
            boolean exits = operations.existsFile(file.getAbsoluteFilePath());
            if (!exits) {
                deleted = true;
            } else {
                log.trace("File was not deleted at this attempt will try again in 1 sec.: {}", file);
                // sleep a bit and try again
                Thread.sleep(1000);
            }
        }
        if (!deleted) {
            throw new GenericFileOperationFailedException("Cannot delete file: " + file);
        }
    } finally {
        // must release lock last
        if (!releaseEager && exclusiveReadLockStrategy != null) {
            exclusiveReadLockStrategy.releaseExclusiveReadLockOnCommit(operations, file, exchange);
        }
    }
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) GenericFileEndpoint(org.apache.camel.component.file.GenericFileEndpoint)

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