Skip to content

Commit

Permalink
GH-9268: Fix regression in SMB Inbound for sub-dirs
Browse files Browse the repository at this point in the history
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/`

(cherry picked from commit f2399a4)
  • Loading branch information
artembilan authored and spring-builds committed Jun 24, 2024
1 parent 23745aa commit 841a9f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,26 @@ public boolean remove(String _path) throws IOException {
}

/**
* 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()) {
Expand All @@ -154,6 +155,7 @@ else if (!smbDir.isDirectory()) {
throw new IOException("Failed to list in [" + remotePath + "].", _ex);
}
}

logListedFiles(remotePath, files);

return files;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void testSmbInboundFlow() {
DirectoryScanner scanner = new DefaultDirectoryScanner();
IntegrationFlow flow = IntegrationFlow.from(Smb.inboundAdapter(sessionFactory())
.preserveTimestamp(true)
.remoteDirectory("smbSource")
.remoteDirectory("smbSource/subSmbSource/")
.maxFetchSize(10)
.scanner(scanner)
.regexFilter(".*\\.txt$")
Expand All @@ -115,13 +115,13 @@ public void testSmbInboundFlow() {
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();
Expand Down

0 comments on commit 841a9f5

Please sign in to comment.