I would like to use the SFTP Outbound Gateway to get
a file via SFTP but I only find examples using XML config. How can this be done using Java config?
Update (Thanks to Artem Bilan help)
MyConfiguration class:
@Configuration
public class MyConfiguration {
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory();
sftpSessionFactory.setHost("myhost");
sftpSessionFactory.setPort(22);
sftpSessionFactory.setUser("uname");
sftpSessionFactory.setPassword("pass");
sftpSessionFactory.setAllowUnknownKeys(true);
return new CachingSessionFactory<LsEntry>(sftpSessionFactory);
}
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler() {
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "get", "#getPayload() == '/home/samadmin/test.endf'");
sftpOutboundGateway.setLocalDirectory(new File("C:/test/gateway/"));
return sftpOutboundGateway;
}
}
My application class:
@SpringBootApplication
@EnableIntegration
public class TestIntegrationApplication {
public static void main(String[] args) {
SpringApplication.run(TestIntegrationApplication.class, args);
}
}
Configuration succeeds now but no SFTP occurs. Need to figure out how to request SFTP.
Quoting Reference Manual:
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler() {
return new SftpOutboundGateway(ftpSessionFactory(), "ls");
}
Also pay attention to the Java DSL sample in the next section there.
EDIT
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler() {
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "get", "payload");
sftpOutboundGateway.setLocalDirectory(new File("C:/test/gateway/"));
return sftpOutboundGateway;
}
In case of GET
SFTP command the expression
ctor arg maybe like above - just a reference to the Message.getPayload()
for all incoming messages.
In this case you should send to the sftpChannel
a Message
like:
new GenericMessage<>("/home/samadmin/test.endf");
So, that /home/samadmin/test.endf
is a payload
of that Message
. When it arrives to the SftpOutboundGateway
, that expression is evaluated against that message and getPayload()
is called by SpEL. So, the GET
command will be preformed with the desired path to the remote file.
An other message may have fully different path to some other file.