INT-1945 (S)FTP Outbound Gateway Samples

Samples execute ls, get and rm commands.
This commit is contained in:
Gary Russell
2011-08-29 13:23:26 -04:00
parent a24994dc1c
commit 6131356619
12 changed files with 340 additions and 3 deletions

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 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<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.remoteDir + '/' + payload.filename"/>
<int-ftp:outbound-gateway id="gatewayRM" reply-channel="aggregateResultsChannel"
session-factory="ftpSessionFactory"
expression="headers['file_remote_dir'] + '/' + headers['file_remote_file']"
request-channel="toRm"
command="rm"/>
<int:aggregator input-channel="aggregateResultsChannel"/>
</beans>