1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
53
54
55
56
57
58
59
60 public static LdapContext getWiredContext( LdapService ldapService ) throws Exception
61 {
62 return getWiredContext( ldapService, null );
63 }
64
65
66
67
68
69
70
71
72
73
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
91
92
93
94
95
96
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
113
114
115
116
117
118
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
136
137
138
139
140
141
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
159
160
161
162
163
164
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 }