[#ftl attributes={"generated.type":"blueprint"}]
<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:camelcxf="http://camel.apache.org/schema/blueprint/cxf"
           xmlns:cxf="http://cxf.apache.org/blueprint/core"
           xsi:schemaLocation="
             http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
             http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
             ">
<!-- 

	factoryId: openapi-based-rest-service-public
	factoryType: entaxy.runtime.service
	outputType: init 
	properties:
	[#list properties?keys as key]
		[=key]
	[/#list]
 -->

    [#if properties.authorizationType == "JAAS"]
    <reference id="systemInterceptor" interface="org.apache.cxf.phase.PhaseInterceptor"
               filter="(type=system)"/>
    <reference id="authInterceptor" interface="org.apache.cxf.phase.PhaseInterceptor"
               filter="(type=authentication)"/>
	<reference id="serviceInterceptor" interface="org.apache.cxf.phase.PhaseInterceptor"
               filter="(type=service)"
               timeout="30000" availability="mandatory"/>
    <bean id="resolveLoginInterceptor" class="ru.entaxy.platform.services.runtime.interceptor.ResolveLoginInterceptor" />
	[/#if]


    <!-- JAXRS providers -->
	<bean id="jsonProviderFactory" class="ru.entaxy.platform.core.objects.service.producing.JacksonJsonProviderFactory"/>
	<bean id="jsonProvider" factory-ref="jsonProviderFactory" factory-method="create"/>
   
    <!-- CXF OpenApiFeature -->  
    <bean id="openApiFeature" class="org.apache.cxf.jaxrs.openapi.OpenApiFeature">
        <property name="runAsFilter" value="true" />
        <property name="useContextBasedConfig" value="true" />
        <property name="scan" value="true" />
    </bean> 
    
    <!-- CXF Bus -->
    <cxf:bus bus="cxf-local-[=objectId]" name="cxf-local-[=objectId]">
        <cxf:properties key-type="java.lang.String" value-type="java.lang.Object">
            <entry key="org.apache.cxf.common.util.ClassUnwrapper" value-ref="class-unwrapper-[=objectId]" />
        </cxf:properties>
    </cxf:bus>

	[#if properties._serviceMetadata.classes.apis??]	
	[#list 0..properties._serviceMetadata.classes.apis?size-1 as i]
	<bean id="proxy-bean-[=objectId]-[=i]" activation="eager" class="ru.entaxy.platform.core.support.runtime.cxf.EntaxyCxfServiceClassProxyBean">
		<property name="realClassName" value="[=properties._serviceMetadata.classes.apis[i]]"></property>
	</bean>	
	[/#list]
	[/#if]

	<bean id="class-unwrapper-[=objectId]" activation="eager" class="ru.entaxy.platform.core.support.runtime.cxf.EntaxyCxfClassResourceUnwrapper" 
			init-method="activate">
		<property name="resourceFolders">
			<list value-type="java.lang.String">
				<value>service-resources:[=properties._serviceMetadata.serviceResourcesRoot]/[=properties._serviceMetadata.classes.folder]/</value>
			</list>
		</property>
		<property name="useCache" value="true"></property>
		<property name="bus" ref="cxf-local-[=objectId]"></property>
		<property name="bundleContext" ref="blueprintBundleContext"></property>
	</bean>

<!-- 
	<bean id="[=objectId]-connector-registry" class="ru.entaxy.platform.services.runtime.impl.EntaxyServiceConnectorRegistry" activation="eager">
		<property name="profileNameHeaders">
			<list value-type="java.lang.String">
				<value>X-SystemName</value>
			</list>
		</property>
	</bean>

	<reference-list id="connectorList"
                    interface="ru.entaxy.platform.services.runtime.EntaxyServiceConnectorInfo"
                    filter="(service=[=objectId])" availability="optional">
        <reference-listener ref="[=objectId]-connector-registry"
                            bind-method="register" unbind-method="unregister"/>
    </reference-list>
-->

    <camelcxf:rsServer id="[=objectId]-rs-server"
                       address="/[=properties.address]"
                       loggingFeatureEnabled="true" 
                       loggingSizeLimit="50"
                       bus="cxf-local-[=objectId]">
		<camelcxf:serviceBeans>
		[#if properties._serviceMetadata.classes.apis??]	
		[#list 0..properties._serviceMetadata.classes.apis?size-1 as i]
			<ref component-id="proxy-bean-[=objectId]-[=i]"/>
		[/#list]
		[/#if]
		</camelcxf:serviceBeans>   
		<camelcxf:providers>
			<ref component-id="jsonProvider"/>
		</camelcxf:providers>
		<camelcxf:features>
			<ref component-id="openApiFeature"/>
		</camelcxf:features>
        <camelcxf:inInterceptors>
		[#if properties.authorizationType == "JAAS"]
            <ref component-id="authInterceptor"/>
            <ref component-id="systemInterceptor"/>
            <ref component-id="resolveLoginInterceptor"/>
    	[/#if]
        </camelcxf:inInterceptors>
    </camelcxf:rsServer>

    <camelContext id="openapi-[=objectId]-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="[=objectId]-cxf-rs-endpoint" streamCache="true">
            <from uri="cxfrs:bean:[=objectId]-rs-server?bindingStyle=SimpleConsumer"/>
            <setHeader name="ENTAXY_EndpointName">
                <simple>[=objectId]</simple>
            </setHeader>
            <setHeader name="NTX_ERROR_HANDLER_SYNC_TYPE">
                <constant>application/json</constant>
            </setHeader>
            
			[#include "templates:service-components/generateLoggingKey.ftl"]
            
            <to uri="direct:[=objectId]-operation-router" />
        </route>
        
    </camelContext>

</blueprint> 
