Search in sources :

Example 1 with LDAPConnectionFactory

use of org.forgerock.opendj.ldap.LDAPConnectionFactory in project OpenAM by OpenRock.

the class UserIdRepo method getConnectionFactory.

private synchronized ConnectionFactory getConnectionFactory(String hostname, int port, Options options) {
    if (factory == null) {
        factory = new LDAPConnectionFactory(hostname, port, options);
        ShutdownManager.getInstance().addShutdownListener(new ShutdownListener() {

            @Override
            public void shutdown() {
                if (factory != null) {
                    factory.close();
                }
            }
        });
    }
    return factory;
}
Also used : ShutdownListener(org.forgerock.util.thread.listener.ShutdownListener) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory)

Example 2 with LDAPConnectionFactory

use of org.forgerock.opendj.ldap.LDAPConnectionFactory in project OpenAM by OpenRock.

the class AMSetupDSConfig method getLDAPConnection.

/**
     * Helper method to return Ldap connection 
     *
     * @param ssl <code>true</code> if directory server is running SSL.
     * @return Ldap connection 
     */
private synchronized Connection getLDAPConnection(boolean ssl) {
    try {
        if (ld == null) {
            ShutdownManager shutdownMan = com.sun.identity.common.ShutdownManager.getInstance();
            // All connections will use authentication
            SimpleBindRequest request = LDAPRequests.newSimpleBindRequest(dsManager, dsAdminPwd.toCharArray());
            Options options = Options.defaultOptions().set(REQUEST_TIMEOUT, new Duration((long) 3, TimeUnit.SECONDS)).set(AUTHN_BIND_REQUEST, request);
            if (ssl) {
                options = options.set(SSL_CONTEXT, new SSLContextBuilder().getSSLContext());
            }
            ld = new LDAPConnectionFactory(dsHostName, getPort(), options);
            shutdownMan.addShutdownListener(new ShutdownListener() {

                public void shutdown() {
                    disconnectDServer();
                }
            });
        }
        return ld.getConnection();
    } catch (LdapException e) {
        disconnectDServer();
        dsConfigInstance = null;
        ld = null;
    } catch (Exception e) {
        dsConfigInstance = null;
        ld = null;
    }
    return null;
}
Also used : ShutdownListener(org.forgerock.util.thread.listener.ShutdownListener) Options(org.forgerock.util.Options) SimpleBindRequest(org.forgerock.opendj.ldap.requests.SimpleBindRequest) ShutdownManager(org.forgerock.util.thread.listener.ShutdownManager) Duration(org.forgerock.util.time.Duration) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) SSLContextBuilder(org.forgerock.opendj.ldap.SSLContextBuilder) LdapException(org.forgerock.opendj.ldap.LdapException) LdapException(org.forgerock.opendj.ldap.LdapException) IOException(java.io.IOException)

Example 3 with LDAPConnectionFactory

use of org.forgerock.opendj.ldap.LDAPConnectionFactory in project OpenAM by OpenRock.

the class IdRepoUtils method getLDAPConnection.

private static ConnectionFactory getLDAPConnection(Map attrValues) throws Exception {
    Options options = Options.defaultOptions().set(CONNECT_TIMEOUT, new Duration((long) 300, TimeUnit.MILLISECONDS));
    if (CollectionHelper.getBooleanMapAttr(attrValues, "sun-idrepo-ldapv3-config-ssl-enabled", false)) {
        options = options.set(SSL_CONTEXT, new SSLContextBuilder().getSSLContext());
    }
    Set<LDAPURL> ldapUrls = getLDAPUrls(attrValues);
    if (CollectionUtils.isEmpty(ldapUrls)) {
        if (DEBUG.warningEnabled()) {
            DEBUG.warning("IdRepoUtils.getLDAPConnection: No LDAPURLs found");
        }
        throw new IdRepoException(IdRepoBundle.BUNDLE_NAME, IdRepoErrorCode.UNABLE_AUTHENTICATE_LDAP_SERVER, null);
    }
    LDAPURL ldapUrl = ldapUrls.iterator().next();
    if (org.forgerock.openam.utils.StringUtils.isEmpty(ldapUrl.getHost())) {
        if (DEBUG.warningEnabled()) {
            DEBUG.warning("IdRepoUtils.getLDAPConnection: No LDAP host found");
        }
        throw new IdRepoException(IdRepoBundle.BUNDLE_NAME, IdRepoErrorCode.UNABLE_AUTHENTICATE_LDAP_SERVER, null);
    }
    // All connections will use authentication
    String bindDn = CollectionHelper.getMapAttr(attrValues, "sun-idrepo-ldapv3-config-authid");
    if (org.forgerock.openam.utils.StringUtils.isBlank(bindDn)) {
        if (DEBUG.warningEnabled()) {
            DEBUG.warning("IdRepoUtils.getLDAPConnection: No LDAP bindDN found");
        }
        throw new IdRepoException(IdRepoBundle.BUNDLE_NAME, IdRepoErrorCode.UNABLE_AUTHENTICATE_LDAP_SERVER, null);
    }
    String bindPwd = CollectionHelper.getMapAttr(attrValues, "sun-idrepo-ldapv3-config-authpw");
    if (org.forgerock.openam.utils.StringUtils.isBlank(bindPwd)) {
        if (DEBUG.warningEnabled()) {
            DEBUG.warning("IdRepoUtils.getLDAPConnection: No LDAP bindPW found");
        }
        throw new IdRepoException(IdRepoBundle.BUNDLE_NAME, IdRepoErrorCode.UNABLE_AUTHENTICATE_LDAP_SERVER, null);
    }
    options = options.set(AUTHN_BIND_REQUEST, LDAPRequests.newSimpleBindRequest(bindDn, bindPwd.toCharArray()));
    return new LDAPConnectionFactory(ldapUrl.getHost(), ldapUrl.getPort(), options);
}
Also used : Options(org.forgerock.util.Options) LDAPURL(org.forgerock.openam.ldap.LDAPURL) IdRepoException(com.sun.identity.idm.IdRepoException) Duration(org.forgerock.util.time.Duration) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) SSLContextBuilder(org.forgerock.opendj.ldap.SSLContextBuilder)

