<#--
 ~~~~~~licensing~~~~~~
 connector
 ==========
 Copyright (C) 2020 - 2021 EmDev LLC
 ==========
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 ~~~~~~/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="soapOutConnector" class="ru.entaxy.esb.system.profile.commons.connectors.out.QueueOutConnectorImpl">
        <property name="endpointName" value="uniform-service"/>
        <property name="systemName" value="[=systemName]"/>
        <property name="params">
            <map>
                <entry key="isActive" value="[=isActive]"/>
                <entry key="username" value="[=username]"/>
                <entry key="password" value="[=password]"/>
                <entry key="endpointUrl" value="[=endpointUrl]"/>
                <entry key="connectionTimeout" value="[=connectionTimeout]"/>
                <entry key="connectionMaxThreads" value="[=connectionMaxThreads]"/>
            </map>
        </property>
    </bean>

    <service ref="soapOutConnector" auto-export="interfaces">
        <service-properties>
            <entry key="endpointName" value="uniform-service"/>
            <entry key="systemName" value="[=systemName]"/>
        </service-properties>
    </service>

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

    <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-out-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"/>

        <#if isActive == "true">
            <route id="soap-out-active-connector" streamCache="true">
                <from uri="entaxy-broker:queue:entaxy.uniform.service.exchange.[=systemName]?exchangePattern=InOnly"/>
                <log message="Message ${headers} send to system" loggingLevel="TRACE"/>
                <setHeader name="NTX_SystemId">
                    <constant>[=systemName]</constant>
                </setHeader>
                <setHeader name="ENTAXY_SystemEndpoint">
                    <constant>[=endpointUrl]</constant>
                </setHeader>

                <log message="ACTIVE-CONSUMER: Message for system ${headers.NTX_SystemId} to ${headers.ENTAXY_SystemEndpoint}"
                     loggingLevel="DEBUG"/>
                <when>
                    <simple>${headers.ENTAXY_SystemEndpoint} == null || ${headers.NTX_SystemId} == null</simple>
                    <log message="ENTAXY_SystemEndpoint for ${headers.NTX_SystemId} is not set"
                         loggingLevel="ERROR"/>
                    <stop/>
                </when>
                <bean ref="headersConverter" method="xml2Json"/>

                <setHeader name="NTX_OutQueue">
                    <simple>${headers.JMSDestination.name}</simple>
                </setHeader>
                <setHeader name="ENTAXY_AcknowledgeMsgID">
                    <simple>${bean:uuidGenerator.toString}</simple>
                </setHeader>

                <to uri="direct-vm:uniform-service-common-save-acknowledge?block=true&amp;timeout=70000"/>

                <setBody>
                    <simple><![CDATA[
                        <body>${body}</body>
                        ]]>
                    </simple>
                </setBody>
                <to uri="xslt:ru/entaxy/esb/platform/runtime/modules/uniform-service/xslt/PutPacketRequest.xsl"/>
                <setHeader name="operationNamespace">
                    <constant>http://www.entaxy.ru/connector/universal/system/1.0</constant>
                </setHeader>
                <setHeader name="operationName">
                    <constant>PutPackets</constant>
                </setHeader>
                <setHeader name="CamelDestinationOverrideUrl">
                    <simple>${headers.ENTAXY_SystemEndpoint}</simple>
                </setHeader>

                <to uri="direct-vm:uniform-service-send-package-active"/>
                <choice>
                    <when>
                        <xpath>not(//soap-type:status/text() = '200')</xpath>
                        <log message="PutPackets fail" loggingLevel="ERROR"/>
                    </when>
                    <otherwise>
                        <setHeader name="NTX_AckMessage">
                            <simple>true</simple>
                        </setHeader>
                        <to uri="entaxy-broker:queue:entaxy.uniform.service.exchange.aggregate?exchangePattern=InOnly&amp;asyncConsumer=true"/>
                    </otherwise>
                </choice>
            </route>
        </#if>

        <route id="uniform-service-out-connector">
            <from uri="direct-vm:uniform-service-out-connector-[=systemName]"/>
            <log message="Message ${headers} send to system" loggingLevel="TRACE"/>
            <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.[=systemName]</simple>
            </setHeader>
            <removeHeaders pattern="X-.+"/>
            <toD uri="entaxy-broker:queue:${headers.NTX_OutQueue}?exchangePattern=InOnly&amp;priority=${headers.ENTAXY_Priority}"/>
        </route>
    </camelContext>

</blueprint>
