View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.directory.server.integ;
20  
21  
22  import java.util.Hashtable;
23  
24  import javax.naming.Context;
25  import javax.naming.ldap.Control;
26  import javax.naming.ldap.InitialLdapContext;
27  import javax.naming.ldap.LdapContext;
28  
29  import netscape.ldap.LDAPConnection;
30  
31  import org.apache.directory.server.constants.ServerDNConstants;
32  import org.apache.directory.server.core.integ.IntegrationUtils;
33  import org.apache.directory.server.ldap.LdapService;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  public class ServerIntegrationUtils extends IntegrationUtils
39  {
40      /** The class logger */
41      private static final Logger LOG = LoggerFactory.getLogger( ServerIntegrationUtils.class );
42      private static final String CTX_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
43  
44      private static final String DEFAULT_HOST = "localhost";
45      private static final int DEFAULT_PORT = 10389;
46      private static final String DEFAULT_ADMIN = ServerDNConstants.ADMIN_SYSTEM_DN;
47      private static final String DEFAULT_PASSWORD = "secret";
48  
49  
50  
51      /**
52       * Creates a JNDI LdapContext with a connection over the wire using the 
53       * SUN LDAP provider.  The connection is made using the administrative 
54       * user as the principalDN.  The context is to the rootDSE.
55       *
56       * @param ldapService the LDAP server to get the connection to
57       * @return an LdapContext as the administrative user to the RootDSE
58       * @throws Exception if there are problems creating the context
59       */
60      public static LdapContext getWiredContext( LdapService ldapService ) throws Exception
61      {
62          return getWiredContext( ldapService, null );
63      }
64  
65  
66      /**
67       * Creates a JNDI LdapContext with a connection over the wire using the 
68       * SUN LDAP provider.  The connection is made using the administrative 
69       * user as the principalDN.  The context is to the rootDSE.
70       *
71       * @param ldapService the LDAP server to get the connection to
72       * @return an LdapContext as the administrative user to the RootDSE
73       * @throws Exception if there are problems creating the context
74       */
75      public static LdapContext getWiredContext( LdapService ldapService, String principalDn, String password ) 
76          throws Exception
77      {
78          LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapService.getIpPort() );
79          Hashtable<String, String> env = new Hashtable<String, String>();
80          env.put( Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY );
81          env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapService.getIpPort() );
82          env.put( Context.SECURITY_PRINCIPAL, principalDn );
83          env.put( Context.SECURITY_CREDENTIALS, password );
84          env.put( Context.SECURITY_AUTHENTICATION, "simple" );
85          return new InitialLdapContext( env, null );
86      }
87  
88  
89      /**
90       * Creates a JNDI LdapContext with a connection over the wire using the 
91       * SUN LDAP provider.  The connection is made using the administrative 
92       * user as the principalDN.  The context is to the rootDSE.
93       *
94       * @param ldapService the LDAP server to get the connection to
95       * @return an LdapContext as the administrative user to the RootDSE
96       * @throws Exception if there are problems creating the context
97       */
98      public static LdapContext getWiredContext( LdapService ldapService, Control[] controls ) throws Exception
99      {
100         LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapService.getIpPort() );
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:" + ldapService.getIpPort() );
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 ldapService 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( LdapService ldapService ) throws Exception
121     {
122         LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapService.getIpPort() );
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:" + ldapService.getIpPort() );
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 ldapService 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( LdapService ldapService ) throws Exception
144     {
145         LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapService.getIpPort() );
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:" + ldapService.getIpPort() );
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 ldapService 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( LdapService ldapService ) throws Exception
167     {
168         LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapService.getIpPort() );
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:" + ldapService.getIpPort() );
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( LdapService ldapService ) throws Exception
181     {
182         String testServer = System.getProperty( "ldap.test.server", null );
183         
184         if ( testServer == null )
185         {
186             return getWiredConnection( ldapService, 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( LdapService ldapService, String principalDn, String password ) 
210         throws Exception
211     {
212         LDAPConnection conn = new LDAPConnection();
213         conn.connect( 3, "localhost", ldapService.getIpPort(), principalDn, password );
214         return conn;
215     }
216 }