<?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:int-ftp="http://www.springframework.org/schema/integration/ftp"
	xmlns:int="http://www.springframework.org/schema/integration"
	xmlns:int-file="http://www.springframework.org/schema/integration/file"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/integration/file https://www.springframework.org/schema/integration/file/spring-integration-file.xsd
		http://www.springframework.org/schema/integration/ftp https://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd
		http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd">

	<bean id="extraConfig" class="org.springframework.integration.ftp.outbound.FtpServerOutboundTests$Config" />

	<int:channel id="output">
		<int:queue/>
	</int:channel>

	<int:channel id="inboundGet"/>

	<int-ftp:outbound-gateway id="getGW"
							  session-factory="ftpSessionFactory"
							  request-channel="inboundGet"
							  command="get"
							  command-options="-P"
							  expression="payload"
							  local-directory-expression="@extraConfig.targetLocalDirectoryName + #remoteDirectory.toUpperCase()"
							  local-filename-generator-expression="#remoteFileName.replaceFirst('ftpSource', 'localTarget')"
							  reply-channel="output"/>

	<int:channel id="invalidDirExpression"/>

	<int-ftp:outbound-gateway session-factory="ftpSessionFactory"
							  request-channel="invalidDirExpression"
							  command="get"
							  expression="payload"
							  local-directory-expression="T(java.io.File).separator + #remoteDirectory + '?:'"
							  reply-channel="output"/>

	<int:channel id="inboundMGet"/>

	<int-ftp:outbound-gateway session-factory="ftpSessionFactory"
							  request-channel="inboundMGet"
							  command="mget"
							  command-options="-f -P"
							  expression="payload"
							  mode="IGNORE"
							  local-directory-expression="@extraConfig.targetLocalDirectoryName + (#remoteDirectory ?: '')"
							  local-filename-generator-expression="#remoteFileName.replaceFirst('ftpSource', 'localTarget')"
							  reply-channel="output"/>

	<int:channel id="inboundMGetRecursive"/>

	<int-ftp:outbound-gateway session-factory="ftpSessionFactory"
							  request-channel="inboundMGetRecursive"
							  command="mget"
							  expression="payload"
							  command-options="-R -P"
							  mode="REPLACE_IF_MODIFIED"
							  filter="starDotTxtFilter"
							  local-directory-expression="@extraConfig.targetLocalDirectoryName + #remoteDirectory"
							  local-filename-generator-expression="#remoteFileName.replaceFirst('ftpSource', 'localTarget')"
							  reply-channel="output"/>

	<bean id="starDotTxtFilter" class="org.springframework.integration.ftp.filters.FtpSimplePatternFileListFilter">
		<constructor-arg value="*.txt" />
		<property name="alwaysAcceptDirectories" value="true" />
	</bean>

	<int:channel id="inboundMGetRecursiveFiltered"/>

	<int-ftp:outbound-gateway session-factory="ftpSessionFactory"
							  request-channel="inboundMGetRecursiveFiltered"
							  command="mget"
							  expression="payload"
							  command-options="-R"
							  filename-regex="(subFtpSource|.*1.txt)"
							  local-directory-expression="@extraConfig.targetLocalDirectoryName + #remoteDirectory"
							  local-filename-generator-expression="#remoteFileName.replaceFirst('ftpSource', 'localTarget')"
							  reply-channel="output"/>

	<int:channel id="inboundMPut"/>

	<int-ftp:outbound-gateway session-factory="ftpSessionFactory"
							  request-channel="inboundMPut"
							  command="mput"
							  auto-create-directory="true"
							  filename-pattern="*.txt"
							  expression="payload"
							  chmod="600"
							  remote-directory="ftpTarget"
							  mput-filter="sortingFilter"
							  reply-channel="output"/>

	<int:channel id="inboundMPutRecursive"/>

	<int-ftp:outbound-gateway session-factory="ftpSessionFactory"
							  request-channel="inboundMPutRecursive"
							  command="mput"
							  command-options="-R"
							  auto-create-directory="true"
							  filename-pattern="*.txt"
							  expression="payload"
							  remote-directory="ftpTarget"
							  mput-filter="sortingFilter"
							  reply-channel="output"/>

	<int:channel id="inboundMPutRecursiveFiltered"/>

	<int-ftp:outbound-gateway session-factory="ftpSessionFactory"
							  request-channel="inboundMPutRecursiveFiltered"
							  command="mput"
							  command-options="-R"
							  mput-regex="(.*1.txt|sub.*)"
							  auto-create-directory="true"
							  filename-pattern="*.txt"
							  expression="payload"
							  remote-directory="ftpTarget"
							  reply-channel="output"/>

	<int-ftp:outbound-gateway session-factory="ftpSessionFactory"
							  request-channel="inboundGetStream"
							  command="get"
							  command-options="-stream"
							  expression="payload"
							  remote-directory="ftpTarget"
							  reply-channel="stream" />

	<int:chain input-channel="stream">
		<int-file:splitter markers="true" />
		<int:payload-type-router resolution-required="false" default-output-channel="output">
			<int:mapping type="org.springframework.integration.file.splitter.FileSplitter$FileMarker"
						 channel="markers" />
		</int:payload-type-router>
	</int:chain>

	<int:service-activator input-channel="markers"
	  expression="headers['file_marker']?.equals('END') ? headers['closeableResource'].close() : null"/>

	<int:channel id="appending" />

	<int-ftp:outbound-channel-adapter id="appender"
							  session-factory="ftpSessionFactory"
							  channel="appending"
							  mode="APPEND"
							  chmod="600"
							  use-temporary-file-name="false"
							  remote-directory="ftpTarget"
							  auto-create-directory="true"
							  remote-file-separator="/"	/>

	<int:channel id="ignoring" />

	<int-ftp:outbound-channel-adapter id="ignore"
							  session-factory="ftpSessionFactory"
							  channel="ignoring"
							  mode="IGNORE"
							  remote-directory="ftpTarget"
							  auto-create-directory="true"
							  remote-file-separator="/"	/>

	<int:channel id="failing" />

	<int-ftp:outbound-channel-adapter id="fail"
							  session-factory="ftpSessionFactory"
							  channel="failing"
							  mode="FAIL"
							  remote-directory="ftpTarget"
							  auto-create-directory="true"
							  remote-file-separator="/"	/>

	<bean id="sortingFilter"
		class="org.springframework.integration.ftp.outbound.FtpServerOutboundTests$SortingFileListFilter"/>

	<int-ftp:outbound-gateway
			session-factory="ftpSessionFactory"
			session-callback="messageSessionCallback"
			request-channel="inboundCallback"
			reply-channel="output"/>

	<bean id="messageSessionCallback"
		  class="org.springframework.integration.ftp.outbound.FtpServerOutboundTests$TestMessageSessionCallback"/>

	<int-ftp:outbound-gateway session-factory="ftpSessionFactory"
							  request-channel="inboundLs"
							  command="ls"
							  command-options="-1"
							  reply-channel="output"/>

	<int-ftp:outbound-gateway session-factory="ftpSessionFactory"
							  request-channel="inboundNlst"
							  command="nlst"
							  working-dir-expression="'ftpSource'"
							  reply-channel="output"/>

	<int-ftp:inbound-channel-adapter id="ftpInbound"
									 channel="output"
									 auto-startup="false"
									 session-factory="ftpSessionFactory"
									 auto-create-local-directory="true"
									 delete-remote-files="false"
									 filename-pattern="*.txt"
									 temporary-file-suffix=".foo"
									 local-directory="#{T (System).getProperty('java.io.tmpdir')
															 + T(java.io.File).separator
															 + T (java.util.UUID).randomUUID().toString()}">
		<int:poller fixed-delay="100"/>
	</int-ftp:inbound-channel-adapter>

</beans>
