Search in sources :

Example 1 with DefaultConnectionFactory

use of org.ldaptive.DefaultConnectionFactory in project cas by apereo.

the class Beans method newLdaptiveBlockingConnectionPool.

/**
     * New blocking connection pool connection pool.
     *
     * @param l the l
     * @return the connection pool
     */
public static ConnectionPool newLdaptiveBlockingConnectionPool(final AbstractLdapProperties l) {
    final DefaultConnectionFactory bindCf = newLdaptiveConnectionFactory(l);
    final PoolConfig pc = newLdaptivePoolConfig(l);
    final BlockingConnectionPool cp = new BlockingConnectionPool(pc, bindCf);
    cp.setBlockWaitTime(newDuration(l.getBlockWaitTime()));
    cp.setPoolConfig(pc);
    final IdlePruneStrategy strategy = new IdlePruneStrategy();
    strategy.setIdleTime(newDuration(l.getIdleTime()));
    strategy.setPrunePeriod(newDuration(l.getPrunePeriod()));
    cp.setPruneStrategy(strategy);
    switch(l.getValidator().getType().trim().toLowerCase()) {
        case "compare":
            final CompareRequest compareRequest = new CompareRequest();
            compareRequest.setDn(l.getValidator().getDn());
            compareRequest.setAttribute(new LdapAttribute(l.getValidator().getAttributeName(), l.getValidator().getAttributeValues().toArray(new String[] {})));
            compareRequest.setReferralHandler(new SearchReferralHandler());
            cp.setValidator(new CompareValidator(compareRequest));
            break;
        case "none":
            LOGGER.debug("No validator is configured for the LDAP connection pool of [{}]", l.getLdapUrl());
            break;
        case "search":
        default:
            final SearchRequest searchRequest = new SearchRequest();
            searchRequest.setBaseDn(l.getValidator().getBaseDn());
            searchRequest.setSearchFilter(new SearchFilter(l.getValidator().getSearchFilter()));
            searchRequest.setReturnAttributes(ReturnAttributes.NONE.value());
            searchRequest.setSearchScope(l.getValidator().getScope());
            searchRequest.setSizeLimit(1L);
            searchRequest.setReferralHandler(new SearchReferralHandler());
            cp.setValidator(new SearchValidator(searchRequest));
            break;
    }
    cp.setFailFastInitialize(l.isFailFast());
    if (StringUtils.isNotBlank(l.getPoolPassivator())) {
        final AbstractLdapProperties.LdapConnectionPoolPassivator pass = AbstractLdapProperties.LdapConnectionPoolPassivator.valueOf(l.getPoolPassivator().toUpperCase());
        switch(pass) {
            case CLOSE:
                cp.setPassivator(new ClosePassivator());
                break;
            case BIND:
                final BindRequest bindRequest = new BindRequest();
                bindRequest.setDn(l.getBindDn());
                bindRequest.setCredential(new Credential(l.getBindCredential()));
                cp.setPassivator(new BindPassivator(bindRequest));
                break;
            default:
                break;
        }
    }
    LOGGER.debug("Initializing ldap connection pool for [{}] and bindDn [{}]", l.getLdapUrl(), l.getBindDn());
    cp.initialize();
    return cp;
}
Also used : DefaultConnectionFactory(org.ldaptive.DefaultConnectionFactory) IdlePruneStrategy(org.ldaptive.pool.IdlePruneStrategy) SearchRequest(org.ldaptive.SearchRequest) MongoCredential(com.mongodb.MongoCredential) Credential(org.ldaptive.Credential) ClosePassivator(org.ldaptive.pool.ClosePassivator) BindRequest(org.ldaptive.BindRequest) BlockingConnectionPool(org.ldaptive.pool.BlockingConnectionPool) SearchFilter(org.ldaptive.SearchFilter) AbstractLdapProperties(org.apereo.cas.configuration.model.support.ldap.AbstractLdapProperties) CompareRequest(org.ldaptive.CompareRequest) SearchValidator(org.ldaptive.pool.SearchValidator) CompareValidator(org.ldaptive.pool.CompareValidator) BindPassivator(org.ldaptive.pool.BindPassivator) LdapAttribute(org.ldaptive.LdapAttribute) PoolConfig(org.ldaptive.pool.PoolConfig) SearchReferralHandler(org.ldaptive.referral.SearchReferralHandler)

Example 2 with DefaultConnectionFactory

use of org.ldaptive.DefaultConnectionFactory in project cas by apereo.

the class LdapUtils method newLdaptiveConnectionFactory.

/**
 * New connection factory connection factory.
 *
 * @param l the l
 * @return the connection factory
 */
