Skip to content

Commit 35b964c

Browse files
joaquinjsbartembilan
authored andcommitted
GH-3026: Fix chmod support for DSL
Fixes #3026 **Cherry-pick to `5.1.x`** * Populate proper `FileTransferringMessageHandler` impl from DSL spec implementations. This way we are able to use a provided `chmod` from Java DSL * Added `FileTransferringMessageHandlerSpec` ctor TODO * Update SftpTests * Code cleanup; `@Ignore` `SftpTests.testSftpOutboundFlowWithChmod()` since it doesn't work properly on Windows # Conflicts: # spring-integration-sftp/src/test/java/org/springframework/integration/sftp/dsl/SftpTests.java # spring-integration-sftp/src/test/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizerTests.java
1 parent c365a87 commit 35b964c

File tree

6 files changed

+78
-7
lines changed

6 files changed

+78
-7
lines changed

spring-integration-file/src/main/java/org/springframework/integration/file/dsl/FileTransferringMessageHandlerSpec.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @param <S> the target {@link FileTransferringMessageHandlerSpec} implementation type.
4242
*
4343
* @author Artem Bilan
44+
* @author Joaquin Santana
4445
*
4546
* @since 5.0
4647
*/
@@ -52,6 +53,10 @@ public abstract class FileTransferringMessageHandlerSpec<F, S extends FileTransf
5253

5354
private DefaultFileNameGenerator defaultFileNameGenerator;
5455

