diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java index d3d66aab99d3a..8ad2a4bbdfd5f 100644 --- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java +++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java @@ -48,7 +48,7 @@ * FTP remote file operations */ public class FtpOperations implements RemoteFileOperations { - + protected final transient Logger log = LoggerFactory.getLogger(getClass()); protected final FTPClient client; protected final FTPClientConfig clientConfig; @@ -218,11 +218,39 @@ public boolean deleteFile(String name) throws GenericFileOperationFailedExceptio if (log.isDebugEnabled()) { log.debug("Deleting file: " + name); } + + boolean result; + String target = name; + String currentDir = null; + try { - return this.client.deleteFile(name); + 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 + 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; } public boolean renameFile(String from, String to) throws GenericFileOperationFailedException { @@ -330,7 +358,7 @@ private boolean retrieveFileToStreamInBody(String name, Exchange exchange) throw @SuppressWarnings("unchecked") private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exchange) throws GenericFileOperationFailedException { - File temp; + File temp; File local = new File(FileUtil.normalizePath(endpoint.getLocalWorkDirectory())); OutputStream os; try { @@ -338,7 +366,7 @@ private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exc GenericFile target = (GenericFile) 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); @@ -354,7 +382,7 @@ private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exc if (local.exists()) { if (!FileUtil.deleteFile(local)) { throw new GenericFileOperationFailedException("Cannot delete existing local work file: " + local); - } + } } // create new temp local work file @@ -368,7 +396,7 @@ private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exc // set header with the path to the local work file exchange.getIn().setHeader(Exchange.FILE_LOCAL_WORK_PATH, local.getPath()); - } catch (Exception e) { + } catch (Exception e) { throw new GenericFileOperationFailedException("Cannot create new local work file: " + local); } @@ -395,7 +423,7 @@ private boolean retrieveFileToFileInLocalWorkDirectory(String name, Exchange exc } result = client.retrieveFile(remoteName, os); - + // change back to current directory if (endpoint.getConfiguration().isStepwise()) { changeCurrentDirectory(currentDir);