List of usage examples for org.springframework.integration.file.support FileExistsMode APPEND
FileExistsMode APPEND
To view the source code for org.springframework.integration.file.support FileExistsMode APPEND.
Click Source Link
From source file:org.springframework.integration.file.FileWritingMessageHandler.java
/** * Will set the {@link FileExistsMode} that specifies what will happen in * case the destination exists. For example {@link FileExistsMode#APPEND} * instructs this handler to append data to the existing file rather then * creating a new file for each {@link Message}. * * If set to {@link FileExistsMode#APPEND}, the adapter will also * create a real instance of the {@link LockRegistry} to ensure that there * is no collisions when multiple threads are writing to the same file. * * Otherwise the LockRegistry is set to {@link PassThruLockRegistry} which * has no effect./*from ww w.j av a 2 s . co m*/ * * @param fileExistsMode Must not be null */ public void setFileExistsMode(FileExistsMode fileExistsMode) { Assert.notNull(fileExistsMode, "'fileExistsMode' must not be null."); this.fileExistsMode = fileExistsMode; if (FileExistsMode.APPEND.equals(fileExistsMode)) { this.lockRegistry = this.lockRegistry instanceof PassThruLockRegistry ? new DefaultLockRegistry() : this.lockRegistry; } }
From source file:org.springframework.integration.file.FileWritingMessageHandler.java
private void validateDestinationDirectory(File destinationDirectory, boolean autoCreateDirectory) { if (!destinationDirectory.exists() && autoCreateDirectory) { destinationDirectory.mkdirs();// w w w .ja v a2s .c o m } Assert.isTrue(destinationDirectory.exists(), "Destination directory [" + destinationDirectory + "] does not exist."); Assert.isTrue(destinationDirectory.isDirectory(), "Destination path [" + destinationDirectory + "] does not point to a directory."); Assert.isTrue(destinationDirectory.canWrite(), "Destination directory [" + destinationDirectory + "] is not writable."); Assert.state(!(this.temporaryFileSuffixSet && FileExistsMode.APPEND.equals(this.fileExistsMode)), "'temporaryFileSuffix' can not be set when appending to an existing file"); }
From source file:org.springframework.integration.file.FileWritingMessageHandler.java
private File handleFileMessage(final File sourceFile, File tempFile, final File resultFile) throws IOException { if (FileExistsMode.APPEND.equals(this.fileExistsMode)) { File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile); final FileOutputStream fos = new FileOutputStream(fileToWriteTo, true); final FileInputStream fis = new FileInputStream(sourceFile); WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry, fileToWriteTo.getAbsolutePath()) { @Override/*from w w w. j av a 2s. c o m*/ protected void whileLocked() throws IOException { FileCopyUtils.copy(fis, fos); } }; whileLockedProcessor.doWhileLocked(); this.cleanUpAfterCopy(fileToWriteTo, resultFile, sourceFile); return resultFile; } else { if (this.deleteSourceFiles) { if (sourceFile.renameTo(resultFile)) { return resultFile; } if (logger.isInfoEnabled()) { logger.info(String.format("Failed to move file '%s'. Using copy and delete fallback.", sourceFile.getAbsolutePath())); } } FileCopyUtils.copy(sourceFile, tempFile); this.cleanUpAfterCopy(tempFile, resultFile, sourceFile); return resultFile; } }
From source file:org.springframework.integration.file.FileWritingMessageHandler.java
private File handleByteArrayMessage(final byte[] bytes, File originalFile, File tempFile, final File resultFile) throws IOException { File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile); final boolean append = FileExistsMode.APPEND.equals(this.fileExistsMode); final FileOutputStream fos = new FileOutputStream(fileToWriteTo, append); WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry, fileToWriteTo.getAbsolutePath()) { @Override//from www .ja v a2s .c om protected void whileLocked() throws IOException { FileCopyUtils.copy(bytes, fos); } }; whileLockedProcessor.doWhileLocked(); this.cleanUpAfterCopy(fileToWriteTo, resultFile, originalFile); return resultFile; }
From source file:org.springframework.integration.file.FileWritingMessageHandler.java
private File handleStringMessage(final String content, File originalFile, File tempFile, final File resultFile) throws IOException { File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile); final boolean append = FileExistsMode.APPEND.equals(this.fileExistsMode); final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(fileToWriteTo, append), this.charset); WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry, fileToWriteTo.getAbsolutePath()) { @Override/*from w w w . j a v a2 s .c om*/ protected void whileLocked() throws IOException { FileCopyUtils.copy(content, writer); } }; whileLockedProcessor.doWhileLocked(); this.cleanUpAfterCopy(fileToWriteTo, resultFile, originalFile); return resultFile; }
From source file:org.springframework.integration.file.FileWritingMessageHandler.java
private void cleanUpAfterCopy(File fileToWriteTo, File resultFile, File originalFile) throws IOException { if (!FileExistsMode.APPEND.equals(this.fileExistsMode) && StringUtils.hasText(this.temporaryFileSuffix)) { this.renameTo(fileToWriteTo, resultFile); }//from www .j a va 2 s.co m if (this.deleteSourceFiles && originalFile != null) { originalFile.delete(); } }
From source file:org.springframework.integration.file.remote.RemoteFileTemplate.java
@Override public String append(final Message<?> message, String subDirectory) { return send(message, subDirectory, FileExistsMode.APPEND); }
From source file:org.springframework.integration.file.remote.RemoteFileTemplate.java
private String send(final Message<?> message, final String subDirectory, final FileExistsMode mode) { Assert.notNull(this.directoryExpressionProcessor, "'remoteDirectoryExpression' is required"); Assert.isTrue(!FileExistsMode.APPEND.equals(mode) || !this.useTemporaryFileName, "Cannot append when using a temporary file name"); Assert.isTrue(!FileExistsMode.REPLACE_IF_MODIFIED.equals(mode), "FilExistsMode.REPLACE_IF_MODIFIED can only be used for local files"); final StreamHolder inputStreamHolder = this.payloadToInputStream(message); if (inputStreamHolder != null) { try {// w w w . j a va 2s . c om return this.execute(session -> { String fileName = inputStreamHolder.getName(); try { String remoteDirectory = RemoteFileTemplate.this.directoryExpressionProcessor .processMessage(message); remoteDirectory = RemoteFileTemplate.this.normalizeDirectoryPath(remoteDirectory); if (StringUtils.hasText(subDirectory)) { if (subDirectory.startsWith(RemoteFileTemplate.this.remoteFileSeparator)) { remoteDirectory += subDirectory.substring(1); } else { remoteDirectory += RemoteFileTemplate.this.normalizeDirectoryPath(subDirectory); } } String temporaryRemoteDirectory = remoteDirectory; if (RemoteFileTemplate.this.temporaryDirectoryExpressionProcessor != null) { temporaryRemoteDirectory = RemoteFileTemplate.this.temporaryDirectoryExpressionProcessor .processMessage(message); } fileName = RemoteFileTemplate.this.fileNameGenerator.generateFileName(message); RemoteFileTemplate.this.sendFileToRemoteDirectory(inputStreamHolder.getStream(), temporaryRemoteDirectory, remoteDirectory, fileName, session, mode); return remoteDirectory + fileName; } catch (FileNotFoundException e) { throw new MessageDeliveryException(message, "File [" + inputStreamHolder.getName() + "] not found in local working directory; it was moved or deleted unexpectedly.", e); } catch (IOException e) { throw new MessageDeliveryException(message, "Failed to transfer file [" + inputStreamHolder.getName() + " -> " + fileName + "] from local directory to remote directory.", e); } catch (Exception e) { throw new MessageDeliveryException(message, "Error handling message for file [" + inputStreamHolder.getName() + " -> " + fileName + "]", e); } }); } finally { try { inputStreamHolder.getStream().close(); } catch (IOException e) { } } } else { // A null holder means a File payload that does not exist. if (this.logger.isWarnEnabled()) { this.logger.warn("File " + message.getPayload() + " does not exist"); } return null; } }
From source file:org.springframework.integration.file.remote.RemoteFileTemplate.java
private void sendFileToRemoteDirectory(InputStream inputStream, String temporaryRemoteDirectory, String remoteDirectory, String fileName, Session<F> session, FileExistsMode mode) throws IOException { remoteDirectory = this.normalizeDirectoryPath(remoteDirectory); temporaryRemoteDirectory = this.normalizeDirectoryPath(temporaryRemoteDirectory); String remoteFilePath = remoteDirectory + fileName; String tempRemoteFilePath = temporaryRemoteDirectory + fileName; // write remote file first with temporary file extension if enabled String tempFilePath = tempRemoteFilePath + (this.useTemporaryFileName ? this.temporaryFileSuffix : ""); if (this.autoCreateDirectory) { try {/*from w w w.ja va 2s .c om*/ RemoteFileUtils.makeDirectories(remoteDirectory, session, this.remoteFileSeparator, this.logger); } catch (IllegalStateException e) { // Revert to old FTP behavior if recursive mkdir fails, for backwards compatibility session.mkdir(remoteDirectory); } } try { boolean rename = this.useTemporaryFileName; if (FileExistsMode.REPLACE.equals(mode)) { session.write(inputStream, tempFilePath); } else if (FileExistsMode.APPEND.equals(mode)) { session.append(inputStream, tempFilePath); } else { if (exists(remoteFilePath)) { if (FileExistsMode.FAIL.equals(mode)) { throw new MessagingException( "The destination file already exists at '" + remoteFilePath + "'."); } else { if (this.logger.isDebugEnabled()) { this.logger.debug("File not transferred to '" + remoteFilePath + "'; already exists."); } } rename = false; } else { session.write(inputStream, tempFilePath); } } // then rename it to its final name if necessary if (rename) { session.rename(tempFilePath, remoteFilePath); } } catch (Exception e) { throw new MessagingException("Failed to write to '" + tempFilePath + "' while uploading the file", e); } finally { inputStream.close(); } }