/** * Perform the work to process the fileExchange * * @param exchange fileExchange * @param target the target filename * @throws Exception is thrown if some error */ protected void processExchange(Exchange exchange, String target) throws Exception { log.trace("Processing file: {} for exchange: {}", target, exchange); try { preWriteCheck(); // should we write to a temporary name and then afterwards rename to real target boolean writeAsTempAndRename = ObjectHelper.isNotEmpty(endpoint.getTempFileName()); String tempTarget = null; // remember if target exists to avoid checking twice Boolean targetExists = null; if (writeAsTempAndRename) { // compute temporary name with the temp prefix tempTarget = createTempFileName(exchange, target); log.trace("Writing using tempNameFile: {}", tempTarget); // if we should eager delete target file before deploying temporary file if (endpoint.getFileExist() != GenericFileExist.TryRename && endpoint.isEagerDeleteTargetFile()) { // cater for file exists option on the real target as // the file operations code will work on the temp file // if an existing file already exists what should we do? targetExists = operations.existsFile(target); if (targetExists) { log.trace("EagerDeleteTargetFile, target exists"); if (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.", target); return; } else if (endpoint.getFileExist() == GenericFileExist.Fail) { throw new GenericFileOperationFailedException( "File already exist: " + target + ". Cannot write new file."); } else if (endpoint.isEagerDeleteTargetFile() && endpoint.getFileExist() == GenericFileExist.Override) { // we override the target so we do this by deleting it so the temp file can be renamed // later // with success as the existing target file have been deleted log.trace("Eagerly deleting existing file: {}", target); if (!operations.deleteFile(target)) { throw new GenericFileOperationFailedException("Cannot delete file: " + target); } } } } // delete any pre existing temp file if (operations.existsFile(tempTarget)) { log.trace("Deleting existing temp file: {}", tempTarget); if (!operations.deleteFile(tempTarget)) { throw new GenericFileOperationFailedException("Cannot delete file: " + tempTarget); } } } // write/upload the file writeFile(exchange, tempTarget != null ? tempTarget : target); // if we did write to a temporary name then rename it to the real // name after we have written the file if (tempTarget != null) { // if we did not eager delete the target file if (endpoint.getFileExist() != GenericFileExist.TryRename && !endpoint.isEagerDeleteTargetFile()) { // if an existing file already exists what should we do? targetExists = operations.existsFile(target); if (targetExists) { log.trace("Not using EagerDeleteTargetFile, target exists"); if (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.", target); return; } else if (endpoint.getFileExist() == GenericFileExist.Fail) { throw new GenericFileOperationFailedException( "File already exist: " + target + ". Cannot write new file."); } else if (endpoint.getFileExist() == GenericFileExist.Override) { // we override the target so we do this by deleting it so the temp file can be renamed // later // with success as the existing target file have been deleted log.trace("Deleting existing file: {}", target); if (!operations.deleteFile(target)) { throw new GenericFileOperationFailedException("Cannot delete file: " + target); } } } } // now we are ready to rename the temp file to the target file log.trace("Renaming file: [{}] to: [{}]", tempTarget, target); boolean renamed = operations.renameFile(tempTarget, target); if (!renamed) { throw new GenericFileOperationFailedException( "Cannot rename file from: " + tempTarget + " to: " + target); } } // any done file to write? if (endpoint.getDoneFileName() != null) { String doneFileName = endpoint.createDoneFileName(target); ObjectHelper.notEmpty(doneFileName, "doneFileName", endpoint); // create empty exchange with empty body to write as the done file Exchange empty = new DefaultExchange(exchange); empty.getIn().setBody(""); log.trace("Writing done file: [{}]", doneFileName); // delete any existing done file if (operations.existsFile(doneFileName)) { if (!operations.deleteFile(doneFileName)) { throw new GenericFileOperationFailedException( "Cannot delete existing done file: " + doneFileName); } } writeFile(empty, doneFileName); } // let's store the name we really used in the header, so end-users // can retrieve it exchange.getIn().setHeader(Exchange.FILE_NAME_PRODUCED, target); } catch (Exception e) { handleFailedWrite(exchange, e); } postWriteCheck(); }