001package com.nimbusds.common.ldap;
002
003
004import javax.net.SocketFactory;
005
006import com.unboundid.ldap.sdk.LDAPConnection;
007import com.unboundid.ldap.sdk.LDAPConnectionOptions;
008import com.unboundid.ldap.sdk.ServerSet;
009
010import com.nimbusds.common.config.CustomKeyStoreConfiguration;
011import com.nimbusds.common.config.CustomTrustStoreConfiguration;
012import com.nimbusds.common.config.LDAPServerDetails;
013
014
015/**
016 * Factory for establishing LDAP connections to a preset directory server.
017 */
018public class PresetLDAPConnectionFactory extends LDAPConnectionFactory {
019
020        
021        /**
022         * The LDAP server details.
023         */
024        private final LDAPServerDetails ldapServer;
025        
026        
027        /**
028         * The LDAP server set (single, fail-over or round-robin).
029         */
030        private final ServerSet ldapServerSet;
031        
032        
033        /**
034         * Creates a new preset LDAP connection factory.
035         *
036         * @param ldapServer       The LDAP server details. Must not be 
037         *                         {@code null}.
038         * @param customTrustStore The custom trust store configuration. Must 
039         *                         not be {@code null}.
040         * @param customKeyStore   The custom key store configuration. Must not 
041         *                         be {@code null}.
042         *
043         * @throws LDAPConnectionException If a SSL socket factory is required
044         *                                 and couldn't be established.
045         */
046        public PresetLDAPConnectionFactory(final LDAPServerDetails ldapServer,
047                                           final CustomTrustStoreConfiguration customTrustStore,
048                                           final CustomKeyStoreConfiguration customKeyStore)
049                throws LDAPConnectionException {
050        
051                super(customTrustStore, customKeyStore);
052
053                if (ldapServer == null)
054                        throw new IllegalArgumentException("The LDAP server details must not be null");
055                        
056                this.ldapServer = ldapServer;
057                
058                // Init LDAP server set
059                SocketFactory socketFactory = getSocketFactory(ldapServer.security, 
060                                                               customTrustStore,
061                                                               customKeyStore,
062                                                               ldapServer.trustSelfSignedCerts);
063                
064                LDAPConnectionOptions opts = new LDAPConnectionOptions();
065                opts.setConnectTimeoutMillis(ldapServer.connectTimeout);
066                
067                ldapServerSet = LDAPServerSetFactory.create(ldapServer.url,
068                                                            ldapServer.selectionAlgorithm,
069                                                            socketFactory,
070                                                            opts);
071        }
072
073
074        /**
075         * Gets the LDAP server details.
076         *
077         * @return The LDAP server details.
078         */
079        public LDAPServerDetails getLDAPServerDetails() {
080
081                return ldapServer;
082        }
083        
084        
085        /**
086         * Creates a new LDAP connection to the preset directory server.
087         *
088         * @return A new established unauthenticated LDAP connection ready for
089         *         use.
090         *
091         * @throws LDAPConnectionException If a new LDAP connection could not 
092         *                                 be created.
093         */
094        public LDAPConnection createLDAPConnection()
095                throws LDAPConnectionException {
096                
097                return createLDAPConnection(ldapServerSet, ldapServer.security, ldapServer.trustSelfSignedCerts);
098        }
099}