56+
// TODO: should be refactored using generics in next release (breaking change), see PR-3080.
57+
protected FileTransferringMessageHandlerSpec() {
58+
}
59+
5560
protected FileTransferringMessageHandlerSpec(SessionFactory<F> sessionFactory) {
5661
this.target = new FileTransferringMessageHandler<>(sessionFactory);
5762
}
@@ -62,6 +67,7 @@ protected FileTransferringMessageHandlerSpec(RemoteFileTemplate<F> remoteFileTem
6267

6368
protected FileTransferringMessageHandlerSpec(RemoteFileTemplate<F> remoteFileTemplate,
6469
FileExistsMode fileExistsMode) {
70+
6571
this.target = new FileTransferringMessageHandler<>(remoteFileTemplate, fileExistsMode);
6672
}
6773

spring-integration-ftp/src/main/java/org/springframework/integration/ftp/dsl/FtpMessageHandlerSpec.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,28 @@
2222
import org.springframework.integration.file.remote.RemoteFileTemplate;
2323
import org.springframework.integration.file.remote.session.SessionFactory;
2424
import org.springframework.integration.file.support.FileExistsMode;
25+
import org.springframework.integration.ftp.outbound.FtpMessageHandler;
2526

2627
/**
2728
* A {@link FileTransferringMessageHandlerSpec} for FTP.
2829
*
2930
* @author Artem Bilan
31+
* @author Joaquin Santana
32+
*
3033
* @since 5.0
3134
*/
3235
public class FtpMessageHandlerSpec extends FileTransferringMessageHandlerSpec<FTPFile, FtpMessageHandlerSpec> {
3336

3437
FtpMessageHandlerSpec(SessionFactory<FTPFile> sessionFactory) {
35-
super(sessionFactory);
38+
this.target = new FtpMessageHandler(sessionFactory);
3639
}
3740

3841
FtpMessageHandlerSpec(RemoteFileTemplate<FTPFile> remoteFileTemplate) {
39-
super(remoteFileTemplate);
42+
this.target = new FtpMessageHandler(remoteFileTemplate.getSessionFactory());
4043
}
4144

4245
FtpMessageHandlerSpec(RemoteFileTemplate<FTPFile> remoteFileTemplate, FileExistsMode fileExistsMode) {
43-
super(remoteFileTemplate, fileExistsMode);
46+
this.target = new FtpMessageHandler(remoteFileTemplate, fileExistsMode);
4447
}
4548

4649
}

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/dsl/FtpTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
/**
7575
* @author Artem Bilan
7676
* @author Gary Russell
77+
* @author Joaquin Santana
7778
*
7879
* @since 5.0
7980
*/
@@ -197,6 +198,30 @@ public void testFtpOutboundFlow() {
197198
registration.destroy();
198199
}
199200

201+
@Test
202+
public void testFtpOutboundFlowWithChmod() {
203+
IntegrationFlow flow = f -> f
204+
.handle(Ftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL)
205+
.useTemporaryFileName(false)
206+
.fileNameExpression("headers['" + FileHeaders.FILENAME + "']")
207+
.chmod(0644)
208+
.remoteDirectory("ftpTarget"));
209+
IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
210+
String fileName = "foo.file";
211+
Message<ByteArrayInputStream> message = MessageBuilder
212+
.withPayload(new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8)))
213+
.setHeader(FileHeaders.FILENAME, fileName)
214+
.build();
215+
registration.getInputChannel().send(message);
216+
RemoteFileTemplate<FTPFile> template = new RemoteFileTemplate<>(sessionFactory());
217+
FTPFile[] files = template.execute(session ->
218+
session.list(getTargetRemoteDirectory().getName() + "/" + fileName));
219+
assertEquals(1, files.length);
220+
assertEquals(3, files[0].getSize());
221+
222+
registration.destroy();
223+
}
224+
200225
@Test
201226
@SuppressWarnings("unchecked")
202227
public void testFtpMgetFlow() {

spring-integration-sftp/src/main/java/org/springframework/integration/sftp/dsl/SftpMessageHandlerSpec.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,30 @@
2020
import org.springframework.integration.file.remote.RemoteFileTemplate;
2121
import org.springframework.integration.file.remote.session.SessionFactory;
2222
import org.springframework.integration.file.support.FileExistsMode;
23+
import org.springframework.integration.sftp.outbound.SftpMessageHandler;
24+
import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
2325

2426
import com.jcraft.jsch.ChannelSftp;
2527

2628
/**
2729
* @author Artem Bilan
30+
* @author Joaquin Santana
31+
*
2832
* @since 5.0
2933
*/
3034
public class SftpMessageHandlerSpec
3135
extends FileTransferringMessageHandlerSpec<ChannelSftp.LsEntry, SftpMessageHandlerSpec> {
3236

3337
SftpMessageHandlerSpec(SessionFactory<ChannelSftp.LsEntry> sessionFactory) {
34-
super(sessionFactory);
38+
this.target = new SftpMessageHandler(sessionFactory);
3539
}
3640

3741
SftpMessageHandlerSpec(RemoteFileTemplate<ChannelSftp.LsEntry> remoteFileTemplate) {
38-
super(remoteFileTemplate);
42+
this.target = new SftpMessageHandler(remoteFileTemplate.getSessionFactory());
3943
}
4044

4145
SftpMessageHandlerSpec(RemoteFileTemplate<ChannelSftp.LsEntry> remoteFileTemplate, FileExistsMode fileExistsMode) {
42-
super(remoteFileTemplate, fileExistsMode);
46+
this.target = new SftpMessageHandler(new SftpRemoteFileTemplate(remoteFileTemplate.getSessionFactory()), fileExistsMode);
4347
}
4448

4549
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.regex.Matcher;
3131

3232
import org.hamcrest.Matchers;
33+
import org.junit.Ignore;
3334
import org.junit.Test;
3435
import org.junit.runner.RunWith;
3536

@@ -61,6 +62,7 @@
6162
/**
6263
* @author Artem Bilan
6364
* @author Gary Russell
65+
* @author Joaquin Santana
6466
*
6567
* @since 5.0
6668
*
@@ -152,6 +154,34 @@ public void testSftpOutboundFlow() {
152154
registration.destroy();
153155
}
154156

157+
158+
@Test
159+
@Ignore("Doesn't work as expected on Windows")
160+
public void testSftpOutboundFlowWithChmod() {
161+
IntegrationFlow flow = f -> f.handle(Sftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL)
162+
.useTemporaryFileName(false)
163+
.fileNameExpression("headers['" + FileHeaders.FILENAME + "']")
164+
.chmod(0644)
165+
.remoteDirectory("sftpTarget"));
166+
IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
167+
String fileName = "foo.file";
168+
registration.getInputChannel().send(MessageBuilder.withPayload("foo")
169+
.setHeader(FileHeaders.FILENAME, fileName)
170+
.build());
171+
172+
RemoteFileTemplate<ChannelSftp.LsEntry> template = new RemoteFileTemplate<>(sessionFactory());
173+
ChannelSftp.LsEntry[] files = template.execute(session ->
174+
session.list(getTargetRemoteDirectory().getName() + "/" + fileName));
175+
assertEquals(1, files.length);
176+
assertEquals(3, files[0].getAttrs().getSize());
177+
String[] permissions = files[0].getAttrs().getPermissionsString().substring(1).replaceAll("--", "-").split("-");
178+
assertEquals("rw", permissions[0]);
179+
assertEquals("r", permissions[1]);
180+
assertEquals("r", permissions[2]);
181+
182+
registration.destroy();
183+
}
184+
155185
@Test
156186
@SuppressWarnings("unchecked")
157187
public void testSftpMgetFlow() {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
* @author Gunnar Hillert
6565
* @author Gary Russell
6666
* @author Artem Bilan
67+
* @author Joaquin Santana
68+
*
6769
* @since 2.0
6870
*/
6971
public class SftpInboundRemoteFileSystemSynchronizerTests {
@@ -170,7 +172,8 @@ private void init() {
170172

171173
Calendar calendar = Calendar.getInstance();
172174
calendar.add(Calendar.DATE, 1);
173-
when(lsEntry.getAttrs().getMTime()).thenReturn(new Long(calendar.getTimeInMillis() / 1000).intValue());
175+
when(lsEntry.getAttrs().getMTime())
176+
.thenReturn(Long.valueOf(calendar.getTimeInMillis() / 1000).intValue());
174177
when(lsEntry.getFilename()).thenReturn(fileName);
175178
when(lsEntry.getLongname()).thenReturn(fileName);
176179
sftpEntries.add(lsEntry);

0 commit comments

Comments
 (0)