Skip to content

Commit cb5eafe

Browse files
artembilanspring-builds
authored andcommitted
GH-9123: SFTP: Use canonicalPath for read operation
Fixes: #9123 The `/` at the beginning of the remote dir path is not necessary when listing files, although it is necessary to download them The `SftpTemplate.get()` should work also with `remote-dir/MyFile.csv` as input. * Fix `SftpSession.readRaw()` to call `sftpClient.canonicalPath(source)` if the path does not start with a `/`. Something similar what is does * Delegate to `SftpSession.readRaw()` from the `SftpSession.read()` * Reuse `normalizePath()` for `doList()` (cherry picked from commit a2215af)
1 parent 340532a commit cb5eafe

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -107,23 +107,24 @@ public Stream<SftpClient.DirEntry> doList(String path) throws IOException {
107107
remoteDir = remotePath;
108108
}
109109
}
110-
remoteDir =
111-
!remoteDir.isEmpty() && remoteDir.charAt(0) == '/'
112-
? remoteDir
113-
: this.sftpClient.canonicalPath(remoteDir);
110+
remoteDir = normalizePath(remoteDir);
114111
return StreamSupport.stream(this.sftpClient.readDir(remoteDir).spliterator(), false)
115112
.filter((entry) -> !isPattern || PatternMatchUtils.simpleMatch(remoteFile, entry.getFilename()));
116113
}
117114

118115
@Override
119116
public void read(String source, OutputStream os) throws IOException {
120-
InputStream is = this.sftpClient.read(source);
117+
InputStream is = readRaw(source);
121118
FileCopyUtils.copy(is, os);
122119
}
123120

124121
@Override
125122
public InputStream readRaw(String source) throws IOException {
126-
return this.sftpClient.read(source);
123+
return this.sftpClient.read(normalizePath(source));
124+
}
125+
126+
private String normalizePath(String path) throws IOException {
127+
return !path.isEmpty() && path.charAt(0) == '/' ? path : this.sftpClient.canonicalPath(path);
127128
}
128129

129130
@Override

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizerTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -193,8 +193,11 @@ public SftpSession getSession() {
193193

194194
String[] files = new File("remote-test-dir").list();
195195
for (String fileName : files) {
196-
when(sftpClient.read("remote-test-dir/" + fileName))
197-
.thenReturn(new FileInputStream("remote-test-dir/" + fileName));
196+
String remoteFilePath = "remote-test-dir/" + fileName;
197+
when(sftpClient.canonicalPath(remoteFilePath))
198+
.thenReturn("/" + remoteFilePath);
199+
when(sftpClient.read("/" + remoteFilePath))
200+
.thenReturn(new FileInputStream(remoteFilePath));
198201
}
199202
when(sftpClient.readDir("/remote-test-dir")).thenReturn(this.sftpEntries);
200203

0 commit comments

Comments
 (0)