<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~~~~~~licensing~~~~~~
  permission-soap
  ==========
  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~~~~~~
  -->

<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:cxf="http://cxf.apache.org/blueprint/core"
           xmlns:camelcxf="http://camel.apache.org/schema/blueprint/cxf"
           xmlns:p="http://www.entaxy.ru/permission/"
           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
             ">

    <cm:property-placeholder persistent-id="ru.entaxy.esb.system.permission" update-strategy="reload">
        <cm:default-properties>
            <cm:property name="endpoint.address" value="/permission-management"/>
        </cm:default-properties>
    </cm:property-placeholder>

    <reference id="permissionService"
               interface="ru.entaxy.esb.system.core.permission.jpa.PermissionService"
               timeout="30000"
               availability="mandatory"/>

    <reference id="serviceInterceptor"
               interface="org.apache.cxf.phase.PhaseInterceptor"
               filter="(type=service)"
               timeout="30000"/>
    <reference id="authInterceptor" interface="org.apache.cxf.phase.PhaseInterceptor"
               filter="(type=authentication)"/>

    <camelcxf:cxfEndpoint id="permission-endpoint"
                          address="${endpoint.address}"
                          endpointName="p:permission-serviceSOAP"
                          serviceName="p:permission-service"
                          wsdlURL="wsdl/permission-service.wsdl">
        <camelcxf:properties>
            <entry key="dataFormat" value="PAYLOAD"/>
            <entry key="mtom-enabled" value="false"/>
        </camelcxf:properties>
        <camelcxf:inInterceptors>
            <ref component-id="authInterceptor"/>
            <ref component-id="serviceInterceptor"/>
        </camelcxf:inInterceptors>
    </camelcxf:cxfEndpoint>

    <camelContext id="permission-context" xmlns="http://camel.apache.org/schema/blueprint">

        <route id="permission-endpoint" streamCache="true">
            <from uri="cxf:bean:permission-endpoint"/>
            <log message="${body}" loggingLevel="INFO"/>

            <toD uri="direct:${headers.operationName}"/>
        </route>

        <route id="check">
            <from uri="direct:check"/>
            <doTry>
                <to uri="direct:preparePermissionCommon"/>

                <to uri="bean-fix:permissionService?method=existByAllParameters(${exchangeProperty.objectId}, ${exchangeProperty.objectType},
                 	${exchangeProperty.subjectId}, ${exchangeProperty.subjectType}, ${exchangeProperty.action})"/>

                <log message="\r\nRESPONSE ${body}\r\n" loggingLevel="DEBUG"/>
                <setBody>
                    <simple>&lt;response xmlns="http://www.entaxy.ru/permission/"&gt;${body}&lt;/response&gt;</simple>
                </setBody>
                <doCatch>
                    <exception>java.lang.Exception</exception>
                    <log message="${exception.message}" loggingLevel="ERROR"/>
                    <log message="${exception.stacktrace}" loggingLevel="DEBUG"/>
                    <setBody>
                        <constant>&lt;response xmlns="http://www.entaxy.ru/permission/"&gt;false&lt;/response&gt;
                        </constant>
                    </setBody>
                </doCatch>
            </doTry>
        </route>

        <route id="get">
            <from uri="direct:get"/>

            <setProperty name="permissionId">
                <xpath resultType="long">//p:permissionId</xpath>
            </setProperty>

            <to uri="bean-fix:permissionService?method=get(${exchangeProperty.permissionId})"/>

            <log message="\r\nGet permission ${body}\r\n" loggingLevel="DEBUG"/>
            <to uri="direct:modelToHeaders"/>
            <to uri="xslt:ru/entaxy/esb/system/core/permission/template/permission.xsl?failOnNullBody=false"/>
            <removeHeaders pattern="model_*"/>

            <log message="\r\nRESPONSE ${body}\r\n" loggingLevel="DEBUG"/>
        </route>

        <route id="getByAllParams">
            <from uri="direct:getByAllParams"/>
            <to uri="direct:preparePermissionCommon"/>

            <to uri="bean-fix:permissionService?method=getByAllParameters(${exchangeProperty.objectId}, ${exchangeProperty.objectType},
                 	${exchangeProperty.subjectId}, ${exchangeProperty.subjectType}, ${exchangeProperty.action})"/>

            <to uri="direct:modelToHeaders"/>
            <to uri="xslt:ru/entaxy/esb/system/core/permission/template/permission.xsl?failOnNullBody=false"/>
            <removeHeaders pattern="model_*"/>
            <log message="\r\nRESPONSE ${body}\r\n" loggingLevel="DEBUG"/>
        </route>

        <route id="create">
            <from uri="direct:create"/>
            <to uri="direct:preparePermissionCommon"/>

            <to uri="bean-fix:permissionService?method=add(${exchangeProperty.objectId}, ${exchangeProperty.objectType},
                 	${exchangeProperty.subjectId}, ${exchangeProperty.subjectType}, ${exchangeProperty.action})"/>

            <to uri="direct:modelToHeaders"/>
            <to uri="xslt:ru/entaxy/esb/system/core/permission/template/permission.xsl?failOnNullBody=false"/>
            <removeHeaders pattern="model_*"/>
            <log message="\r\nRESPONSE ${body}\r\n" loggingLevel="DEBUG"/>
        </route>

        <route id="update">
            <from uri="direct:update"/>
            <setProperty name="permissionId">
                <xpath resultType="long">//p:permission/p:permissionId</xpath>
            </setProperty>
            <setProperty name="objectId">
                <xpath resultType="long">//p:permission/p:objectId</xpath>
            </setProperty>
            <setProperty name="objectType">
                <xpath resultType="String">//p:permission/p:objectType</xpath>
            </setProperty>
            <setProperty name="subjectId">
                <xpath resultType="String">//p:permission/p:subjectId</xpath>
            </setProperty>
            <setProperty name="subjectType">
                <xpath resultType="String">//p:permission/p:subjectType</xpath>
            </setProperty>
            <setProperty name="action">
                <xpath resultType="String">//p:permission/p:action</xpath>
            </setProperty>

            <to uri="bean-fix:permissionService?method=update(${exchangeProperty.permissionId}, ${exchangeProperty.objectId}, ${exchangeProperty.objectType},
                 	${exchangeProperty.subjectId}, ${exchangeProperty.subjectType}, ${exchangeProperty.action})"/>

            <to uri="direct:modelToHeaders"/>
            <to uri="xslt:ru/entaxy/esb/system/core/permission/template/permission.xsl?failOnNullBody=false"/>
            <removeHeaders pattern="model_*"/>
            <log message="\r\nRESPONSE ${body}\r\n" loggingLevel="DEBUG"/>
        </route>

        <route id="delete">
            <from uri="direct:delete"/>
            <to uri="direct:preparePermissionCommon"/>

            <to uri="bean-fix:permissionService?method=remove(${exchangeProperty.objectId}, ${exchangeProperty.objectType},
             	${exchangeProperty.subjectId}, ${exchangeProperty.subjectType}, ${exchangeProperty.action})"/>

            <setBody>
                <simple>&lt;response xmlns="http://www.entaxy.ru/permission/"&gt;true&lt;/response&gt;</simple>
            </setBody>
        </route>


        <route>
            <from uri="direct:modelToProperties"/>
            <setProperty name="permissionId">
                <simple>${body.id}</simple>
            </setProperty>
            <setProperty name="objectId">
                <simple>${body.objectId}</simple>
            </setProperty>
            <setProperty name="objectType">
                <simple>${body.objectType}</simple>
            </setProperty>
            <setProperty name="subjectId">
                <simple>${body.subjectId}</simple>
            </setProperty>
            <setProperty name="subjectType">
                <simple>${body.subjectType}</simple>
            </setProperty>
            <setProperty name="action">
                <simple>${body.action}</simple>
            </setProperty>
        </route>

        <route>
            <from uri="direct:modelToHeaders"/>
            <setHeader name="model_permissionId">
                <simple>${body.id}</simple>
            </setHeader>
            <setHeader name="model_objectId">
                <simple>${body.objectId}</simple>
            </setHeader>
            <setHeader name="model_objectType">
                <simple>${body.objectType}</simple>
            </setHeader>
            <setHeader name="model_subjectId">
                <simple>${body.subjectId}</simple>
            </setHeader>
            <setHeader name="model_subjectType">
                <simple>${body.subjectType}</simple>
            </setHeader>
            <setHeader name="model_action">
                <simple>${body.action}</simple>
            </setHeader>
        </route>

        <route>
            <from uri="direct:preparePermissionCommon"/>
            <setProperty name="objectId">
                <xpath resultType="long">//p:permissionCommon/p:objectId</xpath>
            </setProperty>
            <setProperty name="objectType">
                <xpath resultType="String">//p:permissionCommon/p:objectType</xpath>
            </setProperty>
            <setProperty name="subjectId">
                <xpath resultType="String">//p:permissionCommon/p:subjectId</xpath>
            </setProperty>
            <setProperty name="subjectType">
                <xpath resultType="String">//p:permissionCommon/p:subjectType</xpath>
            </setProperty>
            <setProperty name="action">
                <xpath resultType="String">//p:permissionCommon/p:action</xpath>
            </setProperty>

            <log message="\r\nobjectId ${exchangeProperty.objectId}\r\n" loggingLevel="DEBUG"/>
            <log message="\r\nobjectType ${exchangeProperty.objectType}\r\n" loggingLevel="DEBUG"/>
            <log message="\r\nsubjectId ${exchangeProperty.subjectId}\r\n" loggingLevel="DEBUG"/>
            <log message="\r\nsubjectType ${exchangeProperty.subjectType}\r\n" loggingLevel="DEBUG"/>
            <log message="\r\naction ${exchangeProperty.action}\r\n" loggingLevel="DEBUG"/>
        </route>
    </camelContext>
</blueprint>
