From d00e0cdcc1c89fa46627c5eb2bf87d95928721b5 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 29 Jul 2016 12:45:25 -0400 Subject: [PATCH] Document Java(DSL) for (S)FTP Outbound SO: http://stackoverflow.com/questions/38654837/retreive-files-from-ftp-using-spring-integration-ftpoutboundgateway --- src/reference/asciidoc/amqp.adoc | 2 +- src/reference/asciidoc/ftp.adoc | 184 +++++++++++++++++++++++++++++++ src/reference/asciidoc/sftp.adoc | 152 +++++++++++++++++++++++++ 3 files changed, 337 insertions(+), 1 deletion(-) diff --git a/src/reference/asciidoc/amqp.adoc b/src/reference/asciidoc/amqp.adoc index c0ba93e924..5b3e7376c1 100644 --- a/src/reference/asciidoc/amqp.adoc +++ b/src/reference/asciidoc/amqp.adoc @@ -214,7 +214,7 @@ For this reason, you must define the container using a normal Spring `` d ===== IMPORTANT: Even though the Spring Integration JMS and AMQP support is very similar, important differences exist. -The JMS Inbound Channel Adapter is using a JmsDestinationPollingSource under the covers and expects a configured Poller. +The JMS Inbound Channel Adapter is using a `JmsDestinationPollingSource` under the covers and expects a configured Poller. The AMQP Inbound Channel Adapter on the other side uses a`SimpleMessageListenerContainer` and is message driven. In that regard it is more similar to the JMS Message Driven Channel Adapter. diff --git a/src/reference/asciidoc/ftp.adoc b/src/reference/asciidoc/ftp.adoc index 9f0bcde471..03af5607a6 100644 --- a/src/reference/asciidoc/ftp.adoc +++ b/src/reference/asciidoc/ftp.adoc @@ -538,6 +538,110 @@ However, there may be situations where you don't want to use this technique (for For situations like this, you can disable this feature by setting `use-temporary-file-name` to `false` (default is `true`). When this attribute is `false`, the file is written with its final name and the consuming application will need some other mechanism to detect that the file is completely uploaded before accessing it. +==== Configuring with Java Configuration + +The following Spring Boot application provides an example of configuring the Outbound Adapter using Java configuration: +[source, java] +---- +@SpringBootApplication +@IntegrationComponentScan +public class FtpJavaApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext context = + new SpringApplicationBuilder(FtpJavaApplication.class) + .web(false) + .run(args); + MyGateway gateway = context.getBean(MyGateway.class); + gateway.sendToFtp(new File("/foo/bar.txt")); + } + + @Bean + public SessionFactory ftpSessionFactory() { + DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory(); + sf.setHost("localhost"); + sf.setPort(port); + sf.setUsername("foo"); + sf.setPassword("foo"); + return new CachingSessionFactory(sf); + } + + @Bean + @ServiceActivator(inputChannel = "ftpChannel") + public MessageHandler handler() { + FtpMessageHandler handler = new FtpMessageHandler(ftpSessionFactory()); + handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir")); + handler.setFileNameGenerator(new FileNameGenerator() { + + @Override + public String generateFileName(Message message) { + return "handlerContent.test"; + } + + }); + return handler; + } + + @MessagingGateway + public interface MyGateway { + + @Gateway(requestChannel = "toFtpChannel") + void sendToFtp(File file); + + } +} +---- + +==== Configuring with the Java DSL + +The following Spring Boot application provides an example of configuring the Outbound Adapter using the Java DSL: + +[source, java] +---- +@SpringBootApplication +@IntegrationComponentScan +public class FtpJavaApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext context = + new SpringApplicationBuilder(FtpJavaApplication.class) + .web(false) + .run(args); + MyGateway gateway = context.getBean(MyGateway.class); + gateway.sendToFtp(new File("/foo/bar.txt")); + } + + @Bean + public SessionFactory ftpSessionFactory() { + DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory(); + sf.setHost("localhost"); + sf.setPort(port); + sf.setUsername("foo"); + sf.setPassword("foo"); + return new CachingSessionFactory(sf); + } + + @Bean + public IntegrationFlow ftpOutboundFlow() { + return IntegrationFlows.from("toFtpChannel") + .handle(Ftp.outboundAdapter(ftpSessionFactory(), FileExistsMode.FAIL) + .useTemporaryFileName(false) + .fileNameExpression("headers['" + FileHeaders.FILENAME + "']") + .remoteDirectory(this.ftpServer.getTargetFtpDirectory().getName()) + ).get(); + } + + @MessagingGateway + public interface MyGateway { + + @Gateway(requestChannel = "toFtpChannel") + void sendToFtp(File file); + + } + +} +---- + [[ftp-outbound-gateway]] === FTP Outbound Gateway @@ -753,6 +857,86 @@ file exists (`PUT` and `MPUT`). Supported modes are `REPLACE`, `APPEND`, `FAIL` For backwards compatibility, the default mode for `PUT` and `MPUT` operations is `REPLACE` and for `GET` and `MGET` operations, the default is `FAIL`. +==== Configuring with Java Configuration + +The following Spring Boot application provides an example of configuring the Outbound Gateway using Java configuration: +[source, java] +---- +@SpringBootApplication +public class FtpJavaApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(FtpJavaApplication.class) + .web(false) + .run(args); + } + + @Bean + public SessionFactory ftpSessionFactory() { + DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory(); + sf.setHost("localhost"); + sf.setPort(port); + sf.setUsername("foo"); + sf.setPassword("foo"); + return new CachingSessionFactory(sf); + } + + @Bean + @ServiceActivator(inputChannel = "ftpChannel") + public MessageHandler handler() { + FtpOutboundGateway ftpOutboundGateway = new FtpOutboundGateway(ftpSessionFactory(), "ls"); + ftpOutboundGateway.setOutputChannelName("lsReplyChannel"); + return ftpOutboundGateway; + } + +} +---- + +==== Configuring with the Java DSL + +The following Spring Boot application provides an example of configuring the Outbound Gateway using the Java DSL: + +[source, java] +---- +@SpringBootApplication +public class FtpJavaApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(FtpJavaApplication.class) + .web(false) + .run(args); + } + + @Bean + public SessionFactory ftpSessionFactory() { + DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory(); + sf.setHost("localhost"); + sf.setPort(port); + sf.setUsername("foo"); + sf.setPassword("foo"); + return new CachingSessionFactory(sf); + } + + @Bean + public FtpOutboundGatewaySpec ftpOutboundGateway() { + return Ftp.outboundGateway(ftpSessionFactory(), + AbstractRemoteFileOutboundGateway.Command.MGET, "payload") + .options(AbstractRemoteFileOutboundGateway.Option.RECURSIVE) + .regexFileNameFilter("(subFtpSource|.*1.txt)") + .localDirectoryExpression("'localDirectory/' + #remoteDirectory") + .localFilenameExpression("#remoteFileName.replaceFirst('ftpSource', 'localTarget')"); + } + + @Bean + public IntegrationFlow ftpMGetFlow(AbstractRemoteFileOutboundGateway ftpOutboundGateway) { + return f -> f + .handle(ftpOutboundGateway) + .channel(c -> c.queue("remoteFileOutputChannel")); + } + +} +---- + [[ftp-partial]] ==== Outbound Gateway Partial Success (mget and mput) diff --git a/src/reference/asciidoc/sftp.adoc b/src/reference/asciidoc/sftp.adoc index 14940bde29..8d3c4ac5d6 100644 --- a/src/reference/asciidoc/sftp.adoc +++ b/src/reference/asciidoc/sftp.adoc @@ -655,6 +655,88 @@ _Version 4.3_ introduced the `chmod` attribute which changes the remote file per Use the conventional Unix octal format, e.g. `600` allows read-write for the file owner only. When configuring the adapter using java, you can use `setChmodOctal("600")` or `setChmodDecimal(384)`. +==== Configuring with Java Configuration + +The following Spring Boot application provides an example of configuring the Outbound Adapter using Java configuration: +[source, java] +---- +@SpringBootApplication +@IntegrationComponentScan +public class SftpJavaApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext context = + new SpringApplicationBuilder(SftpJavaApplication.class) + .web(false) + .run(args); + MyGateway gateway = context.getBean(MyGateway.class); + gateway.sendToSftp(new File("/foo/bar.txt")); + } + + @Bean + public SessionFactory sftpSessionFactory() { + DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true); + factory.setHost("localhost"); + factory.setPort(port); + factory.setUser("foo"); + factory.setPassword("foo"); + factory.setAllowUnknownKeys(true); + return new CachingSessionFactory(factory); + } + + @Bean + @ServiceActivator(inputChannel = "toSftpChannel") + public MessageHandler handler() { + SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory()); + handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir")); + handler.setFileNameGenerator(new FileNameGenerator() { + + @Override + public String generateFileName(Message message) { + return "handlerContent.test"; + } + + }); + return handler; + } + + @MessagingGateway + public interface MyGateway { + + @Gateway(requestChannel = "toSftpChannel") + void sendToSftp(File file); + + } +} +---- + +==== Configuring with the Java DSL + +The following Spring Boot application provides an example of configuring the Outbound Adapter using the Java DSL: + +[source, java] +---- +@SpringBootApplication +public class SftpJavaApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(SftpJavaApplication.class) + .web(false) + .run(args); + } + + @Bean + public IntegrationFlow sftpOutboundFlow() { + return IntegrationFlows.from("toSftpChannel") + .handle(Sftp.outboundAdapter(this.sftpSessionFactory, FileExistsMode.FAIL) + .useTemporaryFileName(false) + .remoteDirectory("/foo") + ).get(); + } + +} +---- + [[sftp-outbound-gateway]] === SFTP Outbound Gateway @@ -873,6 +955,76 @@ file exists (`PUT` and `MPUT`). Supported modes are `REPLACE`, `APPEND`, `FAIL` For backwards compatibility, the default mode for `PUT` and `MPUT` operations is `REPLACE` and for `GET` and `MGET` operations, the default is `FAIL`. +==== Configuring with Java Configuration + +The following Spring Boot application provides an example of configuring the Outbound Gateway using Java configuration: +[source, java] +---- +@SpringBootApplication +public class SftpJavaApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(SftpJavaApplication.class) + .web(false) + .run(args); + } + + @Bean + @ServiceActivator(inputChannel = "sftpChannel") + public MessageHandler handler() { + return new SftpOutboundGateway(ftpSessionFactory(), "ls"); + } + +} +---- + +==== Configuring with the Java DSL + +The following Spring Boot application provides an example of configuring the Outbound Gateway using the Java DSL: + +[source, java] +---- +@SpringBootApplication +public class SftpJavaApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(SftpJavaApplication.class) + .web(false) + .run(args); + } + + @Bean + public SessionFactory ftpSessionFactory() { + DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory(); + sf.setHost("localhost"); + sf.setPort(port); + sf.setUsername("foo"); + sf.setPassword("foo"); + return new CachingSessionFactory(sf); + } + + @Bean + public QueueChannelSpec remoteFileOutputChannel() { + return MessageChannels.queue(); + } + + @Bean + public IntegrationFlow sftpMGetFlow() { + return IntegrationFlows.from("sftpMgetInputChannel") + .handleWithAdapter(h -> + h.sftpGateway(this.sftpSessionFactory, AbstractRemoteFileOutboundGateway.Command.MGET, + "payload") + .options(AbstractRemoteFileOutboundGateway.Option.RECURSIVE) + .regexFileNameFilter("(subSftpSource|.*1.txt)") + .localDirectoryExpression("'myDir/' + #remoteDirectory") + .localFilenameExpression("#remoteFileName.replaceFirst('sftpSource', 'localTarget')")) + .channel("remoteFileOutputChannel") + .get(); + } + +} +---- + [[sftp-partial]] ==== Outbound Gateway Partial Success (mget and mput)