Skip to content

Commit

Permalink
INT-2981 (S)FTP Namespace Support and Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
garyrussell committed Apr 15, 2013
1 parent 0524a08 commit ce68e59
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
Expand All @@ -15,14 +15,13 @@
*/
package org.springframework.integration.file.config;

import org.w3c.dom.Element;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.AbstractConsumerEndpointParser;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.integration.file.remote.session.SessionFactoryFactoryBean;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
* @author Gary Russell
Expand Down Expand Up @@ -60,6 +59,7 @@ protected BeanDefinitionBuilder parseHandler(Element element, ParserContext pars
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "local-directory");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-create-local-directory");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "order");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "rename-expression");
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@
<xsd:attribute name="command" use="required" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
FTP command - ls, get or rm
FTP command - ls, get, rm, mget or mv.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
Expand Down Expand Up @@ -270,6 +270,15 @@
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="rename-expression" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
SpEL expression representing the path for the
new filename when using the 'mv' command. Defaults
to "headers.['file_renameTo']".
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="request-channel" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@
</int-ftp:request-handler-advice-chain>
</int-ftp:outbound-gateway>

<int-ftp:outbound-gateway id="gateway3"
session-factory="sf"
request-channel="inbound1"
reply-channel="outbound"
auto-startup="false"
command="mv"
expression="payload"
rename-expression="'foo'"
order="1"
/>