Example 4 with LDAPConnectionFactory

use of org.forgerock.opendj.ldap.LDAPConnectionFactory in project OpenAM by OpenRock.

the class LDAPUtils method newConnectionFactory.

/**
     * Creates a new connection factory based on the provided parameters.
     *
     * @param ldapurl The address of the LDAP server.
     * @param username The directory user's DN. May be null if this is an anonymous connection.
     * @param password The directory user's password.
     * @param heartBeatInterval The interval for sending out heartbeat requests.
     * @param heartBeatTimeUnit The timeunit for the heartbeat interval.
     * @param ldapOptions Additional LDAP settings used to create the connection factory.
     * @return An authenticated/anonymous connection factory, which may also send heartbeat requests.
     */
private static ConnectionFactory newConnectionFactory(LDAPURL ldapurl, String username, char[] password, int heartBeatInterval, String heartBeatTimeUnit, Options ldapOptions) {
    Boolean ssl = ldapurl.isSSL();
    int heartBeatTimeout = SystemPropertiesManager.getAsInt(Constants.LDAP_HEARTBEAT_TIMEOUT, DEFAULT_HEARTBEAT_TIMEOUT);
    if (ssl != null && ssl.booleanValue()) {
        try {
            //Creating a defensive copy of ldapOptions to handle the case when a mixture of SSL/non-SSL connections
            //needs to be established.
            ldapOptions = Options.copyOf(ldapOptions).set(LDAPConnectionFactory.SSL_CONTEXT, new SSLContextBuilder().getSSLContext());
        } catch (GeneralSecurityException gse) {
            DEBUG.error("An error occurred while creating SSLContext", gse);
        }
    }
    // Enable heartbeat
    if (heartBeatInterval > 0 && heartBeatTimeUnit != null) {
        TimeUnit unit = TimeUnit.valueOf(heartBeatTimeUnit.toUpperCase());
        ldapOptions = ldapOptions.set(HEARTBEAT_ENABLED, true).set(HEARTBEAT_INTERVAL, new Duration(unit.toSeconds(heartBeatInterval), TimeUnit.SECONDS)).set(HEARTBEAT_TIMEOUT, new Duration(unit.toSeconds(heartBeatTimeout), TimeUnit.SECONDS));
    }
    // Enable Authenticated connection
    if (username != null) {
        ldapOptions = ldapOptions.set(AUTHN_BIND_REQUEST, LDAPRequests.newSimpleBindRequest(username, password));
    }
    return new LDAPConnectionFactory(ldapurl.getHost(), ldapurl.getPort(), ldapOptions);
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) TimeUnit(java.util.concurrent.TimeUnit) Duration(org.forgerock.util.time.Duration) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) SSLContextBuilder(org.forgerock.opendj.ldap.SSLContextBuilder)

Example 5 with LDAPConnectionFactory

use of org.forgerock.opendj.ldap.LDAPConnectionFactory in project OpenAM by OpenRock.

the class EmbeddedOpenDS method getLDAPConnectionFactory.

private static synchronized ConnectionFactory getLDAPConnectionFactory(String dsHostName, String dsPort, String dsManager, String dsAdminPwd) {
    if (factory == null) {
        // All connections will use authentication
        Options options = Options.defaultOptions().set(AUTHN_BIND_REQUEST, LDAPRequests.newSimpleBindRequest(dsManager, dsAdminPwd.toCharArray())).set(CONNECT_TIMEOUT, new Duration((long) 3, TimeUnit.SECONDS));
        factory = new LDAPConnectionFactory(dsHostName, Integer.parseInt(dsPort), options);
        ShutdownManager.getInstance().addShutdownListener(new ShutdownListener() {

            @Override
            public void shutdown() {
                if (factory != null) {
                    factory.close();
                }
            }
        });
    }
    return factory;
}
Also used : ShutdownListener(org.forgerock.util.thread.listener.ShutdownListener) Options(org.forgerock.util.Options) Duration(org.forgerock.util.time.Duration) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory)

Aggregations

LDAPConnectionFactory (org.forgerock.opendj.ldap.LDAPConnectionFactory)17 Options (org.forgerock.util.Options)13 SSLContextBuilder (org.forgerock.opendj.ldap.SSLContextBuilder)8 Duration (org.forgerock.util.time.Duration)8 GeneralSecurityException (java.security.GeneralSecurityException)6 LdapException (org.forgerock.opendj.ldap.LdapException)5 IOException (java.io.IOException)4 ConnectionFactory (org.forgerock.opendj.ldap.ConnectionFactory)4 ShutdownListener (org.forgerock.util.thread.listener.ShutdownListener)4 ByteString (org.forgerock.opendj.ldap.ByteString)3 Connection (org.forgerock.opendj.ldap.Connection)3 LDAPUrl (org.forgerock.opendj.ldap.LDAPUrl)3 SimpleBindRequest (org.forgerock.opendj.ldap.requests.SimpleBindRequest)3 DSConfigMgr (com.iplanet.services.ldap.DSConfigMgr)1 LDAPServiceException (com.iplanet.services.ldap.LDAPServiceException)1 IdRepoException (com.sun.identity.idm.IdRepoException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1