SFTP Adapters Spring Integration provides support for file transfer operations via SFTP
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. SFTP protocol 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.
SFTP Session Factory Before configuring SFTP adapters you must configure Sftp Session Factory. You configure Sftp Session Factory via regular bean configuration: Below is a basic configuration: ]]> Now all you need to do is inject this Sftp Session Factory into your adapters. A more practical way to provide values for Sftp Session Factory 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)
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. ]]> 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 local-directory - where files are going to be transfered TO and remote-directory - the remote source directory as well as other attributes including session-factory which we configured earlier. Some times file filtering based on the simple pattern specified via filename-pattern attribute might not be sufficient enough. If this is the case, you can use filename-regex attribute to specify Regular expression (e.g. filename-regex=".*\.test$"). And of course if you need complete control you can use filter attribute and provide reference to a custom implementation of the org.springframework.integration.file.filters.FileListFilter - a strategy interface for filtering a group of files. 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 to a local directory a Message with java.io.File being a payload will be generated and sent to the channel identified with channel attribute. More on File Filtering and Large Files Some times the file that just appeared in the monitored (remote) directory is not complete. Typically such file will be written with some temporary extension (e.g., foo.txt.writing) and then renamed after the writing process finished. As a user in most cases you are only interested in files that are complete and would like to filter only files that are complete. To handle these scenarios use filtering support provided via filename-pattern, filename-regex and filter attributes. We also provide a convenient Regex-based implementation org.springframework.integration.sftp.filters.SftpPatternMatchingFileListFilter. Once configured all you need is include such filter in your adapter via filter attribute. ]]>
SFTP Outbound Channel Adapter SFTP Outbound Channel Adapter is a special MessageHandler that will connect to the remote directory and will initiate a file 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 contents. ]]> As you can see form the configuration above you can configure SFTP Outbound Channel Adapter via outbound-channel-adapter. Please refer to the schema for more details on these attributes. SpEL and SFTP Outbound Adapter As with many other components in Spring Integration, you can benefit from Spring Expression Language (SpEL) when configuring SFTP Outbound Channel Adapter, by specifying two attributes remote-directory-expression and remote-filename-generator-expression (see above). Expression evaluation context will have Message as its root object, thus allowing you to provide expressions which can dynamically compute the file name or the existing directory path based on the data in the Message. In the example above we are defining remote-filename-generator-expression attribute with expression value which computes the file name based on its original name while also appending suffix '-foo'.