From f18626fdce389ce3acd6015fbfec3d025bc3a02e Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 8 May 2024 15:48:08 -0400 Subject: [PATCH] GH-9129: SFTP: Remove extra `/` for the `file_remoteDirectory` header Fixes: #9129 If `.remoteDirectory("/sftpSource")`, then `FileHeaders.REMOTE_DIRECTORY` is `//sftpSource` * Fix `AbstractInboundFileSynchronizer` to not add `/` if one is already present in the beginning of the `remoteDirectoryPath` (cherry picked from commit d87a7098ca3843665818f3fa83d24520b16eacbd) --- .../remote/synchronizer/AbstractInboundFileSynchronizer.java | 5 ++++- .../org/springframework/integration/sftp/dsl/SftpTests.java | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/synchronizer/AbstractInboundFileSynchronizer.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/synchronizer/AbstractInboundFileSynchronizer.java index 0c285ee109..285740e664 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/synchronizer/AbstractInboundFileSynchronizer.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/synchronizer/AbstractInboundFileSynchronizer.java @@ -505,7 +505,10 @@ public abstract class AbstractInboundFileSynchronizer try { String remoteFileMetadata = new URI(protocol(), null, host, Integer.parseInt(port), - '/' + remoteDirectoryPath, null, remoteFileName) + remoteDirectoryPath.charAt(0) == '/' + ? remoteDirectoryPath : + '/' + remoteDirectoryPath, + null, remoteFileName) .toString(); this.remoteFileMetadataStore.put(buildMetadataKey(localFile), remoteFileMetadata); } diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/dsl/SftpTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/dsl/SftpTests.java index 9828fe7ad4..d39d147971 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/dsl/SftpTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/dsl/SftpTests.java @@ -75,7 +75,7 @@ public class SftpTests extends SftpTestSupport { IntegrationFlow flow = IntegrationFlow .from(Sftp.inboundAdapter(sessionFactory()) .preserveTimestamp(true) - .remoteDirectory("sftpSource") + .remoteDirectory("/sftpSource") .regexFilter(".*\\.txt$") .localFilenameExpression("#this.toUpperCase() + '.a'") .localDirectory(getTargetLocalDirectory()) @@ -91,6 +91,7 @@ public class SftpTests extends SftpTestSupport { File file = (File) payload; assertThat(file.getName()).isEqualTo(" SFTPSOURCE1.TXT.a"); assertThat(file.getAbsolutePath()).contains("localTarget"); + assertThat(message.getHeaders()).containsEntry(FileHeaders.REMOTE_DIRECTORY, "/sftpSource"); message = out.receive(10_000); assertThat(message).isNotNull();