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

	<!-- ws://localhost:18080 -->

	<int-ip:tcp-connection-factory id="ws"
		type="server" port="18080"
		using-nio="false"
		so-timeout="600000"
		interceptor-factory-chain="interceptors"
		serializer="wsSerializer"
		deserializer="wsSerializer" />

	<int-ip:tcp-inbound-channel-adapter connection-factory="ws" channel="echoChannel" />

	<!-- wss://localhost:28080 -->

	<!-- We MUST use apply-sequence with NIO so we can resequence frames -->

	<int-ip:tcp-connection-factory id="wss"
		type="server" port="28080"
		using-nio="true" apply-sequence="true"
		so-timeout="600000"
		ssl-context-support="sslCtxSup"
		interceptor-factory-chain="interceptors"
		serializer="wsSerializer"
		deserializer="wsSerializer" />

	<bean id="sslCtxSup" class="org.springframework.integration.ip.tcp.connection.DefaultTcpSSLContextSupport">
		<constructor-arg value="key.store"/>
		<constructor-arg value="trust.store"/>
		<constructor-arg value="secret"/>
		<constructor-arg value="secret"/>
	</bean>

	<int-ip:tcp-inbound-channel-adapter connection-factory="wss" channel="addSSLHeader" />

	<int:header-enricher input-channel="addSSLHeader" output-channel="echoChannel">
		<int:header name="SSL" value="true"/>
	</int:header-enricher>

	<!-- End SSL config -->

	<bean id="wsSerializer" class="org.springframework.integration.x.ip.websocket.WebSocketSerializer">
		<property name="server" value="true" />
		<property name="validateUtf8" value="true" />
	</bean>

	<bean id="interceptors" class="org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain">
		<property name="interceptors">
			<bean class="org.springframework.integration.x.ip.websocket.WebSocketTcpConnectionInterceptorFactory" />
		</property>
	</bean>

	<bean id="service" class="org.springframework.integration.x.ip.websocket.WebSocketServerTests$DemoService" />

	<int:chain input-channel="echoChannel" output-channel="toChooseBrowser">
		<!-- payload is a DataFrame -->
		<int:service-activator expression="payload" />
	</int:chain>

	<int:header-value-router input-channel="toChooseBrowser"
							 header-name="SSL" default-output-channel="toBrowser">
		<int:mapping value="false" channel="toBrowser"/>
		<int:mapping value="true" channel="toBrowserSSL"/>
	</int:header-value-router>

	<!-- outbound send to websocket (ws:) -->

	<int:channel id="toBrowser" />

	<int-ip:tcp-outbound-channel-adapter connection-factory="ws" channel="toBrowser" />

	<!-- outbound send to websocket (wss:) -->

	<int:channel id="toBrowserSSL" />

	<int-ip:tcp-outbound-channel-adapter connection-factory="wss" channel="toBrowserSSL" />

	<int:channel id="stdOut" />

	<int-stream:stdout-channel-adapter append-newline="true" channel="stdOut" />

	<!-- Remove state after error on write -->

	<int:channel id="removeSocket" />

	<int:chain input-channel="removeSocket">
		<int:transformer expression="payload.failedMessage.headers['ip_connectionId']" />
		<int:service-activator ref="service" method="remove" />
	</int:chain>

</beans>
