From f2399a4e13ed2a66e5ff1d91e0da70bc00c57d41 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 24 Jun 2024 14:52:13 -0400 Subject: [PATCH] GH-9268: Fix regression in SMB Inbound for sub-dirs Fixes: #9268 The new `SmbSession.list()` behavior prevents to pull files from sub-folders in inbound channel adapter * Fix `SmbSession.list()` similar way to `SftpSession`: if `remoteFile.isFile()` then remove it as is. Otherwise, perform `smbDir.listFiles()` on that sub-dir * Modify `SmbTests.testSmbInboundFlow()` to poll files from the `smbSource/subSmbSource/` **Auto-cherry-pick to `6.3.x` & `6.2.x`** --- .../integration/smb/session/SmbSession.java | 20 ++++++++++--------- .../integration/smb/dsl/SmbTests.java | 6 +++--- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java index 6212d10dba..dc2df6b176 100644 --- a/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java +++ b/spring-integration-smb/src/main/java/org/springframework/integration/smb/session/SmbSession.java @@ -119,25 +119,26 @@ public class SmbSession implements Session { } /** - * Return the contents of the specified SMB resource as an array of SmbFile objects. + * Return the content of the specified SMB resource as an array of SmbFile objects. * In case the remote resource does not exist, an empty array is returned. - * @param remotePath path to a remote directory or remote file path + * @param path path to a remote directory or remote file path * @return array of SmbFile objects - * @throws IOException on error conditions returned by a CIFS server or if the remote resource is not a directory. + * @throws IOException on error conditions returned by a CIFS server. */ @Override - public SmbFile[] list(String remotePath) throws IOException { - SmbFile[] files; + public SmbFile[] list(String path) throws IOException { + String remotePath = StringUtils.trimTrailingCharacter(path, '/'); + SmbFile[] files = null; int lastIndex = StringUtils.hasText(remotePath) ? remotePath.lastIndexOf('/') : 0; String remoteFileName = lastIndex > 0 ? remotePath.substring(lastIndex + 1) : null; if (StringUtils.hasText(remoteFileName)) { SmbFile remoteFile = createSmbFileObject(remotePath); - if (!remoteFile.isFile()) { - throw new IOException("[" + remotePath + "] is not a file."); + if (remoteFile.isFile()) { + files = new SmbFile[] {remoteFile}; } - files = new SmbFile[] {remoteFile}; } - else { + + if (files == null) { try { SmbFile smbDir = createSmbDirectoryObject(remotePath); if (!smbDir.exists()) { @@ -154,6 +155,7 @@ public class SmbSession implements Session { throw new IOException("Failed to list in [" + remotePath + "].", _ex); } } + logListedFiles(remotePath, files); return files; diff --git a/spring-integration-smb/src/test/java/org/springframework/integration/smb/dsl/SmbTests.java b/spring-integration-smb/src/test/java/org/springframework/integration/smb/dsl/SmbTests.java index d1ea9ea548..846baff286 100644 --- a/spring-integration-smb/src/test/java/org/springframework/integration/smb/dsl/SmbTests.java +++ b/spring-integration-smb/src/test/java/org/springframework/integration/smb/dsl/SmbTests.java @@ -92,7 +92,7 @@ public class SmbTests extends SmbTestSupport { DirectoryScanner scanner = new DefaultDirectoryScanner(); IntegrationFlow flow = IntegrationFlow.from(Smb.inboundAdapter(sessionFactory()) .preserveTimestamp(true) - .remoteDirectory("smbSource") + .remoteDirectory("smbSource/subSmbSource/") .maxFetchSize(10) .scanner(scanner) .regexFilter(".*\\.txt$") @@ -115,13 +115,13 @@ public class SmbTests extends SmbTestSupport { Object payload = message.getPayload(); assertThat(payload).isInstanceOf(File.class); File file = (File) payload; - assertThat(file.getName()).isIn("SMBSOURCE1.TXT.a", "SMBSOURCE2.TXT.a"); + assertThat(file.getName()).isEqualTo("SUBSMBSOURCE1.TXT.a", "SUBSMBSOURCE2.TXT.a"); assertThat(file.getAbsolutePath()).contains("localTarget"); message = out.receive(10_000); assertThat(message).isNotNull(); file = (File) message.getPayload(); - assertThat(file.getName()).isIn("SMBSOURCE1.TXT.a", "SMBSOURCE2.TXT.a"); + assertThat(file.getName()).isIn("SUBSMBSOURCE1.TXT.a", "SUBSMBSOURCE2.TXT.a"); assertThat(file.getAbsolutePath()).contains("localTarget"); assertThat(out.receive(10)).isNull();