public static DefaultConnectionFactory newLdaptiveConnectionFactory(final AbstractLdapProperties l) {
    LOGGER.debug("Creating LDAP connection factory for [{}]", l.getLdapUrl());
    final ConnectionConfig cc = newLdaptiveConnectionConfig(l);
    final DefaultConnectionFactory bindCf = new DefaultConnectionFactory(cc);
    if (l.getProviderClass() != null) {
        try {
            final Class clazz = ClassUtils.getClass(l.getProviderClass());
            bindCf.setProvider(Provider.class.cast(clazz.getDeclaredConstructor().newInstance()));
        } catch (final Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    return bindCf;
}
Also used : DefaultConnectionFactory(org.ldaptive.DefaultConnectionFactory) UtilityClass(lombok.experimental.UtilityClass) ConnectionConfig(org.ldaptive.ConnectionConfig) LdapException(org.ldaptive.LdapException) Provider(org.ldaptive.provider.Provider)

Example 3 with DefaultConnectionFactory

use of org.ldaptive.DefaultConnectionFactory in project pac4j by pac4j.

the class LdaptiveAuthenticatorBuilder method newConnectionFactory.

/**
 * New connection factory connection factory.
 *
 * @param l the l
 * @return the connection factory
 */
public static DefaultConnectionFactory newConnectionFactory(final AbstractLdapProperties l) {
    final ConnectionConfig cc = newConnectionConfig(l);
    final DefaultConnectionFactory bindCf = new DefaultConnectionFactory(cc);
    if (l.getProviderClass() != null) {
        try {
            final Class clazz = ClassUtils.getClass(l.getProviderClass());
            bindCf.setProvider(Provider.class.cast(clazz.newInstance()));
        } catch (final Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    return bindCf;
}
Also used : DefaultConnectionFactory(org.ldaptive.DefaultConnectionFactory) ConnectionConfig(org.ldaptive.ConnectionConfig) Provider(org.ldaptive.provider.Provider)

Example 4 with DefaultConnectionFactory

use of org.ldaptive.DefaultConnectionFactory in project pac4j by pac4j.

the class LdaptiveAuthenticatorBuilder method newBlockingConnectionPool.

/**
 * New blocking connection pool connection pool.
 *
 * @param l the l
 * @return the connection pool
 */
public static ConnectionPool newBlockingConnectionPool(final AbstractLdapProperties l) {
    final DefaultConnectionFactory bindCf = newConnectionFactory(l);
    final PoolConfig pc = newPoolConfig(l);
    final BlockingConnectionPool cp = new BlockingConnectionPool(pc, bindCf);
    cp.setBlockWaitTime(newDuration(l.getBlockWaitTime()));
    cp.setPoolConfig(pc);
    final IdlePruneStrategy strategy = new IdlePruneStrategy();
    strategy.setIdleTime(newDuration(l.getIdleTime()));
    strategy.setPrunePeriod(newDuration(l.getPrunePeriod()));
    cp.setPruneStrategy(strategy);
    cp.setValidator(new SearchValidator());
    cp.setFailFastInitialize(l.isFailFast());
    if (StringUtils.isNotBlank(l.getPoolPassivator())) {
        final AbstractLdapProperties.LdapConnectionPoolPassivator pass = AbstractLdapProperties.LdapConnectionPoolPassivator.valueOf(l.getPoolPassivator().toUpperCase());
        switch(pass) {
            case CLOSE:
                cp.setPassivator(new ClosePassivator());
                break;
            case BIND:
                LOGGER.debug("Creating a bind passivator instance for the connection pool");
                final BindRequest bindRequest = new BindRequest();
                bindRequest.setDn(l.getBindDn());
                bindRequest.setCredential(new Credential(l.getBindCredential()));
                cp.setPassivator(new BindPassivator(bindRequest));
                break;
            default:
                break;
        }
    }
    LOGGER.debug("Initializing ldap connection pool for {} and bindDn {}", l.getLdapUrl(), l.getBindDn());
    cp.initialize();
    return cp;
}
Also used : DefaultConnectionFactory(org.ldaptive.DefaultConnectionFactory) IdlePruneStrategy(org.ldaptive.pool.IdlePruneStrategy) SearchValidator(org.ldaptive.pool.SearchValidator) Credential(org.ldaptive.Credential) BindPassivator(org.ldaptive.pool.BindPassivator) ClosePassivator(org.ldaptive.pool.ClosePassivator) BindRequest(org.ldaptive.BindRequest) PoolConfig(org.ldaptive.pool.PoolConfig) BlockingConnectionPool(org.ldaptive.pool.BlockingConnectionPool)

Example 5 with DefaultConnectionFactory

use of org.ldaptive.DefaultConnectionFactory in project cas by apereo.

the class LdaptiveResourceCRLFetcher method prepareConnectionFactory.

/**
 * Prepare a new LDAP connection.
 *
 * @param ldapURL the ldap uRL
 * @return connection factory
 */
protected ConnectionFactory prepareConnectionFactory(final String ldapURL) {
    val cc = ConnectionConfig.copy(this.connectionConfig);
    cc.setLdapUrl(ldapURL);
    return new DefaultConnectionFactory(cc);
}
Also used : lombok.val(lombok.val) DefaultConnectionFactory(org.ldaptive.DefaultConnectionFactory)

Aggregations

DefaultConnectionFactory (org.ldaptive.DefaultConnectionFactory)9 ConnectionConfig (org.ldaptive.ConnectionConfig)5 Provider (org.ldaptive.provider.Provider)4 lombok.val (lombok.val)3 BindRequest (org.ldaptive.BindRequest)3 Credential (org.ldaptive.Credential)3 UtilityClass (lombok.experimental.UtilityClass)2 AbstractLdapProperties (org.apereo.cas.configuration.model.support.ldap.AbstractLdapProperties)2 AttributeModification (org.ldaptive.AttributeModification)2 CompareRequest (org.ldaptive.CompareRequest)2 LdapAttribute (org.ldaptive.LdapAttribute)2 BindPassivator (org.ldaptive.pool.BindPassivator)2 BlockingConnectionPool (org.ldaptive.pool.BlockingConnectionPool)2 ClosePassivator (org.ldaptive.pool.ClosePassivator)2 IdlePruneStrategy (org.ldaptive.pool.IdlePruneStrategy)2 PoolConfig (org.ldaptive.pool.PoolConfig)2 SearchValidator (org.ldaptive.pool.SearchValidator)2 MongoCredential (com.mongodb.MongoCredential)1 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 IOException (java.io.IOException)1