[#ftl attributes={"generated.type":"blueprint"}]
[#--

    ~~~~~~licensing~~~~~~
    connector
    ==========
    Copyright (C) 2020 - 2023 EmDev LLC
    ==========
    You may not use this file except in accordance with the License Terms of
    the Copyright
    Holder located at: https://entaxy.ru/eula . All copyrights, all
    intellectual property
    rights to the Software and any copies are the property of the Copyright
    Holder. Unless
    it is explicitly allowed the Copyright Holder, the User is prohibited
    from using the
    Software for commercial purposes to provide services to third parties.

    The Copyright Holder hereby declares that the Software is provided on an
    "AS IS".
    Under no circumstances does the Copyright Holder guarantee or promise
    that the
    Software provided by him will be suitable or not suitable for the
    specific purposes
    of the User, that the Software will meet all commercial and personal
    subjective
    expectations of the User, that the Software will work properly, without
    technical
    errors, quickly and uninterruptedly.

    Under no circumstances shall the Copyright Holder or its Affiliates is
    not liable
    to the User for any direct or indirect losses of the User, his expenses
    or actual
    damage, including, downtime; loss of bussines; lost profit; lost
    earnings; loss
    or damage to data, property, etc.
    ~~~~~~/licensing~~~~~~

--]
<?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:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           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">

    <cm:property-placeholder persistent-id="uniform.service.support"
                             update-strategy="reload">
        <cm:default-properties>
            <cm:property name="redelivery.maximumRedeliveries" value="2"/>
            <cm:property name="redelivery.deadLetterQueue" value="revert.message.dead"/>
            <cm:property name="acknowledge.completion.timeout" value="600000"/>

            <cm:property name="bus.id" value="FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"/>
            <cm:property name="get.destination" value=""/>
            <cm:property name="test.empty.rate" value="20"/>
            <cm:property name="acknowledge.aggregation.repository" value="jdbcAggregationRepository"/>
            <cm:property name="error.system.name" value="error"/>
        </cm:default-properties>
    </cm:property-placeholder>

    <reference id="entaxy-broker" interface="org.apache.camel.Component"
               filter="(connection.name=entaxy-broker)"/>

    <bean id="inConnector" class="ru.entaxy.esb.system.profile.commons.connectors.in.DirectVMInConnectorImpl">
        <property name="endpointName" value="uniform-passive-service"/>
        <property name="systemName" value="[=systemName]"/>
        <property name="params">
            <map>
                <entry key="role" value="default"/>
                <entry key="type" value="uniform-service"/>
                <entry key="subtype" value="passive"/>
            </map>
        </property>
    </bean>
    <service ref="inConnector" auto-export="interfaces">
        <service-properties>
            <entry key="endpointName" value="uniform-passive-service"/>
            <entry key="systemName" value="[=systemName]"/>
        </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="uniform-service-in-connector-[=systemName]-context"
                  xmlns="http://camel.apache.org/schema/blueprint"
                  errorHandlerRef="commonErrorHandler">

        <errorHandler id="commonErrorHandler" redeliveryPolicyRef="noRedelivery"
                      type="DeadLetterChannel" deadLetterUri="direct-vm:commonErrorEndpoint"/>
        <redeliveryPolicyProfile id="noRedelivery" disableRedelivery="true"/>

        <route id="uniform-passive-service-in-connector">
            <from uri="direct-vm:uniform-passive-service-in-connector-[=systemName]"/>
            <log message="Start uniform exchange connector ${headers.operationName}" loggingLevel="DEBUG"/>
            <setHeader name="ENTAXY_ConnectorType">
                <simple>uniform-service</simple>
            </setHeader>
            <setHeader name="ENTAXY_ConnectorName">
                <simple>uniform-service-in-connector-[=systemName]</simple>
            </setHeader>
            <to uri="direct:send-packets"/>
        </route>

        <route id="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>
            <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="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="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>
                    <when>
                        <simple>${headers.ENTAXY_ExchangePattern} == 'InOnly'</simple>
                        <setExchangePattern pattern="InOnly"/>
                    </when>
                    <when>
                        <simple>${headers.ENTAXY_ExchangePattern} == 'InOut'</simple>
                        <setExchangePattern pattern="InOut"/>
                    </when>
                    <setHeader name="NTX_CustomHeaders">
                        <xpath>/soap-type:packet/soap-type:message/soap-type:customHeader</xpath>
                    </setHeader>
                    <bean ref="headersConverter" method="xml2Json"/>
                    <log message="Headers converted: ${headers}" loggingLevel="DEBUG"/>

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

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

                    <to uri="direct-vm:profile-[=systemName]-exit-dispatcher"/>
                    <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="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="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>
