List of usage examples for org.springframework.integration.file.support FileExistsMode IGNORE
FileExistsMode IGNORE
To view the source code for org.springframework.integration.file.support FileExistsMode IGNORE.
Click Source Link
From source file:org.springframework.integration.file.FileWritingMessageHandler.java
@Override protected Object handleRequestMessage(Message<?> requestMessage) { Assert.notNull(requestMessage, "message must not be null"); Object payload = requestMessage.getPayload(); Assert.notNull(payload, "message payload must not be null"); String generatedFileName = this.fileNameGenerator.generateFileName(requestMessage); File originalFileFromHeader = this.retrieveOriginalFileFromHeader(requestMessage); final File destinationDirectoryToUse = evaluateDestinationDirectoryExpression(requestMessage); File tempFile = new File(destinationDirectoryToUse, generatedFileName + temporaryFileSuffix); File resultFile = new File(destinationDirectoryToUse, generatedFileName); if (FileExistsMode.FAIL.equals(this.fileExistsMode) && resultFile.exists()) { throw new MessageHandlingException(requestMessage, "The destination file already exists at '" + resultFile.getAbsolutePath() + "'."); }/*from w w w . ja v a 2 s . com*/ final boolean ignore = FileExistsMode.IGNORE.equals(this.fileExistsMode) && (resultFile.exists() || (StringUtils.hasText(this.temporaryFileSuffix) && tempFile.exists())); if (!ignore) { try { if (payload instanceof File) { resultFile = this.handleFileMessage((File) payload, tempFile, resultFile); } else if (payload instanceof byte[]) { resultFile = this.handleByteArrayMessage((byte[]) payload, originalFileFromHeader, tempFile, resultFile); } else if (payload instanceof String) { resultFile = this.handleStringMessage((String) payload, originalFileFromHeader, tempFile, resultFile); } else { throw new IllegalArgumentException( "unsupported Message payload type [" + payload.getClass().getName() + "]"); } } catch (Exception e) { throw new MessageHandlingException(requestMessage, "failed to write Message payload to file", e); } } if (!this.expectReply) { return null; } if (resultFile != null) { if (originalFileFromHeader == null && payload instanceof File) { return MessageBuilder.withPayload(resultFile).setHeader(FileHeaders.ORIGINAL_FILE, payload); } } return resultFile; }
From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java
@Test public void existingFileIgnored() throws Exception { Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT).build(); QueueChannel output = new QueueChannel(); File outFile = temp.newFile("/outputDirectory/" + message.getHeaders().getId().toString() + ".msg"); FileCopyUtils.copy("foo".getBytes(), new FileOutputStream(outFile)); handler.setCharset(DEFAULT_ENCODING); handler.setOutputChannel(output);/*from w w w . j a va 2s . co m*/ handler.setFileExistsMode(FileExistsMode.IGNORE); handler.handleMessage(message); Message<?> result = output.receive(0); assertFileContentIs(result, "foo"); }
From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java
@Test public void existingWritingFileIgnored() throws Exception { Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT).build(); QueueChannel output = new QueueChannel(); File outFile = temp.newFile("/outputDirectory/" + message.getHeaders().getId().toString() + ".msg.writing"); FileCopyUtils.copy("foo".getBytes(), new FileOutputStream(outFile)); handler.setCharset(DEFAULT_ENCODING); handler.setOutputChannel(output);//from w w w . j a va 2 s .c om handler.setFileExistsMode(FileExistsMode.IGNORE); handler.handleMessage(message); Message<?> result = output.receive(0); File destFile = (File) result.getPayload(); assertNotSame(destFile, sourceFile); assertThat(destFile.exists(), is(false)); assertThat(outFile.exists(), is(true)); }
From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java
@Test public void existingWritingFileNotIgnoredIfEmptySuffix() throws Exception { Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT).build(); QueueChannel output = new QueueChannel(); File outFile = temp.newFile("/outputDirectory/" + message.getHeaders().getId().toString() + ".msg.writing"); FileCopyUtils.copy("foo".getBytes(), new FileOutputStream(outFile)); handler.setCharset(DEFAULT_ENCODING); handler.setOutputChannel(output);/*from ww w . j av a 2 s .co m*/ handler.setFileExistsMode(FileExistsMode.IGNORE); handler.setTemporaryFileSuffix(""); handler.handleMessage(message); Message<?> result = output.receive(0); File destFile = (File) result.getPayload(); assertNotSame(destFile, sourceFile); assertFileContentIsMatching(result); assertThat(outFile.exists(), is(true)); assertFileContentIs(outFile, "foo"); }