<?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:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           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://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd
  http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd">
    <cm:property-placeholder persistent-id="org.apache.unomi.rest.segmentserviceendpoint"
                             update-strategy="reload">
    </cm:property-placeholder>

    <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="org.apache.unomi.persistence.spi.CustomObjectMapper"/>
    <bean id="jaxb-provider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider">
        <argument index="0" ref="jacksonMapper" type="com.fasterxml.jackson.databind.ObjectMapper "/>
        <argument index="1" type="com.fasterxml.jackson.jaxrs.cfg.Annotations[]">
            <array>
                <value>JACKSON</value>
                <value>JAXB</value>
            </array>
        </argument>
    </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"/>
        <!-- Activate this if you want to force a redirect if auth is missing, by default it will trigger a 403 which
             is usually preferred -->
        <!--property name="redirectURI" value="/login.jsp"/-->
    </bean>

    <jaxrs:server address="/segments" id="restSegmentService">
        <jaxrs:providers>
            <ref component-id="jaxb-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
        </jaxrs:providers>

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

    <jaxrs:server address="/userList" id="restUserListService">
        <jaxrs:providers>
            <ref component-id="jaxb-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
        </jaxrs:providers>

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

    <jaxrs:server address="/scoring" id="restScoringService">
        <jaxrs:providers>
            <ref component-id="jaxb-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
        </jaxrs:providers>

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

    <jaxrs:server address="/definitions" id="restDefinitionsService">
        <jaxrs:providers>
            <ref component-id="jaxb-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
        </jaxrs:providers>

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

    <jaxrs:server address="/goals" id="restGoalsService">
        <jaxrs:providers>
            <ref component-id="jaxb-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
        </jaxrs:providers>

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

    <jaxrs:server address="/campaigns" id="restCampaignsService">
        <jaxrs:providers>
            <ref component-id="jaxb-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
        </jaxrs:providers>

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

    <jaxrs:server address="/rules" id="restRulesService">
        <jaxrs:providers>
            <ref component-id="jaxb-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
        </jaxrs:providers>

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

    <jaxrs:server address="/profiles" id="restProfileService">
        <jaxrs:providers>
            <ref component-id="jaxb-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
        </jaxrs:providers>

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

    <jaxrs:server address="/cluster" id="restClusterService">
        <jaxrs:providers>
            <ref component-id="jaxb-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
        </jaxrs:providers>

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

    <jaxrs:server address="/query" id="restQueryService">
        <jaxrs:providers>
            <ref component-id="jaxb-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
        </jaxrs:providers>

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

    <jaxrs:server address="/patches" id="restPatchService">
        <jaxrs:providers>
            <ref component-id="jaxb-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
        </jaxrs:providers>

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

    <jaxrs:server address="/events" id="restEventService">
        <jaxrs:providers>
            <ref component-id="jaxb-provider"/>
            <ref component-id="cors-filter"/>
            <ref component-id="jaas-filter"/>
        </jaxrs:providers>

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

    <reference id="segmentService" interface="org.apache.unomi.api.services.SegmentService"/>
    <reference id="userListService" interface="org.apache.unomi.api.services.UserListService"/>
    <reference id="definitionsService" interface="org.apache.unomi.api.services.DefinitionsService"/>
    <reference id="goalsService" interface="org.apache.unomi.api.services.GoalsService"/>
    <reference id="rulesService" interface="org.apache.unomi.api.services.RulesService"/>
    <reference id="profileService" interface="org.apache.unomi.api.services.ProfileService"/>
    <reference id="clusterService" interface="org.apache.unomi.api.services.ClusterService"/>
    <reference id="queryService" interface="org.apache.unomi.api.services.QueryService"/>
    <reference id="eventService" interface="org.apache.unomi.api.services.EventService"/>
    <reference id="patchService" interface="org.apache.unomi.api.services.PatchService"/>

    <bean id="segmentServiceEndPoint" class="org.apache.unomi.rest.SegmentServiceEndPoint">
        <property name="segmentService" ref="segmentService"/>
    </bean>

    <bean id="userListServiceEndPoint" class="org.apache.unomi.rest.UserListServiceEndPoint">
        <property name="userListService" ref="userListService"/>
    </bean>

    <bean id="scoringServiceEndPoint" class="org.apache.unomi.rest.ScoringServiceEndPoint">
        <property name="segmentService" ref="segmentService"/>
    </bean>

    <bean id="definitionsServiceEndPoint" class="org.apache.unomi.rest.DefinitionsServiceEndPoint">
        <property name="definitionsService" ref="definitionsService"/>
        <property name="localizationHelper" ref="localizationHelper"/>
    </bean>

    <bean id="goalsServiceEndPoint" class="org.apache.unomi.rest.GoalsServiceEndPoint">
        <property name="goalsService" ref="goalsService"/>
    </bean>

    <bean id="campaignServiceEndPoint" class="org.apache.unomi.rest.CampaignsServiceEndPoint">
        <property name="goalsService" ref="goalsService"/>
    </bean>

    <bean id="rulesServiceEndPoint" class="org.apache.unomi.rest.RulesServiceEndPoint">
        <property name="rulesService" ref="rulesService"/>
    </bean>

    <bean id="profileServiceEndPoint" class="org.apache.unomi.rest.ProfileServiceEndPoint">
        <property name="profileService" ref="profileService"/>
        <property name="eventService" ref="eventService"/>
        <property name="segmentService" ref="segmentService"/>
        <property name="localizationHelper" ref="localizationHelper"/>
    </bean>

    <bean id="clusterServiceEndPoint" class="org.apache.unomi.rest.ClusterServiceEndPoint">
        <property name="clusterService" ref="clusterService"/>
    </bean>

    <bean id="queryServiceEndPoint" class="org.apache.unomi.rest.QueryServiceEndPoint">
        <property name="queryService" ref="queryService"/>
        <property name="localizationHelper" ref="localizationHelper"/>
    </bean>

    <bean id="patchServiceEndPoint" class="org.apache.unomi.rest.PatchServiceEndPoint">
        <property name="patchService" ref="patchService"/>
    </bean>

    <bean id="eventServiceEndPoint" class="org.apache.unomi.rest.EventServiceEndpoint">
        <property name="eventService" ref="eventService"/>
    </bean>

    <bean id="resourceBundleHelper" class="org.apache.unomi.rest.ResourceBundleHelper">
        <property name="bundleContext" ref="blueprintBundleContext"/>
    </bean>

    <bean id="localizationHelper" class="org.apache.unomi.rest.LocalizationHelper">
    <property name="bundleContext" ref="blueprintBundleContext"/>
    <property name="resourceBundleHelper" ref="resourceBundleHelper"/>
  </bean>

</blueprint>
