19. SFTP Adapters

Spring Integration provides support for file transfer operations via SFTP

19.1 Introduction

Secure File Transfer Protocol (SFTP) is a network protocol which allows you to transfer files between two computers on the Internet over any reliable stream.

Similar to FTP, SFTP requires two actors - client and server. The protocol also requires secure channel, such as SSH, as well as visibility to client's identity throughout SFTP session.

Spring Integration supports sending and receiving files over SFTP by providing two types of clients - Inbound Channel Adapters and Outbound Channel Adapters as well as convenient namespace configuration to define these clients.

xmlns:sftp="http://www.springframework.org/schema/integration/sftp"
xsi:schemaLocation="http://www.springframework.org/schema/integration/sftp 
	http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-2.0.xsd" 

19.2 SFTP Inbound Channel Adapter

SFTP Inbound Channel Adapter is a special listener that will connect to the FTP server and will listen for the remote directory events (e.g., new file created) at which point it will initiate a file transfer.

<sftp:inbound-channel-adapter id="sftpAdapterAutoCreate"
			channel="requestChannel"
			filter="filter"
			filename-pattern="foo*.txt"
			username="oleg"
			remote-directory="ftp://foo"
			local-directory-path="file:target/foo"
			host="localhost"
			password="hello"
			port="1234"
			key-file="ker.txt"
			key-file-password="hello"
			auto-create-directories="true"
			auto-delete-remote-files-on-sync="false">
		<poller fixed-rate="1000"/>
</sftp:inbound-channel-adapter>

As you can see form the configuration above you can configure SFTP Inbound Channel Adapter via inbound-channel-adapter element while also providing values for various attributes such as username and password to connect to an FTP server, key-file attributes as well as other attributes. Please refer to the schema for more details on these attributes.

It is also important to understand that SFTP Inbound Channel Adapter is a polling consumer and therefore you must configure a poller (global or local) Once the file has been transferred a Message with java.io.File being a payload will be generated and sent to the channel identified with channel attribute.

19.3 SFTP Outbound Channel Adapter

SFTP Outbound Channel Adapter is a special MessageHandler that will connect to the FTP server and will initiate an FTP transfer for every file it will receive as a payload of the Message. It also supports several representation of the File so you are not limited to the File object. Similar to FTP outbound adapter SFTP Outbound Channel Adapter supports the following payloads: 1) java.io.File - the actual file object; 2) byte[] - byte array that represents the file contents; 3) java.lang.String - represents the file path.

<sftp:outbound-channel-adapter id="sftpAdapterAutoCreate"
			channel="requestChannel"
			filter="filter"
			filename-pattern="foo*.txt"
			username="oleg"
			remote-directory="ftp://foo"
			local-directory-path="file:target/foo"
			host="localhost"
			password="hello"
			port="1234"
			key-file="ker.txt"
			key-file-password="hello"
			auto-create-directories="true"
			auto-delete-remote-files-on-sync="false"/>

As you can see form the configuration above you can configure SFTP Outbound Channel Adapter via outbound-channel-adapter element while also providing values for various attributes such as username and password to connect to an FTP server, key-file attributes as well as other attributes. Please refer to the schema for more details on these attributes.

[Note]Note
A more practical way to configure these types of adapters would be via Spring's property placeholder (http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-placeholderconfigurer)