[#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]
    <reference id="entaxy-broker" interface="org.apache.camel.Component"
               filter="(connection.name=entaxy-broker)"/>

    <bean id="uuidGenerator" class="java.util.UUID" scope="prototype" factory-method="randomUUID"/>

    <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="out"/>
        <property name="endpoint" value="direct-vm:[=objectId].uniform-exchange.entry-point.for-get"/>
    </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-out"/>
            <entry key="direction" value="out"/>
        </service-properties>
    </service>

    <bean id="headerMergeAggregator" class="ru.entaxy.esb.system.common.aggregation.HeaderMergeAggregatorImpl"/>

    <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.out.[=properties.profile]"/>
        </globalOptions>

        <route id="[=objectId].output-route">
            <from uri="direct:entry-cascade-finish"/>
            <when>
                <simple>${headers.ENTAXY_Priority} == null || ${headers.ENTAXY_Priority} == ""</simple>
                <setHeader name="ENTAXY_Priority">
                    <simple resultType="Integer">4</simple>
                </setHeader>
            </when>
            <setHeader name="NTX_OutQueue">
                <simple>entaxy.uniform.service.exchange.[=properties.profile]</simple>
            </setHeader>
            <removeHeaders pattern="X-.+"/>
            <log message="${headers.NTX_loggingKey} Message sent to broker to queue:${headers.NTX_OutQueue} "
                 loggingLevel="INFO"/>
            <log message="${headers.NTX_loggingKey} Body is: ${body}" loggingLevel="TRACE"/>
            <toD uri="entaxy-broker:queue:${headers.NTX_OutQueue}?priority=${headers.ENTAXY_Priority}"/>
        </route>

        <route id="[=objectId].input-route">
            <from uri="direct-vm:[=objectId].uniform-exchange.entry-point.for-get"/>
            <choice>
                <when>
                    <simple>${headers.operationName} == 'ConfirmGettingPackets'</simple>
                    <to uri="direct:confirm-packets"/>
                </when>
                <when>
                    <simple>${headers.operationName} == 'GetPackets'</simple>
                    <to uri="direct:get-packets"/>
                </when>
                <otherwise>
                    <to uri="direct:sent-message"/>
                </otherwise>
            </choice>
        </route>

        <route id="[=objectId].confirm-packets">
            <from uri="direct:confirm-packets"/>

            <setHeader name="NTX_UuidCount">
                <xpath resultType="Integer">count(/soap-type:uuids/soap-type:uuid)</xpath>
            </setHeader>
            <when>
                <simple>${headers.NTX_UuidCount} &gt; 1</simple>
                <log message="${headers.NTX_loggingKey} Not supported more then 1 uuid" loggingLevel="WARN"/>
                <throwException exceptionType="java.lang.UnsupportedOperationException"
                                message="Not supported more then 1 uuid"/>
            </when>
            <setBody>
                <xpath resultType="org.w3c.dom.Document">/soap-type:uuids/soap-type:uuid[1]</xpath>
            </setBody>

            <setHeader name="ENTAXY_AcknowledgeMsgID">
                <xpath resultType="String">/soap-type:uuid/text()</xpath>
            </setHeader>
            <to uri="direct-vm:uniform-service-receive-acknowledge"/>
            <setHeader name="NTX_UUID">
                <simple>${headers.ENTAXY_AcknowledgeMsgID}</simple>
            </setHeader>
            <to uri="xslt:ru/entaxy/esb/platform/runtime/modules/uniform-service/xslt/ConfirmGettingPacketResponse.xsl?failOnNullBody=false"/>
            <removeHeaders pattern="Authorization|breadcrumbId|NTX_.+|ENTAXY_.+"/>
        </route>

        <route id="[=objectId].get-packets">
            <from uri="direct:get-packets"/>
            <setHeader name="NTX_SystemId">
                <xpath>/soap-type:getPacketsRequest/soap-type:destination/soap-type:id/text()</xpath>
            </setHeader>
            <choice>
                <when>
                    <simple>${headers.NTX_SystemId} == ''</simple>
                    <setHeader name="NTX_SystemId">
                        <simple>${headers.X-SystemName}</simple>
                    </setHeader>
                </when>
                <when>
                    <simple>${headers.NTX_SystemId} != ${headers.X-SystemName}</simple>
                    <log message="${headers.NTX_loggingKey} Source name header and SystemId is not equals!" loggingLevel="WARN"/>
                    <throwException exceptionType="java.security.AccessControlException" message="Unauthorized"/>
                </when>
            </choice>

            <setHeader name="NTX_OutQueue">
                <simple>entaxy.uniform.service.exchange.[=properties.profile]</simple>
            </setHeader>
            <setHeader name="NTX_LimitCount">
                <xpath resultType="Integer">/soap-type:getPacketsRequest/soap-type:limitCount/text()</xpath>
            </setHeader>
            <setHeader name="NTX_LimitSize">
                <xpath resultType="Integer">/soap-type:getPacketsRequest/soap-type:limitSize/text()</xpath>
            </setHeader>
            <when>
                <simple>${headers.NTX_LimitCount} &gt; 1 || ${headers.NTX_LimitSize} &gt; 0</simple>
                <log message="${headers.NTX_loggingKey} Not supported limitation mode! Only default values." loggingLevel="WARN"/>
                <throwException exceptionType="java.lang.UnsupportedOperationException"
                                message="Not supported limitation mode! Only default values."/>
            </when>

            <log message="${headers.NTX_loggingKey} Fetch From JMS queue: ${headers.NTX_OutQueue}; SystemId=${headers.NTX_SystemId}"
                 loggingLevel="INFO"/>

            <setBody>
                <simple>${null}</simple>
            </setBody>

            <pollEnrich timeout="0" strategyRef="headerMergeAggregator">
                <simple>entaxy-broker:queue:${headers.NTX_OutQueue}</simple>
            </pollEnrich>

            <choice>
                <when>
                    <simple>${body} == null</simple>
                    <log message="${headers.NTX_loggingKey} No Messages" loggingLevel="WARN"/>
                    <setHeader name="NTX_Status">
                        <constant>204</constant>
                    </setHeader>
                    <to uri="xslt:ru/entaxy/esb/platform/runtime/modules/uniform-service/xslt/GetPacketResponse.xsl?failOnNullBody=false"/>
                    <removeHeaders pattern="NTX_Status"/>
                </when>
                <otherwise>
                    <log message="${headers.NTX_loggingKey} Get Message ${body}" loggingLevel="DEBUG"/>
                    <setHeader name="ENTAXY_AcknowledgeMsgID">
                        <simple>${bean:uuidGenerator.toString}</simple>
                    </setHeader>
                    <to uri="direct-vm:uniform-service-common-save-acknowledge?block=true&amp;timeout=70000"/>

                    <bean ref="headersConverter" method="json2xml"/>
                    <when>
                        <simple>${headers.CamelJms_IntMessageType} == 'String'</simple>
                        <bean beanType="org.apache.commons.lang3.StringEscapeUtils"
                              method="escapeXml(${body})"/>
                    </when>
                    <setBody>
                        <simple><![CDATA[<body>${body}</body>]]></simple>
                    </setBody>

                    <to uri="xslt:ru/entaxy/esb/platform/runtime/modules/uniform-service/xslt/GetPacketResponse.xsl"/>
                    <to uri="direct-vm:uniform-service-transform-body?block=true&amp;timeout=30000"/>
                    <log message="${headers.NTX_loggingKey} Send message ${headers.ENTAXY_MessageUUID} for ${headers.NTX_SystemId} ackId ${headers.ENTAXY_AcknowledgeMsgID}"
                         loggingLevel="INFO"/>
                    <removeHeaders pattern="JMS.+|NTX_.+|ENTAXY_.+"/>
                </otherwise>
            </choice>
        </route>
    </camelContext>
</blueprint>
