Skip to content

Commit 5ccfcbd

Browse files
committed
Fix SftpSession for host:port resolution
The plain `SocketAddress.toString()` may resolve to `hostName/IP` pair which is not parsed properly via `URI.getHost()` downstream leaving `/IP` part for `uri.getPath()` request. In the end this may lead to wrong file name to be used in the SFTP logic * Fix `SftpSession.getHostPort()` to use `SshdSocketAddress` utility to resolve host and port properly from the provided `SocketAddress`
1 parent f7dd876 commit 5ccfcbd

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import java.io.InputStream;
2121
import java.io.OutputStream;
2222
import java.io.UncheckedIOException;
23+
import java.net.SocketAddress;
2324
import java.time.Duration;
2425
import java.util.stream.Stream;
2526
import java.util.stream.StreamSupport;
2627

28+
import org.apache.sshd.common.util.net.SshdSocketAddress;
2729
import org.apache.sshd.sftp.SftpModuleProperties;
2830
import org.apache.sshd.sftp.client.SftpClient;
2931
import org.apache.sshd.sftp.common.SftpConstants;
@@ -200,7 +202,8 @@ public SftpClient getClientInstance() {
200202

201203
@Override
202204
public String getHostPort() {
203-
return this.sftpClient.getSession().getConnectAddress().toString();
205+
SocketAddress connectAddress = this.sftpClient.getSession().getConnectAddress();
206+
return SshdSocketAddress.toAddressString(connectAddress) + ':' + SshdSocketAddress.toAddressPort(connectAddress);
204207
}
205208

206209
@Override

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/dsl/SftpTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void testSftpInboundStreamFlow() throws Exception {
123123
assertThat(message).isNotNull();
124124
assertThat(message.getPayload()).isInstanceOf(InputStream.class);
125125
assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE)).isIn(" sftpSource1.txt", "sftpSource2.txt");
126-
assertThat(message.getHeaders().get(FileHeaders.REMOTE_HOST_PORT, String.class)).contains("localhost");
126+
assertThat(message.getHeaders().get(FileHeaders.REMOTE_HOST_PORT, String.class)).contains("localhost:");
127127
((InputStream) message.getPayload()).close();
128128
new IntegrationMessageHeaderAccessor(message).getCloseableResource().close();
129129

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void testAllContents() {
119119
received = (Message<byte[]>) this.data.receive(10000);
120120
assertThat(received).isNotNull();
121121
assertThat(received.getHeaders().get(FileHeaders.REMOTE_FILE_INFO)).isInstanceOf(SftpFileInfo.class);
122-
assertThat(received.getHeaders().get(FileHeaders.REMOTE_HOST_PORT, String.class)).contains("localhost");
122+
assertThat(received.getHeaders().get(FileHeaders.REMOTE_HOST_PORT, String.class)).contains("localhost:");
123123
this.adapter.stop();
124124
}
125125

0 commit comments

Comments
 (0)