Merge pull request #10 from garyrussell/INT-1945

INT-1945 Add Samples for (S)FTP Outbound Gateways
This commit is contained in:
Gary Russell
2011-10-02 11:42:15 -07:00
12 changed files with 342 additions and 5 deletions

View File

@@ -6,7 +6,7 @@
<name>FTP Demo</name>
<packaging>jar</packaging>
<properties>
<spring.integration.version>2.0.5.RELEASE</spring.integration.version>
<spring.integration.version>2.1.0.M1</spring.integration.version>
<log4j.version>1.2.16</log4j.version>
<junit.version>4.7</junit.version>
</properties>
@@ -60,4 +60,4 @@
<url>http://maven.springframework.org/snapshot</url>
</repository>
</repositories>
</project>
</project>

View File

@@ -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.

View File

@@ -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 FtpOutboundGatewaySample {
@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<Boolean> 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();
}
}
}

View File

@@ -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<Boolean> lsGetAndRmFiles(String dir);
}

View File

@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp"
xsi:schemaLocation="http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp-2.1.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:user.properties"/>
<int:gateway id="gw" service-interface="org.springframework.integration.samples.ftp.ToFtpFlowGateway"
default-request-channel="inbound"/>
<bean id="ftpSessionFactory"
class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
<property name="host" value="localhost"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<int-ftp:outbound-gateway id="gatewayLS"
session-factory="ftpSessionFactory"
request-channel="inbound"
command="ls"
command-options=""
expression="payload"
reply-channel="toSplitter"/>
<int:splitter input-channel="toSplitter" output-channel="toGet"/>
<int-ftp:outbound-gateway id="gatewayGET"
local-directory="#{ T(System).getProperty('java.io.tmpdir')}"
session-factory="ftpSessionFactory"
request-channel="toGet"
reply-channel="toRm"
command="get"
command-options="-P"
expression="payload.remoteDirectory + '/' + payload.filename"/>
<int-ftp:outbound-gateway id="gatewayRM" reply-channel="aggregateResultsChannel"
session-factory="ftpSessionFactory"
expression="headers['file_remoteDirectory'] + '/' + headers['file_remoteFile']"
request-channel="toRm"
command="rm"/>
<int:aggregator input-channel="aggregateResultsChannel"/>
</beans>

View File

@@ -7,7 +7,7 @@
<name>SFTP Demo</name>
<packaging>jar</packaging>
<properties>
<spring.integration.version>2.0.5.RELEASE</spring.integration.version>
<spring.integration.version>2.1.0.M1</spring.integration.version>
<log4j.version>1.2.16</log4j.version>
<junit.version>4.7</junit.version>
</properties>
@@ -61,4 +61,4 @@
<url>http://maven.springframework.org/snapshot</url>
</repository>
</repositories>
</project>
</project>

View File

@@ -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 ======

View File

@@ -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 SftpOutboundGatewaySample {
@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<Boolean> 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();
}
}
}

View File

@@ -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<Boolean> lsGetAndRmFiles(String dir);
}

View File

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-2.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:user.properties"/>
<int:gateway id="gw" service-interface="org.springframework.integration.samples.sftp.ToSftpFlowGateway"
default-request-channel="inbound"/>
<bean id="sftpSessionFactory"
class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="localhost"/>
<property name="privateKey" value="${private.keyfile}"/>
<property name="privateKeyPassphrase" value="${passphrase}"/>
<property name="port" value="22"/>
<property name="user" value="${user}"/>
</bean>
<int-sftp:outbound-gateway id="gatewayLS"
session-factory="sftpSessionFactory"
request-channel="inbound"
command="ls"
command-options=""
expression="payload"
reply-channel="toSplitter"/>
<int:splitter input-channel="toSplitter" output-channel="toGet"/>
<int-sftp:outbound-gateway id="gatewayGET"
local-directory="#{ T(System).getProperty('java.io.tmpdir')}"
session-factory="sftpSessionFactory"
request-channel="toGet"
reply-channel="toRm"
command="get"
command-options="-P"
expression="payload.remoteDirectory + '/' + payload.filename"/>
<int-sftp:outbound-gateway id="gatewayRM" reply-channel="aggregateResultsChannel"
session-factory="sftpSessionFactory"
expression="headers['file_remoteDirectory'] + '/' + headers['file_remoteFile']"
request-channel="toRm"
command="rm"/>
<int:aggregator input-channel="aggregateResultsChannel"/>
</beans>

View File

@@ -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

View File

@@ -1,2 +1,4 @@
user=
passphrase=
passphrase=
#private.keyfile=file:/home/someuser/.ssh/id_rsa
private.keyfile=classpath:META-INF/keys/sftp_rsa