<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You 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.
  -->

<blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:cxf="http://cxf.apache.org/blueprint/core"
           xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
           xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
                              http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd
                              http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd">

    <cxf:bus>
        <cxf:features>
            <cxf:logging/>
            <bean class="org.apache.cxf.metrics.MetricsFeature"/>
        </cxf:features>
    </cxf:bus>

    <bean id="cors-filter" class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter"/>
    <bean id="jacksonMapper" class="com.fasterxml.jackson.databind.ObjectMapper"/>
    <bean id="json-provider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider">
        <argument index="0" ref="jacksonMapper" type="com.fasterxml.jackson.databind.ObjectMapper"/>
    </bean>
    <bean id="jaas-filter" class="org.apache.cxf.jaxrs.security.JAASAuthenticationFilter">
        <!-- Name of the JAAS Context -->
        <property name="contextName" value="karaf"/>
        <!-- Hint to the filter on how to have Principals representing users and roles separated
             while initializing a SecurityContext -->
        <property name="rolePrefix" value="ROLE_"/>
        <property name="realmName" value="cxs"/>
    </bean>

    <bean id="exceptionMapper" class="org.apache.unomi.router.rest.ConfigRestExceptionMapper"/>

    <jaxrs:server address="/importConfiguration" id="restImportConfigurationService">
        <jaxrs:providers>
            <ref component-id="json-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
            <ref component-id="exceptionMapper"/>
        </jaxrs:providers>

        <jaxrs:serviceBeans>
            <ref component-id="importConfigurationServiceEndPoint"/>
        </jaxrs:serviceBeans>
    </jaxrs:server>

    <jaxrs:server address="/exportConfiguration" id="restExportConfigurationService">
        <jaxrs:providers>
            <ref component-id="json-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
            <ref component-id="exceptionMapper"/>
        </jaxrs:providers>

        <jaxrs:serviceBeans>
            <ref component-id="exportConfigurationServiceEndPoint"/>
        </jaxrs:serviceBeans>
    </jaxrs:server>

    <reference id="importConfigurationService" interface="org.apache.unomi.router.api.services.ImportExportConfigurationService"
            filter="(configDiscriminator=IMPORT)"/>

    <reference id="exportConfigurationService" interface="org.apache.unomi.router.api.services.ImportExportConfigurationService"
               filter="(configDiscriminator=EXPORT)"/>

    <reference id="configSharingService" interface="org.apache.unomi.api.services.ConfigSharingService"/>
    <reference id="profileExportService" interface="org.apache.unomi.router.api.services.ProfileExportService"/>
    <reference id="profileService" interface="org.apache.unomi.api.services.ProfileService"/>

    <bean id="importConfigurationServiceEndPoint" class="org.apache.unomi.router.rest.ImportConfigurationServiceEndPoint">
        <property name="importConfigurationService" ref="importConfigurationService"/>
        <property name="configSharingService" ref="configSharingService"/>
    </bean>

    <bean id="exportConfigurationServiceEndPoint" class="org.apache.unomi.router.rest.ExportConfigurationServiceEndPoint">
        <property name="exportConfigurationService" ref="exportConfigurationService"/>
        <property name="configSharingService" ref="configSharingService"/>
        <property name="profileExportService" ref="profileExportService"/>
        <property name="profileService" ref="profileService"/>
    </bean>

</blueprint>
