Skip to content

Commit 78b09ed

Browse files
committed
GH-8713: Add support for custom SftpClient
Fixes #8713 * Introduce `DefaultSftpSessionFactory.createSftpClient()` factory method which can be overridden for any custom `SftpClient` use-case * Add changes to the docs * Some code clean up **Cherry-pick to `6.1.x`** # Conflicts: # spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java # src/reference/antora/modules/ROOT/pages/sftp/session-factory.adoc # src/reference/antora/modules/ROOT/pages/whats-new.adoc
1 parent 5eda90b commit 78b09ed

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353

5454
/**
5555
* Factory for creating {@link SftpSession} instances.
56+
* <p>
57+
* The {@link #createSftpClient(ClientSession, SftpVersionSelector, SftpErrorDataHandler)}
58+
* can be overridden to provide a custom {@link SftpClient}.
59+
* The {@link ConcurrentSftpClient} is used by default.
5660
*
5761
* @author Josh Long
5862
* @author Mario Gray
@@ -64,6 +68,7 @@
6468
* @author Artem Bilan
6569
* @author Krzysztof Debski
6670
* @author Auke Zaaiman
71+
* @author Adama Sorho
6772
*
6873
* @since 2.0
6974
*/
@@ -279,9 +284,7 @@ public SftpSession getSession() {
279284
try {
280285
boolean freshSftpClient = false;
281286
if (sftpClient == null || !sftpClient.isOpen()) {
282-
sftpClient =
283-
new ConcurrentSftpClient(initClientSession(), this.sftpVersionSelector,
284-
SftpErrorDataHandler.EMPTY);
287+
sftpClient = createSftpClient(initClientSession(), this.sftpVersionSelector, SftpErrorDataHandler.EMPTY);
285288
freshSftpClient = true;
286289
}
287290
sftpSession = new SftpSession(sftpClient);
@@ -290,8 +293,8 @@ public SftpSession getSession() {
290293
this.sharedSftpClient = sftpClient;
291294
}
292295
}
293-
catch (Exception e) {
294-
throw new IllegalStateException("failed to create SFTP Session", e);
296+
catch (Exception ex) {
297+
throw new IllegalStateException("failed to create SFTP Session", ex);
295298
}
296299
finally {
297300
if (this.sharedSessionLock != null) {
@@ -390,15 +393,32 @@ public void resetSharedSession() {
390393
this.sharedSftpClient = null;
391394
}
392395

396+
/**
397+
* Can be overridden to provide a custom {@link SftpClient} to {@link #getSession()}.
398+
* @param clientSession the {@link ClientSession}
399+
* @param initialVersionSelector the initial {@link SftpVersionSelector}
400+
* @param errorDataHandler the {@link SftpErrorDataHandler} to handle incoming data
401+
* through the error stream.
402+
* @return {@link SftpClient}
403+
* @throws IOException if failed to initialize
404+
* @since 6.1.3
405+
*/
406+
protected SftpClient createSftpClient(
407+
ClientSession clientSession, SftpVersionSelector initialVersionSelector,
408+
SftpErrorDataHandler errorDataHandler) throws IOException {
409+
410+
return new ConcurrentSftpClient(clientSession, initialVersionSelector, errorDataHandler);
411+
}
412+
393413
/**
394414
* The {@link DefaultSftpClient} extension to lock the {@link #send(int, Buffer)}
395415
* for concurrent interaction.
396416
*/
397-
private static class ConcurrentSftpClient extends DefaultSftpClient {
417+
protected static class ConcurrentSftpClient extends DefaultSftpClient {
398418

399419
private final Lock sendLock = new ReentrantLock();
400420

401-
ConcurrentSftpClient(ClientSession clientSession, SftpVersionSelector initialVersionSelector,
421+
protected ConcurrentSftpClient(ClientSession clientSession, SftpVersionSelector initialVersionSelector,
402422
SftpErrorDataHandler errorDataHandler) throws IOException {
403423

404424
super(clientSession, initialVersionSelector, errorDataHandler);

0 commit comments

Comments
 (0)