Skip to content

Commit ea3e118

Browse files
authored
Remove request FILENAME header for MPUT (#8761)
Related to: https://stackoverflow.com/questions/77268009/how-to-use-sftp-spring-integration-mput-with-sftpoutboundgateway-when-file-objec In some scenarios when the flow starts with a file inbound channel adapter and then an MPUT operation is performed for remote file outbound gateway, the populated in the beginning `FileHeaders.FILENAME` is used from the `DefaultFileNameGenerator` for all the files from local directory to upload. Such a behaviour leads only to the last file in the target remote directory and only with the name from that header. * Fix the `AbstractRemoteFileOutboundGateway` to remove a `FileHeaders.FILENAME` header when message is build for specific item from MPUT request. This way an original local file is used when we upload directory.
1 parent ce4ce74 commit ea3e118

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@
5555
import org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor;
5656
import org.springframework.integration.handler.MessageProcessor;
5757
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
58-
import org.springframework.integration.support.MutableMessage;
58+
import org.springframework.integration.support.MutableMessageBuilder;
5959
import org.springframework.integration.support.PartialSuccessException;
6060
import org.springframework.integration.support.utils.IntegrationUtils;
6161
import org.springframework.lang.Nullable;
6262
import org.springframework.messaging.Message;
6363
import org.springframework.messaging.MessageHandlingException;
64+
import org.springframework.messaging.MessageHeaders;
6465
import org.springframework.messaging.MessagingException;
6566
import org.springframework.util.Assert;
6667
import org.springframework.util.ObjectUtils;
@@ -867,7 +868,8 @@ else if (!(payload instanceof Collection)) {
867868
}
868869
if (payload instanceof Collection<?> files) {
869870
return files.stream()
870-
.map(p -> doMput(new MutableMessage<>(p, requestMessage.getHeaders())))
871+
.map((filePayload) -> mputItemMessage(filePayload, requestMessage.getHeaders()))
872+
.map(this::doMput)
871873
.collect(Collectors.toList());
872874
}
873875
else if (!file.isDirectory()) {
@@ -879,6 +881,13 @@ else if (!file.isDirectory()) {
879881
}
880882
}
881883

884+
private static Message<?> mputItemMessage(Object payload, MessageHeaders requestHeaders) {
885+
return MutableMessageBuilder.withPayload(payload)
886+
.copyHeaders(requestHeaders)
887+
.removeHeader(FileHeaders.FILENAME)
888+
.build();
889+
}
890+
882891
/**
883892
* Put files from the provided directory to the remote server recursively.
884893
* The message can be consulted to determine some context.
@@ -899,7 +908,7 @@ private List<String> putLocalDirectory(Message<?> requestMessage, File file, Str
899908
try {
900909
for (File filteredFile : filteredFiles) {
901910
if (!filteredFile.isDirectory()) {
902-
String path = doPut(new MutableMessage<>(filteredFile, requestMessage.getHeaders()), subDirectory);
911+
String path = doPut(mputItemMessage(filteredFile, requestMessage.getHeaders()), subDirectory);
903912
if (path != null) {
904913
replies.add(path);
905914
}

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpServerOutboundTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,13 @@ public void testInt3088MPutNotRecursive() throws IOException {
398398
session = TestUtils.getPropertyValue(session, "targetSession", Session.class);
399399
FTPClient client = spy(TestUtils.getPropertyValue(session, "client", FTPClient.class));
400400
new DirectFieldAccessor(session).setPropertyValue("client", client);
401-
this.inboundMPut.send(new GenericMessage<>(getSourceLocalDirectory()));
401+
// The FileHeaders.FILENAME is removed by the AbstractRemoteFileOutboundGateway
402+
// when MPUT item is prepared for its specific PUT
403+
Message<File> mputRequestMessage =
404+
MessageBuilder.withPayload(getSourceLocalDirectory())
405+
.setHeader(FileHeaders.FILENAME, "inbound_file_name")
406+
.build();
407+
this.inboundMPut.send(mputRequestMessage);
402408
@SuppressWarnings("unchecked")
403409
Message<List<String>> out = (Message<List<String>>) this.output.receive(1000);
404410
assertThat(out).isNotNull();

0 commit comments

Comments
 (0)