From 61313566198f1986f2357e87fd52efe296221aff Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Mon, 29 Aug 2011 13:23:26 -0400 Subject: [PATCH 1/4] INT-1945 (S)FTP Outbound Gateway Samples Samples execute ls, get and rm commands. --- basic/ftp/pom.xml | 2 +- basic/ftp/readme.txt | 9 +++ .../ftp/FtpOutboundGatrewaySample.java | 76 ++++++++++++++++++ .../samples/ftp/ToFtpFlowGateway.java | 28 +++++++ .../FtpOutboundGatewaySample-context.xml | 51 ++++++++++++ basic/sftp/pom.xml | 2 +- basic/sftp/readme.txt | 11 +++ .../sftp/SftpOutboundGatrewaySample.java | 78 +++++++++++++++++++ .../samples/sftp/ToSftpFlowGateway.java | 28 +++++++ .../SftpOutboundGatewaySample-context.xml | 53 +++++++++++++ .../sftp/src/test/resources/log4j.properties | 1 + basic/sftp/src/test/resources/user.properties | 4 +- 12 files changed, 340 insertions(+), 3 deletions(-) create mode 100644 basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundGatrewaySample.java create mode 100644 basic/ftp/src/test/java/org/springframework/integration/samples/ftp/ToFtpFlowGateway.java create mode 100644 basic/ftp/src/test/resources/META-INF/spring/integration/FtpOutboundGatewaySample-context.xml create mode 100644 basic/sftp/src/test/java/org/springframework/integration/samples/sftp/SftpOutboundGatrewaySample.java create mode 100644 basic/sftp/src/test/java/org/springframework/integration/samples/sftp/ToSftpFlowGateway.java create mode 100644 basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml diff --git a/basic/ftp/pom.xml b/basic/ftp/pom.xml index 354d3d15..91d4b798 100644 --- a/basic/ftp/pom.xml +++ b/basic/ftp/pom.xml @@ -6,7 +6,7 @@ FTP Demo jar - 2.0.5.RELEASE + 2.1.0.BUILD-SNAPSHOT 1.2.16 4.7 diff --git a/basic/ftp/readme.txt b/basic/ftp/readme.txt index 5c18cf70..6032f017 100644 --- a/basic/ftp/readme.txt +++ b/basic/ftp/readme.txt @@ -24,3 +24,12 @@ The output should look like this: ===== Successfully transfered 'readme.txt' file to a remote location under the name 'readme.txt' ===== + + +#### OUTBOUND GATEWAY #### + +Run the FtpOutoundGateway sample as a JUnit test; it creates 2 files, retrieves and removes them over ftp. It cleans up +by removing the retrieved files. Test assumes full access to the filesystem via /tmp where the test files are created. +Requires an ftp server running on localhost. + +Requires setting of user and password properties in user.properties. \ No newline at end of file diff --git a/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundGatrewaySample.java b/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundGatrewaySample.java new file mode 100644 index 00000000..fee6b3d3 --- /dev/null +++ b/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundGatrewaySample.java @@ -0,0 +1,76 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.samples.ftp; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.List; +import java.util.Random; + +import org.junit.Test; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * Demonstrates use of the outbound gateway to use ls, get and rm. + * Creates a temporary directory with 2 files; retrieves and removes them. + * @author Gary Russell + * @since 2.1 + * + */ +public class FtpOutboundGatrewaySample { + + @Test + public void testLsGetRm() throws Exception { + ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext( + "classpath:/META-INF/spring/integration/FtpOutboundGatewaySample-context.xml"); + ToFtpFlowGateway toFtpFlow = ctx.getBean(ToFtpFlowGateway.class); + try { + String tmpDir = System.getProperty("java.io.tmpdir"); + + // remove the previous output files if necessary + new File(new File(tmpDir), "1.ftptest").delete(); + new File(new File(tmpDir), "2.ftptest").delete(); + + // create a couple of files in a temp dir + File dir = new File(tmpDir + "/" + new Random().nextInt()); + dir.mkdir(); + File f1 = new File(dir, "1.ftptest"); + f1.createNewFile(); + File f2 = new File(dir, "2.ftptest"); + f2.createNewFile(); + + + // execute the flow (ls, get, rm, aggregate results) + List rmResults = toFtpFlow.lsGetAndRmFiles(dir.getAbsolutePath()); + + + //Check everything went as expected, and clean up + assertEquals(2, rmResults.size()); + for (Boolean result : rmResults) { + assertTrue(result); + } + assertTrue("Expected remote dir to be empty", dir.delete()); + assertTrue("Could note delete retrieved file", new File(new File(tmpDir), "1.ftptest").delete()); + assertTrue("Could note delete retrieved file", new File(new File(tmpDir), "2.ftptest").delete()); + } finally { + ctx.close(); + } + } + +} \ No newline at end of file diff --git a/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/ToFtpFlowGateway.java b/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/ToFtpFlowGateway.java new file mode 100644 index 00000000..2fa7f23d --- /dev/null +++ b/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/ToFtpFlowGateway.java @@ -0,0 +1,28 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.samples.ftp; + +import java.util.List; + +/** + * @author Gary Russell + * @since 2.1 + * + */ +public interface ToFtpFlowGateway { + + public List lsGetAndRmFiles(String dir); +} diff --git a/basic/ftp/src/test/resources/META-INF/spring/integration/FtpOutboundGatewaySample-context.xml b/basic/ftp/src/test/resources/META-INF/spring/integration/FtpOutboundGatewaySample-context.xml new file mode 100644 index 00000000..32af74a8 --- /dev/null +++ b/basic/ftp/src/test/resources/META-INF/spring/integration/FtpOutboundGatewaySample-context.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/basic/sftp/pom.xml b/basic/sftp/pom.xml index b181b351..8430d582 100644 --- a/basic/sftp/pom.xml +++ b/basic/sftp/pom.xml @@ -7,7 +7,7 @@ SFTP Demo jar - 2.0.5.RELEASE + 2.1.0.BUILD-SNAPSHOT 1.2.16 4.7 diff --git a/basic/sftp/readme.txt b/basic/sftp/readme.txt index d94508f2..565f35a3 100644 --- a/basic/sftp/readme.txt +++ b/basic/sftp/readme.txt @@ -33,6 +33,17 @@ Successfully transfered 'readme.txt' file to a remote location under the name 'r NOTE: You can see that we are using SpEL via 'remote-filename-generator-expression' attribute to define the remote file name by simply appending '_foo' to the original file name. + + +#### OUTBOUND GATEWAY #### + +Run the FtpOutoundGateway sample as a JUnit test; it creates 2 files, retrieves and removes them over ftp. It cleans up +by removing the retrieved files. Test assumes full access to the filesystem via /tmp where the test files are created. +Requires sshd to be running on localhost. + +This sample uses a property 'private.keyfile' to point to the location of your private key. + +Requires setting of user, private.keyfile and, optionally, passphrase in user.properties. ====== HOW TO GENERATE KEYS ====== diff --git a/basic/sftp/src/test/java/org/springframework/integration/samples/sftp/SftpOutboundGatrewaySample.java b/basic/sftp/src/test/java/org/springframework/integration/samples/sftp/SftpOutboundGatrewaySample.java new file mode 100644 index 00000000..eea2fb32 --- /dev/null +++ b/basic/sftp/src/test/java/org/springframework/integration/samples/sftp/SftpOutboundGatrewaySample.java @@ -0,0 +1,78 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.samples.sftp; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.List; +import java.util.Random; + +import org.junit.Test; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * Demonstrates use of the outbound gateway to use ls, get and rm. + * Creates a temporary directory with 2 files; retrieves and removes them. + * @author Gary Russell + * @since 2.1 + * + */ +public class SftpOutboundGatrewaySample { + + @Test + public void testLsGetRm() throws Exception { + ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext( + "classpath:/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml"); + ToSftpFlowGateway toFtpFlow = ctx.getBean(ToSftpFlowGateway.class); + try { + String tmpDir = System.getProperty("java.io.tmpdir"); + + // remove the previous output files if necessary + new File(new File(tmpDir), "1.ftptest").delete(); + new File(new File(tmpDir), "2.ftptest").delete(); + + // create a couple of files in a temp dir + File dir = new File(tmpDir + "/" + new Random().nextInt()); + dir.mkdir(); + File f1 = new File(dir, "1.ftptest"); + f1.createNewFile(); + File f2 = new File(dir, "2.ftptest"); + f2.createNewFile(); + + + // execute the flow (ls, get, rm, aggregate results) + List rmResults = toFtpFlow.lsGetAndRmFiles(dir.getAbsolutePath()); + + + //Check everything went as expected, and clean up + assertEquals(2, rmResults.size()); + for (Boolean result : rmResults) { + assertTrue(result); + } + assertTrue("Expected remote dir to be empty", dir.delete()); + assertTrue("Could note delete retrieved file", new File(new File(tmpDir), "1.ftptest").delete()); + assertTrue("Could note delete retrieved file", new File(new File(tmpDir), "2.ftptest").delete()); + } finally { + ctx.close(); + } + } +} + + + diff --git a/basic/sftp/src/test/java/org/springframework/integration/samples/sftp/ToSftpFlowGateway.java b/basic/sftp/src/test/java/org/springframework/integration/samples/sftp/ToSftpFlowGateway.java new file mode 100644 index 00000000..38a43085 --- /dev/null +++ b/basic/sftp/src/test/java/org/springframework/integration/samples/sftp/ToSftpFlowGateway.java @@ -0,0 +1,28 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.samples.sftp; + +import java.util.List; + +/** + * @author Gary Russell + * @since 2.1 + * + */ +public interface ToSftpFlowGateway { + + public List lsGetAndRmFiles(String dir); +} diff --git a/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml b/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml new file mode 100644 index 00000000..83554244 --- /dev/null +++ b/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/basic/sftp/src/test/resources/log4j.properties b/basic/sftp/src/test/resources/log4j.properties index 94686dac..d85acd8e 100644 --- a/basic/sftp/src/test/resources/log4j.properties +++ b/basic/sftp/src/test/resources/log4j.properties @@ -5,5 +5,6 @@ log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n log4j.category.org.springframework=WARN +log4j.category.org.springframework.integration=DEBUG log4j.category.org.springframework.integration.sftp=TRACE log4j.category.org.springframework.integration.samples.sftp=TRACE diff --git a/basic/sftp/src/test/resources/user.properties b/basic/sftp/src/test/resources/user.properties index 986067f9..72d8a131 100644 --- a/basic/sftp/src/test/resources/user.properties +++ b/basic/sftp/src/test/resources/user.properties @@ -1,2 +1,4 @@ user= -passphrase= \ No newline at end of file +passphrase= +#private.keyfile=file:/home/someuser/.ssh/id_rsa +private.keyfile=classpath:META-INF/keys/sftp_rsa \ No newline at end of file From f42cb8bc3552d7a9bd0645b019547893efbffaf5 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Mon, 29 Aug 2011 14:16:52 -0400 Subject: [PATCH 2/4] Change splitter channel name to toSplitter --- .../spring/integration/SftpOutboundGatewaySample-context.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml b/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml index 83554244..0a1af0db 100644 --- a/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml +++ b/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml @@ -29,9 +29,9 @@ command="ls" command-options="" expression="payload" - reply-channel="toSplitter1"/> + reply-channel="toSplitter"/> - + Date: Tue, 6 Sep 2011 09:44:31 -0400 Subject: [PATCH 3/4] INT-1945 Fix class names; update POMs to M1 --- basic/ftp/pom.xml | 4 ++-- ...boundGatrewaySample.java => FtpOutboundGatewaySample.java} | 2 +- basic/sftp/pom.xml | 4 ++-- ...oundGatrewaySample.java => SftpOutboundGatewaySample.java} | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) rename basic/ftp/src/test/java/org/springframework/integration/samples/ftp/{FtpOutboundGatrewaySample.java => FtpOutboundGatewaySample.java} (98%) rename basic/sftp/src/test/java/org/springframework/integration/samples/sftp/{SftpOutboundGatrewaySample.java => SftpOutboundGatewaySample.java} (98%) diff --git a/basic/ftp/pom.xml b/basic/ftp/pom.xml index 91d4b798..4e0d715a 100644 --- a/basic/ftp/pom.xml +++ b/basic/ftp/pom.xml @@ -6,7 +6,7 @@ FTP Demo jar - 2.1.0.BUILD-SNAPSHOT + 2.1.0.M1 1.2.16 4.7 @@ -60,4 +60,4 @@ http://maven.springframework.org/snapshot - \ No newline at end of file + diff --git a/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundGatrewaySample.java b/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundGatewaySample.java similarity index 98% rename from basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundGatrewaySample.java rename to basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundGatewaySample.java index fee6b3d3..0471a1f0 100644 --- a/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundGatrewaySample.java +++ b/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundGatewaySample.java @@ -33,7 +33,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; * @since 2.1 * */ -public class FtpOutboundGatrewaySample { +public class FtpOutboundGatewaySample { @Test public void testLsGetRm() throws Exception { diff --git a/basic/sftp/pom.xml b/basic/sftp/pom.xml index 8430d582..86f05b59 100644 --- a/basic/sftp/pom.xml +++ b/basic/sftp/pom.xml @@ -7,7 +7,7 @@ SFTP Demo jar - 2.1.0.BUILD-SNAPSHOT + 2.1.0.M1 1.2.16 4.7 @@ -61,4 +61,4 @@ http://maven.springframework.org/snapshot - \ No newline at end of file + diff --git a/basic/sftp/src/test/java/org/springframework/integration/samples/sftp/SftpOutboundGatrewaySample.java b/basic/sftp/src/test/java/org/springframework/integration/samples/sftp/SftpOutboundGatewaySample.java similarity index 98% rename from basic/sftp/src/test/java/org/springframework/integration/samples/sftp/SftpOutboundGatrewaySample.java rename to basic/sftp/src/test/java/org/springframework/integration/samples/sftp/SftpOutboundGatewaySample.java index eea2fb32..9745c8be 100644 --- a/basic/sftp/src/test/java/org/springframework/integration/samples/sftp/SftpOutboundGatrewaySample.java +++ b/basic/sftp/src/test/java/org/springframework/integration/samples/sftp/SftpOutboundGatewaySample.java @@ -33,7 +33,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; * @since 2.1 * */ -public class SftpOutboundGatrewaySample { +public class SftpOutboundGatewaySample { @Test public void testLsGetRm() throws Exception { From c7c0cf493c0f5d0c384cef43228c8f7aabd4e39c Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 6 Sep 2011 10:09:30 -0400 Subject: [PATCH 4/4] INT-1945 Dir => Directory --- .../spring/integration/FtpOutboundGatewaySample-context.xml | 4 ++-- .../spring/integration/SftpOutboundGatewaySample-context.xml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/basic/ftp/src/test/resources/META-INF/spring/integration/FtpOutboundGatewaySample-context.xml b/basic/ftp/src/test/resources/META-INF/spring/integration/FtpOutboundGatewaySample-context.xml index 32af74a8..4fa6b697 100644 --- a/basic/ftp/src/test/resources/META-INF/spring/integration/FtpOutboundGatewaySample-context.xml +++ b/basic/ftp/src/test/resources/META-INF/spring/integration/FtpOutboundGatewaySample-context.xml @@ -38,11 +38,11 @@ reply-channel="toRm" command="get" command-options="-P" - expression="payload.remoteDir + '/' + payload.filename"/> + expression="payload.remoteDirectory + '/' + payload.filename"/> diff --git a/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml b/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml index 0a1af0db..9ef3195b 100644 --- a/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml +++ b/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml @@ -40,11 +40,11 @@ reply-channel="toRm" command="get" command-options="-P" - expression="payload.remoteDir + '/' + payload.filename"/> + expression="payload.remoteDirectory + '/' + payload.filename"/>