[#ftl attributes={"generated.type":"blueprint"}]
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		   xmlns:soap-type="http://www.entaxy.ru/ExchangeTypes/1.0"
		   xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

[#import "templates:object-commons/common-utils.ftl" as utils]

	<bean id="uniform-service-exchange-connector-info" class="ru.entaxy.platform.services.runtime.impl.EntaxyServiceConnectorInfoImpl">
		<property name="serviceId" value="uniform-service" />
		<property name="profileId" value="[=properties.profile]" />
		<property name="direction" value="in" />
		<property name="endpoint" value="direct-vm:[=objectId]-uniform-service-exchange.entry-point" />
	</bean>

	<service activation="eager" interface="ru.entaxy.platform.services.runtime.EntaxyServiceConnectorInfo"
			 ref="uniform-service-exchange-connector-info">
		<service-properties>
			<entry key="profile" value="[=properties.profile]"/>
			<entry key="service" value="uniform-service-in"/>
			<entry key="direction" value="in"/>
		</service-properties>
	</service>

	<bean id="forced" class="java.lang.IllegalArgumentException">
		<argument index="0" value="Content is invalid"/>
	</bean>

	<bean id="headersConverter" class="ru.entaxy.esb.system.common.util.HeadersConverter">
		<property name="namespace" value="http://www.entaxy.ru/ExchangeTypes/1.0"/>
		<property name="customHeaders" value="NTX_CustomHeaders"/>
		<property name="customHeaderPrefix" value="NTX_UNIFORM_EXCHANGE_CustomHeader"/>
	</bean>

	<camelContext id="[=objectId]" xmlns="http://camel.apache.org/schema/blueprint">

		<globalOptions>
			<globalOption key="CamelLogEipName" value="ru.entaxy.esb.uniform.exchange.passive.in.[=properties.profile]"/>
		</globalOptions>

		<route id="[=objectId].uniform-service-exchange.input-route">
			<from uri="direct-vm:[=objectId]-uniform-service-exchange.entry-point" />
			<log message="${headers.NTX_loggingKey} Body is ${body}" loggingLevel="TRACE"/>
			<to uri="direct:send-packets"/>
		</route>
		
		<route id="[=objectId].send-packets">
			<from uri="direct:send-packets"/>

			<setHeader name="NTX_UNIFORM_EXCHANGE_PacketCount">
				<xpath resultType="Integer">count(/soap-type:packets/soap-type:packet)</xpath>
			</setHeader>
			<when>
				<simple>${headers.NTX_UNIFORM_EXCHANGE_PacketCount} &gt; 1</simple>
				<throwException exceptionType="java.lang.UnsupportedOperationException"
								message="Not supported more then 1 packet"/>
			</when>

			<setBody>
				<xpath resultType="org.w3c.dom.Document">/soap-type:packets/soap-type:packet[1]</xpath>
			</setBody>

			<setHeader name="ENTAXY_MessageUUID">
				<xpath resultType="String">/soap-type:packet/soap-type:message/soap-type:messageUUID/text()</xpath>
			</setHeader>

			<when>
				<simple>${headers.ENTAXY_generateLoggingKey}</simple>
				<to uri="direct-vm:fill-logging-key"/>
			</when>

			<log message="${headers.NTX_loggingKey} Message-id is ${headers.ENTAXY_MessageUUID}" loggingLevel="INFO"/>
			<choice>
				<when>
					<simple>${headers.ENTAXY_MessageUUID} == "00000000-0000-0000-0000-000000000000"</simple>
					[#--                    health check --]
					<removeHeaders pattern="ENTAXY_.+|NTX_.+|X-.+"/>
					<setBody>
						<simple><![CDATA[
							<test/>
							]]>
						</simple>
					</setBody>
					<to uri="xslt:ru/entaxy/esb/platform/runtime/modules/uniform-service/xslt/PutPacketResponse.xsl?failOnNullBody=false"/>
				</when>
				<otherwise>
					<setHeader name="ENTAXY_Destination">
						<xpath resultType="String">
							/soap-type:packet/soap-type:header/soap-type:destination/soap-type:id/text()
						</xpath>
					</setHeader>
					<setHeader name="ENTAXY_DestinationType">
						<xpath resultType="String">
							/soap-type:packet/soap-type:header/soap-type:destination/soap-type:type/text()
						</xpath>
					</setHeader>

					<log message="${headers.NTX_loggingKey} Receive message ${headers.ENTAXY_MessageUUID} for ${headers.ENTAXY_Destination}"
						 loggingLevel="INFO"/>
					<setHeader name="ENTAXY_Source">
						<xpath resultType="String">
							/soap-type:packet/soap-type:message/soap-type:source/soap-type:id/text()
						</xpath>
					</setHeader>
					<setHeader name="ENTAXY_SourceType">
						<xpath resultType="String">
							/soap-type:packet/soap-type:message/soap-type:source/soap-type:type/text()
						</xpath>
					</setHeader>
					<when>
						<simple>${headers.X-SystemName} != ${headers.ENTAXY_Source}</simple>
						<log message="${headers.NTX_loggingKey} Source name header and auth username is not equals!" loggingLevel="WARN"/>
						<when>
							<simple>${headers.ENTAXY_Source} == ''</simple>
							<setHeader name="ENTAXY_Source">
								<simple>${headers.X-SystemName}</simple>
							</setHeader>
						</when>
					</when>
					<setHeader name="ENTAXY_MessageType">
						<xpath resultType="String">/soap-type:packet/soap-type:message/soap-type:messageType/text()
						</xpath>
					</setHeader>
					<setHeader name="ENTAXY_Priority">
						<xpath resultType="Integer">/soap-type:packet/soap-type:message/soap-type:priority/text()
						</xpath>
					</setHeader>
					<setHeader name="ENTAXY_ExchangePattern">
						<xpath resultType="String">/soap-type:packet/soap-type:message/soap-type:exchangePattern/text()
						</xpath>
					</setHeader>
					<choice>
						<when>
							<simple>${headers.ENTAXY_ExchangePattern} == 'InOut'</simple>
							<setExchangePattern pattern="InOut"/>
						</when>
						<otherwise>
							<setExchangePattern pattern="InOnly"/>
						</otherwise>
					</choice>
					<setHeader name="NTX_CustomHeaders">
						<xpath>/soap-type:packet/soap-type:message/soap-type:customHeader</xpath>
					</setHeader>
					<bean ref="headersConverter" method="xml2Json"/>
					<log message="${headers.NTX_loggingKey} Headers converted: ${headers}" loggingLevel="DEBUG"/>

					<to uri="direct:get-content-and-content-type"/>

					<when>
						<simple>${body} == null</simple>
						<log message="${headers.NTX_loggingKey} Content is invalid" loggingLevel="ERROR"/>
						<throwException ref="forced"/>
					</when>
					<removeHeaders pattern="*" excludePattern="NTX_.+|CamelJms.+|ENTAXY_.+|X-.+"/>

					<to uri="direct:exit-cascade-start" />
					<removeHeaders pattern="ENTAXY_.+|NTX_.+|X-.+"/>
					<setBody>
						<simple><![CDATA[
							<test/>
							]]>
						</simple>
					</setBody>

					<to uri="xslt:ru/entaxy/esb/platform/runtime/modules/uniform-service/xslt/PutPacketResponse.xsl?failOnNullBody=false"/>
				</otherwise>
			</choice>
		</route>

		<route id="[=objectId].get-content-and-content-type">
			<from uri="direct:get-content-and-content-type"/>

			<setHeader name="ENTAXY_ContentType">
				<xpath resultType="String">/soap-type:packet/soap-type:message/soap-type:contentType/text()
				</xpath>
			</setHeader>
			<choice>
				<when>
					<xpath>count(/soap-type:packet/soap-type:message/soap-type:content) = 1</xpath>
					<setHeader name="CamelJms_IntContentXsiType">
						<xpath resultType="String">
							/soap-type:packet/soap-type:message/soap-type:content/@xsi:type
						</xpath>
					</setHeader>
					<setHeader name="NTX_ContentHasNotEmptyText">
						<xpath resultType="Boolean">
							boolean(normalize-space(/soap-type:packet/soap-type:message/soap-type:content/text()))
						</xpath>
					</setHeader>
					<setHeader name="NTX_ContentChildNodesCount">
						<xpath resultType="Integer">
							count(/soap-type:packet/soap-type:message/soap-type:content/child::*)
						</xpath>
					</setHeader>
					<choice>
						<when>
							<simple>${headers.NTX_ContentChildNodesCount} == 1</simple>
							<setBody>
								<xpath resultType="org.w3c.dom.Document">
									/soap-type:packet/soap-type:message/soap-type:content/*
								</xpath>
							</setBody>
							<setHeader name="CamelJms_IntMessageType">
								<constant>Document</constant>
							</setHeader>
						</when>
						<when>
							<simple>${headers.NTX_ContentChildNodesCount} > 1</simple>
							<setBody>
								<xpath resultType="org.w3c.dom.NodeList">
									/soap-type:packet/soap-type:message/soap-type:content/*
								</xpath>
							</setBody>
							<setHeader name="CamelJms_IntMessageType">
								<constant>NodeList</constant>
							</setHeader>
						</when>
						<when>
							<simple>${headers.NTX_ContentChildNodesCount} == 0</simple>
							<choice>
								<when>
									<simple>${headers.NTX_ContentHasNotEmptyText}</simple>
									<setBody>
										<xpath resultType="String">
											/soap-type:packet/soap-type:message/soap-type:content/text()
										</xpath>
									</setBody>
									<setHeader name="CamelJms_IntMessageType">
										<constant>String</constant>
									</setHeader>
								</when>
								<otherwise>
									<setBody>
										<constant>
											<![CDATA[
											<emptyContent xmlns="http://www.entaxy.ru/connector/universal/internal"/>
											]]>
										</constant>
									</setBody>
								</otherwise>
							</choice>
						</when>
						<otherwise>
							<log message="${headers.NTX_loggingKey} Content is invalid" loggingLevel="ERROR"/>
							<throwException ref="forced"/>
						</otherwise>
					</choice>
					<removeHeader headerName="NTX_ContentHasNotEmptyText"/>
					<removeHeader headerName="NTX_ContentChildNodesCount"/>
				</when>
				<otherwise>
					<setBody>
						<constant>
							<![CDATA[
							<empty xmlns="http://www.entaxy.ru/connector/universal/internal"/>
							]]>
						</constant>
					</setBody>
				</otherwise>
			</choice>
		</route>
	</camelContext>
	
</blueprint>
