[#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"
           xmlns:[=objectId]="[=properties.serviceNamespace]"
           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
             ">
<!-- xmlns:soap-type="http://www.entaxy.ru/ExchangeTypes/1.0" -->             
<!-- 

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

	[#assign finalAddress=properties.address]
	[#if !finalAddress?starts_with('http://') && !finalAddress?starts_with('https://')]
		[#if !finalAddress?starts_with('/')]
			[#assign finalAddress='/'+properties.address]
		[/#if]
	[/#if]

	<camelcxf:cxfEndpoint id="[=objectId]-cxf-endpoint"
                          address="[=finalAddress]"
                          endpointName="[=objectId]:[=properties.port]"
                          serviceName="[=objectId]:[=properties.serviceName]"
                          wsdlURL="entaxy-resource://[=properties.schemaUrl?replace(":", "/")]">
        <camelcxf:properties>
            <entry key="dataFormat" value="[=properties.dataFormat]"/>
            <entry key="mtom-enabled" value="[=properties.mtomEnabled?c]"/>
            <entry key="schema-validation-enabled" value="[=properties.schemaValidationEnabled?c]"/>
        </camelcxf:properties>
        <camelcxf:inInterceptors>
	[#if properties.authorizationType == "JAAS"]
        	<ref component-id="jaasLoginInterceptor" />
		[#if properties.restrictByRoles]
			<ref component-id="simpleAuthorizingInterceptor" />
		[/#if]
        	<ref component-id="resolveProfileNameInterceptor" />
        	<ref component-id="resolveLoginInterceptor"/>
        [#if properties.displayServiceSchema]
            <!-- ref component-id="isSchemaDisplayInterceptor" / -->
        [#else]
        	<ref component-id="disableWSDLGetInterceptor" />
        	<ref component-id="postAuthWsdlGetInterceptor" />
        [/#if]
            <!-- ref component-id="authInterceptor"/>
            <ref component-id="systemInterceptor"/ -->
    [/#if]
            <ref component-id="validator"/>
        </camelcxf:inInterceptors>
        <camelcxf:outFaultInterceptors>
            <ref component-id="handleOutFaultInterceptor"/>
        </camelcxf:outFaultInterceptors>
    </camelcxf:cxfEndpoint>

    <bean id="handleOutFaultInterceptor"
          class="ru.entaxy.esb.system.core.common.error.handler.interceptor.HandleOutFaultInterceptor">
        <argument ref="soap-[=objectId]-context"/>
        <argument value="direct:error_from_cxf"/>
    </bean>

    <bean id="validator" class="ru.entaxy.platform.services.runtime.interceptor.SOAPValidateInterceptor">
        <property name="schemaValidationEnabled" value="[=properties.schemaValidationEnabled?c]"/>
    </bean>
    [#if properties.authorizationType == "JAAS"]
	   <bean id="jaasLoginInterceptor" class="org.apache.cxf.interceptor.security.JAASLoginInterceptor">
		   <property name="contextName" value="[=properties.authorizationRealm]" />
		   <property name="roleClassifier" value="RolePrincipal"/>
		   <property name="roleClassifierType" value="classname"/>
   		   <property name="allowAnonymous" value="false"/>
		   <property name="allowNamedPrincipals" value="true"/>
	   </bean>     
	   <bean id="resolveProfileNameInterceptor" class="ru.entaxy.platform.services.runtime.interceptor.ResolveProfileNameInterceptor">
	   </bean>
	   <bean id="resolveLoginInterceptor" class="ru.entaxy.platform.services.runtime.interceptor.ResolveLoginInterceptor" />
		[#if properties.restrictByRoles]
		<bean id="simpleAuthorizingInterceptor" class="org.apache.cxf.interceptor.security.SimpleAuthorizingInterceptor">
		   <property name="methodRolesMap">
		      <map>
		      	<!-- NOT IMPLEMENTED YET -->
		        <!-- no wildcard support, names need to match exactly -->
		        <!-- entry key="addNumbers" value="ROLE_USER ROLE_ADMIN"/>
		        <entry key="divideNumbers" value="ROLE_ADMIN"/ -->
		      </map>
		   </property>
		   <!-- its possible to define global roles that apply to all WSDL operations not listed above -->
		   <property name="globalRoles" value="[=properties.globalRoles]"/>
		</bean>
		[/#if]
	   
    <!-- reference id="systemInterceptor" interface="org.apache.cxf.phase.PhaseInterceptor"
               filter="(type=system)"/>
    <reference id="authInterceptor" interface="org.apache.cxf.phase.PhaseInterceptor"
               filter="(type=authentication)"/ -->
        [#if properties.displayServiceSchema]
    <!-- bean id="isSchemaDisplayInterceptor" class="ru.entaxy.platform.services.runtime.interceptor.IsSchemaDisplayInterceptor">
        <property name="isDisplayServiceSchema" value="[=displayServiceSchema?c]"/>
    </bean -->
    	[#else]
	   <bean id="postAuthWsdlGetInterceptor" class="ru.entaxy.platform.services.runtime.interceptor.PostAuthWSDLGetInterceptor">
	   </bean> 
	   <bean id="disableWSDLGetInterceptor" class="ru.entaxy.platform.services.runtime.interceptor.DisableWSDLGetInterceptor">
	   </bean> 
        [/#if]
	[/#if]

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

        <route id="error_from_cxf">
            <from uri="direct:error_from_cxf"/>
            <log message="${headers.NTX_loggingKey} cxf message: ${body}" loggingLevel="DEBUG"/>
            <log message="${headers.NTX_loggingKey} headers: ${headers}\n" loggingLevel="DEBUG"/>
            <to uri="direct-vm:cxfErrorEndpoint"/>
        </route>

        <route id="[=objectId]-cxf-endpoint" streamCache="true">
            <from uri="cxf:bean:[=objectId]-cxf-endpoint"/>
            <setHeader name="ENTAXY_EndpointName">
                <simple>[=objectId]</simple>
            </setHeader>
            
        	[#include "templates:service-components/generateLoggingKey.ftl"]
        
            <to uri="direct:[=objectId]-operation-router" />

			[#if properties.removeAllHeadersBeforeAnswering]
				<removeHeaders pattern="*"/>
			[/#if]
			<removeProperties pattern="NTX.+|ENTAXY.+"/>
			<removeHeaders pattern="NTX.+|ENTAXY.+"/>
        </route>
        
    </camelContext>

</blueprint> 
