Example usage for org.springframework.integration.file.support FileExistsMode REPLACE_IF_MODIFIED

List of usage examples for org.springframework.integration.file.support FileExistsMode REPLACE_IF_MODIFIED

Introduction

In this page you can find the example usage for org.springframework.integration.file.support FileExistsMode REPLACE_IF_MODIFIED.

Prototype

FileExistsMode REPLACE_IF_MODIFIED

To view the source code for org.springframework.integration.file.support FileExistsMode REPLACE_IF_MODIFIED.

Click Source Link

Document

If the file already exists, replace it only if the last modified time is different.

Usage

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void replaceIfDifferent() throws IOException {
    QueueChannel output = new QueueChannel();
    this.handler.setOutputChannel(output);
    this.handler.setPreserveTimestamp(true);
    this.handler.setFileExistsMode(FileExistsMode.REPLACE_IF_MODIFIED);
    this.handler.handleMessage(
            MessageBuilder.withPayload("foo").setHeader(FileHeaders.FILENAME, "replaceIfDifferent.txt")
                    .setHeader(FileHeaders.SET_MODIFIED, 42_000_000).build());
    Message<?> result = output.receive(0);
    assertFileContentIs(result, "foo");
    assertLastModifiedIs(result, 42_000_000);
    this.handler.handleMessage(
            MessageBuilder.withPayload("bar").setHeader(FileHeaders.FILENAME, "replaceIfDifferent.txt")
                    .setHeader(FileHeaders.SET_MODIFIED, 42_000_000).build());
    result = output.receive(0);//from   ww w.j  a  v  a  2 s . c o  m
    assertFileContentIs(result, "foo"); // no overwrite - timestamp same
    assertLastModifiedIs(result, 42_000_000);
    this.handler.handleMessage(
            MessageBuilder.withPayload("bar").setHeader(FileHeaders.FILENAME, "replaceIfDifferent.txt")
                    .setHeader(FileHeaders.SET_MODIFIED, 43_000_000).build());
    result = output.receive(0);
    assertFileContentIs(result, "bar");
    assertLastModifiedIs(result, 43_000_000);
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void replaceIfDifferentFile() throws IOException {
    File file = new File(this.temp.newFolder(), "foo.txt");
    FileCopyUtils.copy("foo".getBytes(), new FileOutputStream(file));
    file.setLastModified(42_000_000);/*from   w  ww  . java 2s. c  o  m*/
    QueueChannel output = new QueueChannel();
    this.handler.setOutputChannel(output);
    this.handler.setPreserveTimestamp(true);
    this.handler.setFileExistsMode(FileExistsMode.REPLACE_IF_MODIFIED);
    this.handler.handleMessage(MessageBuilder.withPayload(file).build());
    Message<?> result = output.receive(0);
    assertFileContentIs(result, "foo");
    assertLastModifiedIs(result, 42_000_000);
    FileCopyUtils.copy("bar".getBytes(), new FileOutputStream(file));
    file.setLastModified(42_000_000);
    this.handler.handleMessage(MessageBuilder.withPayload(file).build());
    result = output.receive(0);
    assertFileContentIs(result, "foo"); // no overwrite - timestamp same
    assertLastModifiedIs(result, 42_000_000);
    file.setLastModified(43_000_000);
    this.handler.handleMessage(MessageBuilder.withPayload(file).build());
    result = output.receive(0);
    assertFileContentIs(result, "bar");
    assertLastModifiedIs(result, 43_000_000);
}

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 {/*from  ww w.ja  va  2 s .  c  o m*/
            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;
    }
}