7
7
import com .unboundid .ldap .sdk .LDAPConnection ;
8
8
import com .unboundid .ldap .sdk .LDAPException ;
9
9
import com .unboundid .ldap .sdk .LDAPInterface ;
10
+ import net .bytebuddy .ByteBuddy ;
11
+ import net .bytebuddy .dynamic .loading .ClassLoadingStrategy ;
12
+ import net .bytebuddy .implementation .FixedValue ;
10
13
import org .slf4j .Logger ;
11
14
import org .slf4j .LoggerFactory ;
12
15
import org .zapodot .junit .ldap .EmbeddedLdapServer ;
18
21
import javax .naming .directory .DirContext ;
19
22
import javax .naming .directory .InitialDirContext ;
20
23
import javax .naming .ldap .LdapContext ;
24
+ import javax .net .SocketFactory ;
25
+ import javax .net .ssl .SSLSocketFactory ;
26
+ import java .io .IOException ;
21
27
import java .io .UnsupportedEncodingException ;
28
+ import java .lang .reflect .Modifier ;
29
+ import java .net .InetAddress ;
30
+ import java .net .Socket ;
22
31
import java .net .URLDecoder ;
32
+ import java .net .UnknownHostException ;
23
33
import java .util .Hashtable ;
24
34
import java .util .List ;
25
35
@@ -34,11 +44,16 @@ abstract class EmbeddedLdapServerImpl implements EmbeddedLdapServer {
34
44
private LDAPConnection ldapConnection ;
35
45
private InitialDirContext initialDirContext ;
36
46
private boolean isStarted = false ;
47
+ private final boolean useTls ;
48
+ private final SSLSocketFactory socketFactory ;
37
49
38
50
public EmbeddedLdapServerImpl (final InMemoryDirectoryServer inMemoryDirectoryServer ,
39
- final AuthenticationConfiguration authenticationConfiguration1 ) {
51
+ final AuthenticationConfiguration authenticationConfiguration1 ,
52
+ final boolean useTls , SSLSocketFactory socketFactory ) {
40
53
this .inMemoryDirectoryServer = inMemoryDirectoryServer ;
41
54
this .authenticationConfiguration = authenticationConfiguration1 ;
55
+ this .useTls = useTls ;
56
+ this .socketFactory = socketFactory ;
42
57
}
43
58
44
59
protected static InMemoryDirectoryServer createServer (final InMemoryDirectoryServerConfig inMemoryDirectoryServerConfig ,
@@ -112,10 +127,63 @@ private InitialDirContext createOrGetInitialDirContext() throws NamingException
112
127
}
113
128
}
114
129
130
+ public static abstract class AbstractDelegatingSocketFactory extends SocketFactory {
131
+ public static AbstractDelegatingSocketFactory INSTANCE ;
132
+
133
+ public static AbstractDelegatingSocketFactory getDefault () {
134
+ return INSTANCE ;
135
+ }
136
+
137
+ protected abstract SocketFactory getDelegate ();
138
+
139
+ @ Override
140
+ public Socket createSocket () throws IOException , UnknownHostException {
141
+ return getDelegate ().createSocket ();
142
+ }
143
+
144
+ @ Override
145
+ public Socket createSocket (String host , int port ) throws IOException , UnknownHostException {
146
+ return getDelegate ().createSocket (host , port );
147
+ }
148
+
149
+ @ Override
150
+ public Socket createSocket (String host , int port , InetAddress localHost , int localPort )
151
+ throws IOException , UnknownHostException {
152
+ return getDelegate ().createSocket (host , port , localHost , localPort );
153
+ }
154
+
155
+ @ Override
156
+ public Socket createSocket (InetAddress host , int port ) throws IOException {
157
+ return getDelegate ().createSocket (host , port );
158
+ }
159
+
160
+ @ Override
161
+ public Socket createSocket (InetAddress address , int port , InetAddress localAddress , int localPort )
162
+ throws IOException {
163
+ return getDelegate ().createSocket (address , port , localAddress , localPort );
164
+ }
165
+ }
166
+
115
167
private Hashtable <String , String > createLdapEnvironment () {
116
168
final Hashtable <String , String > environment = new Hashtable <>();
169
+ if (socketFactory != null ) {
170
+ final Class <?> delegator = (new ByteBuddy ()).subclass (AbstractDelegatingSocketFactory .class )
171
+ .defineMethod ("getDelegate" , SocketFactory .class , Modifier .PROTECTED )
172
+ .intercept (FixedValue .value (socketFactory ))
173
+ .make ()
174
+ .load (getClass ().getClassLoader (), ClassLoadingStrategy .Default .INJECTION )
175
+ .getLoaded ();
176
+ try {
177
+ final Object instance = delegator .newInstance ();
178
+ delegator .getField ("INSTANCE" ).set (instance , instance );
179
+ } catch (InstantiationException | IllegalAccessException | NoSuchFieldException e ) {
180
+ throw new IllegalStateException (e );
181
+ }
182
+ environment .put ("java.naming.ldap.factory.socket" , delegator .getCanonicalName ());
183
+ }
117
184
environment .put (LdapContext .CONTROL_FACTORIES , JAVA_RT_CONTROL_FACTORY );
118
- environment .put (Context .PROVIDER_URL , String .format ("ldap://%s:%s" ,
185
+ environment .put (Context .PROVIDER_URL , String .format ("%s://%s:%s" ,
186
+ useTls ? "ldaps" : "ldap" ,
119
187
inMemoryDirectoryServer .getListenAddress ().getHostName (),
120
188
embeddedServerPort ()));
121
189
environment .put (Context .INITIAL_CONTEXT_FACTORY , JAVA_RT_CONTEXT_FACTORY );
0 commit comments