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

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

Introduction

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

Prototype

FileExistsMode REPLACE

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

Click Source Link

Document

If the file already exists, replace it.

Usage

From source file:com.qpark.eip.core.sftp.SftpGatewayImpl.java

/**
 * @see com.qpark.eip.core.sftp.SftpGateway#save(java.io.File,
 *      java.lang.String)/*w w w.j a  v a 2  s.  c o m*/
 */
@Override
public boolean save(final File file, final String remoteDirectory) throws Exception {
    boolean success = false;
    final Map<String, Object> headers = new HashMap<>();
    headers.put("directory", "");
    final Message<File> message = MessageBuilder.createMessage(file, new MessageHeaders(headers));
    if (!this.template.exists(remoteDirectory)) {
        this.mkdir(remoteDirectory);
    }
    this.logger.debug("save {} {}", remoteDirectory, file.getName());
    final String remotePath = this.template.send(message, remoteDirectory, FileExistsMode.REPLACE);
    if (remotePath != null) {
        success = true;
    }
    return success;
}

From source file:org.springframework.integration.file.remote.RemoteFileTemplate.java

@Override
public String send(final Message<?> message, String subDirectory, FileExistsMode... mode) {
    FileExistsMode modeToUse = mode == null || mode.length < 1 || mode[0] == null ? FileExistsMode.REPLACE
            : mode[0];/*from w  ww.  j ava2s.com*/
    return send(message, subDirectory, modeToUse);
}

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 ww  w  .j  a  v  a  2  s  . co  m*/
            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();
    }
}