<int-ftp:outbound-gateway id="withBeanExpression"
local-directory="local-test-dir"
session-factory="sf"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
Expand Down Expand Up @@ -28,6 +28,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway.Command;
import org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway.Option;
import org.springframework.integration.file.remote.session.CachingSessionFactory;
import org.springframework.integration.ftp.gateway.FtpOutboundGateway;
import org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor;
Expand Down Expand Up @@ -56,6 +58,9 @@ public class FtpOutboundGatewayParserTests {
@Autowired
AbstractEndpoint gateway2;

@Autowired
AbstractEndpoint gateway3;

@Autowired
AbstractEndpoint withBeanExpression;

Expand All @@ -71,11 +76,11 @@ public void testGateway1() {
assertEquals(new File("local-test-dir"), TestUtils.getPropertyValue(gateway, "localDirectory"));
assertFalse((Boolean) TestUtils.getPropertyValue(gateway, "autoCreateLocalDirectory"));
assertNotNull(TestUtils.getPropertyValue(gateway, "filter"));
assertEquals("ls", TestUtils.getPropertyValue(gateway, "command"));
assertEquals(Command.LS, TestUtils.getPropertyValue(gateway, "command"));
@SuppressWarnings("unchecked")
Set<String> options = TestUtils.getPropertyValue(gateway, "options", Set.class);
assertTrue(options.contains("-1"));
assertTrue(options.contains("-f"));
assertTrue(options.contains(Option.NAME_ONLY));
assertTrue(options.contains(Option.NOSORT));

Long sendTimeout = TestUtils.getPropertyValue(gateway, "messagingTemplate.sendTimeout", Long.class);
assertEquals(Long.valueOf(777), sendTimeout);
Expand All @@ -91,19 +96,29 @@ public void testGateway2() {
assertNotNull(TestUtils.getPropertyValue(gateway, "outputChannel"));
assertEquals(new File("local-test-dir"), TestUtils.getPropertyValue(gateway, "localDirectory"));
assertFalse((Boolean) TestUtils.getPropertyValue(gateway, "autoCreateLocalDirectory"));
assertEquals("get", TestUtils.getPropertyValue(gateway, "command"));
assertEquals(Command.GET, TestUtils.getPropertyValue(gateway, "command"));
@SuppressWarnings("unchecked")
Set<String> options = TestUtils.getPropertyValue(gateway, "options", Set.class);
assertTrue(options.contains("-P"));
assertTrue(options.contains(Option.PRESERVE_TIMESTAMP));
gateway.handleMessage(new GenericMessage<String>("foo"));
assertEquals(1, adviceCalled);
}

@Test
public void testGatewayMv() {
FtpOutboundGateway gateway = TestUtils.getPropertyValue(gateway3,
"handler", FtpOutboundGateway.class);
assertNotNull(TestUtils.getPropertyValue(gateway, "sessionFactory"));
assertNotNull(TestUtils.getPropertyValue(gateway, "outputChannel"));
assertEquals(Command.MV, TestUtils.getPropertyValue(gateway, "command"));
assertEquals("'foo'", TestUtils.getPropertyValue(gateway, "renameProcessor.expression.expression"));
}

@Test
public void testWithBeanExpression() {
FtpOutboundGateway gateway = TestUtils.getPropertyValue(withBeanExpression,
"handler", FtpOutboundGateway.class);
ExpressionEvaluatingMessageProcessor<?> processor = TestUtils.getPropertyValue(gateway, "processor",
ExpressionEvaluatingMessageProcessor<?> processor = TestUtils.getPropertyValue(gateway, "fileNameProcessor",
ExpressionEvaluatingMessageProcessor.class);
assertNotNull(processor);
assertEquals("foo", processor.processMessage(MessageBuilder.withPayload("bar").build()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
<xsd:attribute name="command" use="required" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
sftp command - ls, get or rm
sftp command - ls, get, rm, mget or mv.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
Expand Down Expand Up @@ -272,6 +272,15 @@
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="rename-expression" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
SpEL expression representing the path for the
new filename when using the 'mv' command. Defaults
to "headers.['file_renameTo']".
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="request-channel" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
order="2"
/>

<int-sftp:outbound-gateway id="gateway3"
session-factory="sf"
request-channel="inbound1"
reply-channel="outbound"
command="mv"
expression="payload"
rename-expression="'foo'"
order="1"
/>

<int-sftp:outbound-gateway id="advised"
local-directory="local-test-dir"
session-factory="sf"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
Expand Down Expand Up @@ -28,6 +28,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway.Command;
import org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway.Option;
import org.springframework.integration.file.remote.session.CachingSessionFactory;
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
import org.springframework.integration.message.GenericMessage;
Expand All @@ -54,6 +56,9 @@ public class SftpOutboundGatewayParserTests {
@Autowired
AbstractEndpoint gateway2;

@Autowired
AbstractEndpoint gateway3;

@Autowired
AbstractEndpoint advised;

Expand All @@ -69,11 +74,11 @@ public void testGateway1() {
assertEquals(new File("local-test-dir"), TestUtils.getPropertyValue(gateway, "localDirectory"));
assertFalse((Boolean) TestUtils.getPropertyValue(gateway, "autoCreateLocalDirectory"));
assertNotNull(TestUtils.getPropertyValue(gateway, "filter"));
assertEquals("ls", TestUtils.getPropertyValue(gateway, "command"));
assertEquals(Command.LS, TestUtils.getPropertyValue(gateway, "command"));
@SuppressWarnings("unchecked")
Set<String> options = TestUtils.getPropertyValue(gateway, "options", Set.class);
assertTrue(options.contains("-1"));
assertTrue(options.contains("-f"));
assertTrue(options.contains(Option.NAME_ONLY));
assertTrue(options.contains(Option.NOSORT));

Long sendTimeout = TestUtils.getPropertyValue(gateway, "messagingTemplate.sendTimeout", Long.class);
assertEquals(Long.valueOf(777), sendTimeout);
Expand All @@ -89,10 +94,20 @@ public void testGateway2() {
assertNotNull(TestUtils.getPropertyValue(gateway, "outputChannel"));
assertEquals(new File("local-test-dir"), TestUtils.getPropertyValue(gateway, "localDirectory"));
assertFalse((Boolean) TestUtils.getPropertyValue(gateway, "autoCreateLocalDirectory"));
assertEquals("get", TestUtils.getPropertyValue(gateway, "command"));
assertEquals(Command.GET, TestUtils.getPropertyValue(gateway, "command"));
@SuppressWarnings("unchecked")
Set<String> options = TestUtils.getPropertyValue(gateway, "options", Set.class);
assertTrue(options.contains("-P"));
assertTrue(options.contains(Option.PRESERVE_TIMESTAMP));
}

@Test
public void testGatewayMv() {
SftpOutboundGateway gateway = TestUtils.getPropertyValue(gateway3,
"handler", SftpOutboundGateway.class);
assertNotNull(TestUtils.getPropertyValue(gateway, "sessionFactory"));
assertNotNull(TestUtils.getPropertyValue(gateway, "outputChannel"));
assertEquals(Command.MV, TestUtils.getPropertyValue(gateway, "command"));
assertEquals("'foo'", TestUtils.getPropertyValue(gateway, "renameProcessor.expression.expression"));
}

@Test
Expand Down
17 changes: 16 additions & 1 deletion src/reference/docbook/ftp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ protected void postProcessClientBeforeConnect(T client) throws IOException {
<listitem>get (retrieve file)</listitem>
<listitem>mget (retrieve file(s))</listitem>
<listitem>rm (remove file(s))</listitem>
<listitem>mv (move/rename file)</listitem>
</itemizedlist>
</para>
<para><emphasis>ls</emphasis></para>
Expand Down Expand Up @@ -397,8 +398,22 @@ protected void postProcessClientBeforeConnect(T client) throws IOException {
The remote directory is provided in the <classname>file_remoteDirectory</classname> header, and the filename is
provided in the <classname>file_remoteFile</classname> header.
</para>
<para><emphasis>mv</emphasis></para>
<para>
In each case, the PATH that these commands act on is provided by the 'expression'
The <emphasis>mv</emphasis> command has no options.
</para>
<para>
The <emphasis>expression</emphasis> attribute defines the "from" path and the
<emphasis>rename-expression</emphasis> attribute defines the "to" path. By default, the
<emphasis>rename-expression</emphasis> is <code>headers['file_renameTo']</code>. This
expression must not evaluate to null, or an empty <code>String</code>. The payload of
the result message is <code>Boolean.TRUE</code>.
The original remote directory is provided in the <code>file_remoteDirectory</code> header, and the filename is
provided in the <code>file_remoteFile</code> header. The new path is in
the <code>file_renameTo</code> header.
</para>
<para>
For all commands, the PATH that the command acts on is provided by the 'expression'
property of the gateway. For the mget command, the expression might evaluate to '*', meaning
retrieve all files, or 'somedirectory/*' etc.
</para>
Expand Down
17 changes: 16 additions & 1 deletion src/reference/docbook/sftp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ xsi:schemaLocation="http://www.springframework.org/schema/integration/sftp
<listitem>get (retrieve file)</listitem>
<listitem>mget (retrieve file(s))</listitem>
<listitem>rm (remove file(s))</listitem>
<listitem>mv (move/rename file)</listitem>
</itemizedlist>
</para>
<para><emphasis>ls</emphasis></para>
Expand Down Expand Up @@ -430,8 +431,22 @@ xsi:schemaLocation="http://www.springframework.org/schema/integration/sftp
The remote directory is provided in the <classname>file_remoteDirectory</classname> header, and the filename is
provided in the <classname>file_remoteFile</classname> header.
</para>
<para><emphasis>mv</emphasis></para>
<para>
In each case, the PATH that these commands act on is provided by the 'expression'
The <emphasis>mv</emphasis> command has no options.
</para>
<para>
The <emphasis>expression</emphasis> attribute defines the "from" path and the
<emphasis>rename-expression</emphasis> attribute defines the "to" path. By default, the
<emphasis>rename-expression</emphasis> is <code>headers['file_renameTo']</code>. This
expression must not evaluate to null, or an empty <code>String</code>. The payload of
the result message is <code>Boolean.TRUE</code>.
The original remote directory is provided in the <code>file_remoteDirectory</code> header, and the filename is
provided in the <code>file_remoteFile</code> header. The new path is in
the <code>file_renameTo</code> header.
</para>
<para>
For all commands, the PATH that the command acts on is provided by the 'expression'
property of the gateway. For the mget command, the expression might evaluate to '*', meaning
retrieve all files, or 'somedirectory/*' etc.
</para>
Expand Down

0 comments on commit ce68e59

Please sign in to comment.