From 57c98e16117e64479fc884bab94299acae283961 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 14 Aug 2024 15:56:22 -0400 Subject: [PATCH] GH-9380: Add `DefaultSftpSessionFactory.setSshClientConfigurer()` Fixes: #9380 Expose a `Consumer sshClientConfigurer` option for the `DefaultSftpSessionFactory` to further customize an internal `SshClient` instance. --- .../session/DefaultSftpSessionFactory.java | 24 +++++++++++++++++-- .../sftp/session/SftpSessionFactoryTests.java | 10 +++++++- .../ROOT/pages/sftp/session-factory.adoc | 10 ++++++++ .../antora/modules/ROOT/pages/whats-new.adoc | 8 ++++++- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java index c38c1ddfc0..d32645182a 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java @@ -24,6 +24,7 @@ import java.time.Duration; import java.util.Collection; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Consumer; import org.apache.sshd.client.SshClient; import org.apache.sshd.client.auth.keyboard.UserInteraction; @@ -79,7 +80,8 @@ import org.springframework.util.Assert; * * @since 2.0 */ -public class DefaultSftpSessionFactory implements SessionFactory, SharedSessionCapable, DisposableBean { +public class DefaultSftpSessionFactory + implements SessionFactory, SharedSessionCapable, DisposableBean { private final Lock lock = new ReentrantLock(); @@ -119,6 +121,9 @@ public class DefaultSftpSessionFactory implements SessionFactory sshClientConfigurer = (sshClient) -> { + }; + public DefaultSftpSessionFactory() { this(false); } @@ -283,6 +288,20 @@ public class DefaultSftpSessionFactory implements SessionFactory sshClientConfigurer) { + Assert.state(this.isInnerClient, "Cannot mutate externally provided SshClient"); + Assert.notNull(sshClientConfigurer, "'sshClientConfigurer' must noy be null"); + this.sshClientConfigurer = sshClientConfigurer; + } + @Override public SftpSession getSession() { SftpSession sftpSession; @@ -392,6 +411,7 @@ public class DefaultSftpSessionFactory implements SessionFactory true); server.setPort(0); @@ -221,10 +223,15 @@ public class SftpSessionFactoryTests { sftpSessionFactory.setPassword("pass"); sftpSessionFactory.setAllowUnknownKeys(true); sftpSessionFactory.setTimeout(15_000); + sftpSessionFactory.setSshClientConfigurer((sshClient) -> { + sshClient.setNioWorkers(27); + PropertyResolverUtils.updateProperty(sshClient, CoreModuleProperties.MAX_PACKET_SIZE.getName(), 48 * 1024); + }); ClientChannel clientChannel = sftpSessionFactory.getSession().getClientInstance().getClientChannel(); assertThat(AbstractSftpClient.SFTP_CLIENT_CMD_TIMEOUT.getRequired(clientChannel)).hasSeconds(15); + assertThat(CoreModuleProperties.MAX_PACKET_SIZE.getRequired(clientChannel)).isEqualTo(48 * 1024); sftpSessionFactory.destroy(); } @@ -260,4 +267,5 @@ public class SftpSessionFactoryTests { sftpSessionFactory.destroy(); } } + } diff --git a/src/reference/antora/modules/ROOT/pages/sftp/session-factory.adoc b/src/reference/antora/modules/ROOT/pages/sftp/session-factory.adoc index 2e11d8cc0a..16c3a42d30 100644 --- a/src/reference/antora/modules/ROOT/pages/sftp/session-factory.adoc +++ b/src/reference/antora/modules/ROOT/pages/sftp/session-factory.adoc @@ -107,3 +107,13 @@ If `false`, a pre-populated `knownHosts` file is required. `userInteraction`::A custom `org.apache.sshd.client.auth.keyboard.UserInteraction` to be used during authentication. +Starting with version 6.4, the `DefaultSftpSessionFactory` expose a `Consumer` configurer property to further customize an internal `SshClient`. +For example, this is how to change a default number of NIO workers and packet size for the client: + +[source, java] +---- +sftpSessionFactory.setSshClientConfigurer((sshClient) -> { + sshClient.setNioWorkers(27); + PropertyResolverUtils.updateProperty(sshClient, CoreModuleProperties.MAX_PACKET_SIZE.getName(), 48 * 1024); +}); +---- diff --git a/src/reference/antora/modules/ROOT/pages/whats-new.adoc b/src/reference/antora/modules/ROOT/pages/whats-new.adoc index 0df8de265d..691b6a35b1 100644 --- a/src/reference/antora/modules/ROOT/pages/whats-new.adoc +++ b/src/reference/antora/modules/ROOT/pages/whats-new.adoc @@ -59,4 +59,10 @@ See xref:redis.adoc[Redis Support] for more information. === Groovy Changes The `ControlBusFactoryBean` (and respective `` XML tag) has been deprecated (for removal) in favor of new introduced `ControlBusFactoryBean` based on a new model implemented in the `ControlBusCommandRegistry`. -See xref:control-bus.adoc[Control Bus] for more information. \ No newline at end of file +See xref:control-bus.adoc[Control Bus] for more information. + +[[x6.4-sftp-changes]] +=== SFTP Support Changes + +The `DefaultSftpSessionFactory` now exposes a `Consumer` configurer property to further customize an internal `SshClient`. +See xref:sftp/session-factory.adoc[SFTP Session Factory] for more information. \ No newline at end of file