<?xml version="1.0" encoding="UTF-8"?>
<!--

    Licensed to Apereo under one or more contributor license
    agreements. See the NOTICE file distributed with this work
    for additional information regarding copyright ownership.
    Apereo 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 the following location:

      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.

-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="org.jasig.cas" />
    <context:annotation-config />

    <context:property-placeholder location="classpath:/ldap.properties"/>

    <bean id="ldapCertFetcher"
          class="org.jasig.cas.adaptors.x509.authentication.handler.support.ldap.LdaptiveResourceCRLFetcher"
          c:connectionConfig-ref="provisioningConnectionConfig"
          c:searchExecutor-ref="searchExecutor"  />

    <bean id="poolingLdapCertFetcher"
          class="org.jasig.cas.adaptors.x509.authentication.handler.support.ldap.PoolingLdaptiveResourceCRLFetcher"
          c:connectionConfig-ref="provisioningConnectionConfig"
          c:searchExecutor-ref="searchExecutor"
          c:connectionPool-ref="connectionPool"/>

    <bean id="baseDn" class="java.lang.String">
        <constructor-arg type="java.lang.String" value="${ldap.baseDn}" />
    </bean>

    <util:list id="returnAttributes">
        <value>certificateRevocationList</value>
    </util:list>

    <bean id="searchFilter" class="org.ldaptive.SearchFilter"
          p:filter="${ldap.searchfilter.cert}" />

    <bean id="searchExecutor" class="org.ldaptive.SearchExecutor"
          p:baseDn-ref="baseDn"
          p:searchFilter-ref="searchFilter"
          p:returnAttributes-ref="returnAttributes"
          p:binaryAttributes-ref="returnAttributes" />

    <bean id="provisioningConnectionFactory" class="org.ldaptive.DefaultConnectionFactory"
          p:connectionConfig-ref="provisioningConnectionConfig"
          p:provider-ref="unboundidLdapProvider"  />

    <bean id="unboundidLdapProvider"
          class="org.ldaptive.provider.unboundid.UnboundIDProvider"/>

    <bean id="provisioningConnectionConfig" class="org.ldaptive.ConnectionConfig"
          p:connectTimeout="${ldap.connectTimeout}"
          p:useStartTLS="${ldap.useStartTLS: false}"
          p:connectionInitializer-ref="bindConnectionInitializer"
          p:sslConfig-ref="provisionSslConfig"/>

    <bean id="provisionSslConfig" class="org.ldaptive.ssl.SslConfig">
        <property name="credentialConfig">
            <bean class="org.ldaptive.ssl.KeyStoreCredentialConfig" />
        </property>
    </bean>

    <bean id="bindConnectionInitializer"
          class="org.ldaptive.BindConnectionInitializer"
          p:bindDn="${ldap.managerDn}">
        <property name="bindCredential">
            <bean class="org.ldaptive.Credential"
                  c:password="${ldap.managerPassword}" />
        </property>
    </bean>

    <bean id="connectionPool"
          class="org.ldaptive.pool.BlockingConnectionPool"
          lazy-init="true"
          p:poolConfig-ref="ldapPoolConfig"
          p:blockWaitTime="${ldap.pool.blockWaitTime}"
          p:validator-ref="searchValidator"
          p:pruneStrategy-ref="pruneStrategy" />

    <bean id="pruneStrategy" class="org.ldaptive.pool.IdlePruneStrategy"
          p:prunePeriod="${ldap.pool.prunePeriod}"
          p:idleTime="${ldap.pool.idleTime}" />

    <bean id="searchValidator" class="org.ldaptive.pool.SearchValidator" />

    <bean id="ldapPoolConfig" class="org.ldaptive.pool.PoolConfig"
          p:minPoolSize="${ldap.pool.minSize}"
          p:maxPoolSize="${ldap.pool.maxSize}"
          p:validateOnCheckOut="${ldap.pool.validateOnCheckout}"
          p:validatePeriodically="${ldap.pool.validatePeriodically}"
          p:validatePeriod="${ldap.pool.validatePeriod}" />

    <bean id="validationAnnotationBeanPostProcessor" class="org.jasig.cas.util.CustomBeanValidationPostProcessor"
          p:afterInitialization="true"/>
</beans>
