001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.directory.server.integ;
020    
021    
022    import java.util.Hashtable;
023    
024    import javax.naming.Context;
025    import javax.naming.ldap.Control;
026    import javax.naming.ldap.InitialLdapContext;
027    import javax.naming.ldap.LdapContext;
028    
029    import netscape.ldap.LDAPConnection;
030    
031    import org.apache.directory.server.constants.ServerDNConstants;
032    import org.apache.directory.server.core.integ.IntegrationUtils;
033    import org.apache.directory.server.ldap.LdapServer;
034    import org.slf4j.Logger;
035    import org.slf4j.LoggerFactory;
036    
037    
038    public class ServerIntegrationUtils extends IntegrationUtils
039    {
040        /** The class logger */
041        private static final Logger LOG = LoggerFactory.getLogger( ServerIntegrationUtils.class );
042        private static final String CTX_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
043    
044        private static final String DEFAULT_HOST = "localhost";
045        private static final int DEFAULT_PORT = 10389;
046        private static final String DEFAULT_ADMIN = ServerDNConstants.ADMIN_SYSTEM_DN;
047        private static final String DEFAULT_PASSWORD = "secret";
048    
049    
050    
051        /**
052         * Creates a JNDI LdapContext with a connection over the wire using the 
053         * SUN LDAP provider.  The connection is made using the administrative 
054         * user as the principalDN.  The context is to the rootDSE.
055         *
056         * @param ldapServer the LDAP server to get the connection to
057         * @return an LdapContext as the administrative user to the RootDSE
058         * @throws Exception if there are problems creating the context
059         */
060        public static LdapContext getWiredContext( LdapServer ldapServer ) throws Exception
061        {
062            return getWiredContext( ldapServer, null );
063        }
064    
065    
066        /**
067         * Creates a JNDI LdapContext with a connection over the wire using the 
068         * SUN LDAP provider.  The connection is made using the administrative 
069         * user as the principalDN.  The context is to the rootDSE.
070         *
071         * @param ldapServer the LDAP server to get the connection to
072         * @return an LdapContext as the administrative user to the RootDSE
073         * @throws Exception if there are problems creating the context
074         */
075        public static LdapContext getWiredContext( LdapServer ldapServer, String principalDn, String password ) 
076            throws Exception
077        {
078            LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapServer.getPort() );
079            Hashtable<String, String> env = new Hashtable<String, String>();
080            env.put( Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY );
081            env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
082            env.put( Context.SECURITY_PRINCIPAL, principalDn );
083            env.put( Context.SECURITY_CREDENTIALS, password );
084            env.put( Context.SECURITY_AUTHENTICATION, "simple" );
085            return new InitialLdapContext( env, null );
086        }
087    
088    
089        /**
090         * Creates a JNDI LdapContext with a connection over the wire using the 
091         * SUN LDAP provider.  The connection is made using the administrative 
092         * user as the principalDN.  The context is to the rootDSE.
093         *
094         * @param ldapServer the LDAP server to get the connection to
095         * @return an LdapContext as the administrative user to the RootDSE
096         * @throws Exception if there are problems creating the context
097         */
098        public static LdapContext getWiredContext( LdapServer ldapServer, Control[] controls ) throws Exception
099        {
100            LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapServer.getPort() );
101            Hashtable<String, String> env = new Hashtable<String, String>();
102            env.put( Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY );
103            env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
104            env.put( Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN );
105            env.put( Context.SECURITY_CREDENTIALS, "secret" );
106            env.put( Context.SECURITY_AUTHENTICATION, "simple" );
107            return new InitialLdapContext( env, controls );
108        }
109    
110        
111        /**
112         * Creates a JNDI LdapContext with a connection over the wire using the 
113         * SUN LDAP provider.  The connection is made using the administrative 
114         * user as the principalDN.  The context is to the rootDSE.
115         *
116         * @param ldapServer the LDAP server to get the connection to
117         * @return an LdapContext as the administrative user to the RootDSE
118         * @throws Exception if there are problems creating the context
119         */
120        public static LdapContext getWiredContextThrowOnRefferal( LdapServer ldapServer ) throws Exception
121        {
122            LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapServer.getPort() );
123            Hashtable<String, String> env = new Hashtable<String, String>();
124            env.put( Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY );
125            env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
126            env.put( Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN );
127            env.put( Context.SECURITY_CREDENTIALS, "secret" );
128            env.put( Context.SECURITY_AUTHENTICATION, "simple" );
129            env.put( Context.REFERRAL, "throw" );
130            return new InitialLdapContext( env, null );
131        }
132    
133        
134        /**
135         * Creates a JNDI LdapContext with a connection over the wire using the 
136         * SUN LDAP provider.  The connection is made using the administrative 
137         * user as the principalDN.  The context is to the rootDSE.
138         *
139         * @param ldapServer the LDAP server to get the connection to
140         * @return an LdapContext as the administrative user to the RootDSE
141         * @throws Exception if there are problems creating the context
142         */
143        public static LdapContext getWiredContextRefferalIgnore( LdapServer ldapServer ) throws Exception
144        {
145            LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapServer.getPort() );
146            Hashtable<String, String> env = new Hashtable<String, String>();
147            env.put( Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY );
148            env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
149            env.put( Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN );
150            env.put( Context.SECURITY_CREDENTIALS, "secret" );
151            env.put( Context.SECURITY_AUTHENTICATION, "simple" );
152            env.put( Context.REFERRAL, "ignore" );
153            return new InitialLdapContext( env, null );
154        }
155    
156        
157        /**
158         * Creates a JNDI LdapContext with a connection over the wire using the 
159         * SUN LDAP provider.  The connection is made using the administrative 
160         * user as the principalDN.  The context is to the rootDSE.
161         *
162         * @param ldapServer the LDAP server to get the connection to
163         * @return an LdapContext as the administrative user to the RootDSE
164         * @throws Exception if there are problems creating the context
165         */
166        public static LdapContext getWiredContextFollowOnRefferal( LdapServer ldapServer ) throws Exception
167        {
168            LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapServer.getPort() );
169            Hashtable<String, String> env = new Hashtable<String, String>();
170            env.put( Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY );
171            env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
172            env.put( Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN );
173            env.put( Context.SECURITY_CREDENTIALS, "secret" );
174            env.put( Context.SECURITY_AUTHENTICATION, "simple" );
175            env.put( Context.REFERRAL, "follow" );
176            return new InitialLdapContext( env, null );
177        }
178    
179        
180        public static LDAPConnection getWiredConnection( LdapServer ldapServer ) throws Exception
181        {
182            String testServer = System.getProperty( "ldap.test.server", null );
183            
184            if ( testServer == null )
185            {
186                return getWiredConnection( ldapServer, ServerDNConstants.ADMIN_SYSTEM_DN, "secret" );
187            }
188            
189            LOG.debug( "ldap.test.server = " + testServer );
190            
191            String admin = System.getProperty( testServer + ".admin", DEFAULT_ADMIN );
192            LOG.debug( testServer + ".admin = " + admin );
193    
194            String password = System.getProperty( testServer + ".password", DEFAULT_PASSWORD );
195            LOG.debug( testServer + ".password = " + password );
196    
197            String host = System.getProperty( testServer + ".host", DEFAULT_HOST );
198            LOG.debug( testServer + ".host = " + host );
199    
200            int port = Integer.parseInt( System.getProperty( testServer + ".port", Integer.toString( DEFAULT_PORT ) ) );
201            LOG.debug( testServer + ".port = " + port );
202            
203            LDAPConnection conn = new LDAPConnection();
204            conn.connect( 3, host, port, admin, password );
205            return conn;
206        }
207    
208        
209        public static LDAPConnection getWiredConnection( LdapServer ldapServer, String principalDn, String password ) 
210            throws Exception
211        {
212            LDAPConnection conn = new LDAPConnection();
213            conn.connect( 3, "localhost", ldapServer.getPort(), principalDn, password );
214            return conn;
215        }
216    }