Uploaded image for project: 'Spring Integration'
  1. Spring Integration
  2. INT-3773

int-sftp:outbound-channel-adapter fails to write remote file if mode is FAIL or IGNORE, even when remote file doesn't exist

    XMLWordPrintable

    Details

      Description

      It looks like the problem is a missing else to match the if statement in RemoteFileTemplate at line 511 of v4.1.6. If the mode if FAIL or IGNORE and the file doesn't exist on the remote SFTP server, then write() is never called:

      		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 (logger.isDebugEnabled()) {
      							logger.debug("File not transferred to '" + remoteFilePath + "'; already exists.");
      						}
      					}
      					rename = false;
      				}
      			}
      			// then rename it to its final name if necessary
      			if (rename) {
      			   session.rename(tempFilePath, remoteFilePath);
      			}
      		}
      
      	<int-sftp:outbound-channel-adapter id="sftpOutboundChannelAdapter"
      		session-factory="sftpSessionFactory"
      		channel="outboundRequestChannel"
      		remote-directory="./data"
      		remote-filename-generator-expression="'file-'.concat(headers.timestamp) + '.msg'"
      		use-temporary-file-name="true"
      		mode="FAIL"/>
      

      In the SFTP server logs, I can see the check to see if a remote file exists with the same name, and then later the move fails because the new file wasn't written:

      Jul 15 16:29:23 host1 sftp-server[31920]: received client version 3
      Jul 15 16:29:35 host1 sftp-server[31939]: session opened for local user andy from [127.0.0.1]
      Jul 15 16:29:35 host1 sftp-server[31939]: received client version 3
      Jul 15 16:29:35 host1 sftp-server[31939]: realpath "."
      Jul 15 16:29:35 host1 sftp-server[31939]: debug1: request 1: sent names count 1
      Jul 15 16:29:35 host1 sftp-server[31939]: stat name "/home/andy/./data/file-1436974162485.msg"
      Jul 15 16:29:35 host1 sftp-server[31939]: sent status No such file
      Jul 15 16:29:35 host1 sftp-server[31939]: debug1: read eof
      Jul 15 16:29:35 host1 sftp-server[31939]: session closed for local user andy from [127.0.0.1]
      Jul 15 16:33:33 host1 sftp-server[31920]: realpath "."
      Jul 15 16:33:33 host1 sftp-server[31920]: debug1: request 1: sent names count 1
      Jul 15 16:33:33 host1 sftp-server[31920]: posix-rename old "/home/andy/./data/file-1436974162485.msg.writing" new "/home/andy/./data/file-1436974162485.msg"
      Jul 15 16:33:33 host1 sftp-server[31920]: sent status No such file
      Jul 15 16:33:33 host1 sftp-server[31920]: remove name "/home/andy/./data/file-1436974162485.msg"
      Jul 15 16:33:33 host1 sftp-server[31920]: sent status No such file
      Jul 15 16:33:33 host1 sftp-server[31920]: debug1: read eof
      

        Attachments

          Activity

            People

            Assignee:
            grussell Gary Russell
            Reporter:
            andypeel Andy Peel
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: