Skip to content

Commit c0ac168

Browse files
committed
Ignore temporaryFileName when FileExistsMode is APPEND
Improve runtime behavior by ignoring temporary filename settings when file exists mode is APPEND. Now, in FileExistsMode.APPEND mode, content is always appended directly to the original file regardless of useTemporaryFileName setting. In RemoteFileTemplate: - Remove exception validation when APPEND mode is used with temporary filenames - Modify logic to skip applying temporaryFileSuffix in APPEND mode In AbstractRemoteFileOutboundGateway: - Remove logic that disabled temporary filenames when setting APPEND mode Signed-off-by: Jooyoung Pyoung <[email protected]>
1 parent 8e204c4 commit c0ac168

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2024 the original author or authors.
2+
* Copyright 2013-2025 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.
@@ -63,6 +63,7 @@
6363
* @author Gary Russell
6464
* @author Artem Bilan
6565
* @author Alen Turkovic
66+
* @author Jooyoung Pyoung
6667
*
6768
* @since 3.0
6869
*
@@ -303,8 +304,6 @@ public String send(Message<?> message, String subDirectory, FileExistsMode... mo
303304

304305
private String send(Message<?> message, String subDirectory, FileExistsMode mode) {
305306
Assert.notNull(this.directoryExpressionProcessor, "'remoteDirectoryExpression' is required");
306-
Assert.isTrue(!FileExistsMode.APPEND.equals(mode) || !this.useTemporaryFileName,
307-
"Cannot append when using a temporary file name");
308307
Assert.isTrue(!FileExistsMode.REPLACE_IF_MODIFIED.equals(mode),
309308
"FilExistsMode.REPLACE_IF_MODIFIED can only be used for local files");
310309
final StreamHolder inputStreamHolder = payloadToInputStream(message);
@@ -565,7 +564,10 @@ private void sendFileToRemoteDirectory(InputStream inputStream, String temporary
565564
String tempRemoteFilePath = temporaryRemoteDirectory + fileName;
566565
// write remote file first with temporary file extension if enabled
567566

568-
String tempFilePath = tempRemoteFilePath + (this.useTemporaryFileName ? this.temporaryFileSuffix : "");
567+
String tempFilePath = tempRemoteFilePath;
568+
if (!FileExistsMode.APPEND.equals(mode)) {
569+
tempFilePath += this.useTemporaryFileName ? this.temporaryFileSuffix : "";
570+
}
569571

570572
if (this.autoCreateDirectory) {
571573
try {

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,6 @@ public void setFileExistsModeExpression(String fileExistsModeExpression) {
524524
*/
525525
public void setFileExistsMode(FileExistsMode fileExistsMode) {
526526
this.fileExistsMode = fileExistsMode;
527-
if (FileExistsMode.APPEND.equals(fileExistsMode)) {
528-
this.remoteFileTemplate.setUseTemporaryFileName(false);
529-
}
530527
}
531528

532529
/**

spring-integration-file/src/test/java/org/springframework/integration/file/remote/gateway/RemoteFileOutboundGatewayTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -968,18 +968,19 @@ public void testPutExistsExpression() throws Exception {
968968

969969
Message<String> appendMessage = requestMessageBuilder.setHeader("file.exists.mode", FileExistsMode.APPEND)
970970
.build();
971-
972-
assertThatExceptionOfType(IllegalArgumentException.class)
973-
.isThrownBy(() -> gw.handleRequestMessage(appendMessage))
974-
.withStackTraceContaining("Cannot append when using a temporary file name");
971+
path = (String) gw.handleRequestMessage(appendMessage);
972+
assertThat(path).isEqualTo("foo/bar.txt");
973+
captor = ArgumentCaptor.forClass(String.class);
974+
verify(session).append(any(InputStream.class), captor.capture());
975+
assertThat(captor.getValue()).isEqualTo("foo/bar.txt");
975976

976977
Message<String> ignoreMessage = requestMessageBuilder.setHeader("file.exists.mode", FileExistsMode.IGNORE)
977978
.build();
978979
path = (String) gw.handleRequestMessage(ignoreMessage);
979980
assertThat(path).isEqualTo("foo/bar.txt");
980981
// no more writes/appends
981982
verify(session, times(2)).write(any(InputStream.class), anyString());
982-
verify(session, times(0)).append(any(InputStream.class), anyString());
983+
verify(session, times(1)).append(any(InputStream.class), anyString());
983984
}
984985

985986
@Test

0 commit comments

Comments
 (0)