Skip to content

Commit d0dca6b

Browse files
#784 - Added HTTPS support for OKhttp
1 parent 93e88d9 commit d0dca6b

File tree

3 files changed

+129
-14
lines changed

3 files changed

+129
-14
lines changed

src/main/java/com/marklogic/client/DatabaseClientFactory.java

Lines changed: 105 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import javax.net.ssl.SSLContext;
2323
import javax.net.ssl.SSLException;
24+
import javax.net.ssl.TrustManager;
2425

2526
import okhttp3.OkHttpClient;
2627
import org.slf4j.Logger;
@@ -212,7 +213,7 @@ private DatabaseClientFactory() {
212213
* @return a new client for making database requests
213214
*/
214215
static public DatabaseClient newClient(String host, int port) {
215-
return newClient(host, port, null, null, null, null, null, null);
216+
return newClient(host, port, null, null, null, null, null, null, null);
216217
}
217218

218219
/**
@@ -226,7 +227,7 @@ static public DatabaseClient newClient(String host, int port) {
226227
* @return a new client for making database requests
227228
*/
228229
static public DatabaseClient newClient(String host, int port, String database) {
229-
return newClient(host, port, database, null, null, null, null, null);
230+
return newClient(host, port, database, null, null, null, null, null, null);
230231
}
231232

232233
/**
@@ -240,7 +241,7 @@ static public DatabaseClient newClient(String host, int port, String database) {
240241
* @return a new client for making database requests
241242
*/
242243
static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type) {
243-
return newClient(host, port, null, user, password, type, null, null);
244+
return newClient(host, port, null, user, password, type, null, null, null);
244245
}
245246
/**
246247
* Creates a client to access the database by means of a REST server.
@@ -254,7 +255,7 @@ static public DatabaseClient newClient(String host, int port, String user, Strin
254255
* @return a new client for making database requests
255256
*/
256257
static public DatabaseClient newClient(String host, int port, String database, String user, String password, Authentication type) {
257-
return newClient(host, port, database, user, password, type, null, null);
258+
return newClient(host, port, database, user, password, type, null, null, null);
258259
}
259260
/**
260261
* Creates a client to access the database by means of a REST server.
@@ -266,9 +267,26 @@ static public DatabaseClient newClient(String host, int port, String database, S
266267
* @param type the type of authentication applied to the request
267268
* @param context the SSL context for authenticating with the server
268269
* @return a new client for making database requests
270+
* @deprecated use {@link DatabaseClientFactory#newClient(String, int, String, String, Authentication, SSLContext, TrustManager)}}
269271
*/
270272
static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type, SSLContext context) {
271-
return newClient(host, port, null, user, password, type, context, SSLHostnameVerifier.COMMON);
273+
return newClient(host, port, null, user, password, type, context, null, SSLHostnameVerifier.COMMON);
274+
}
275+
/**
276+
* Creates a client to access the database by means of a REST server.
277+
*
278+
* @param host the host with the REST server
279+
* @param port the port for the REST server
280+
* @param user the user with read, write, or administrative privileges
281+
* @param password the password for the user
282+
* @param type the type of authentication applied to the request
283+
* @param context the SSL context for authenticating with the server
284+
* @param trustManager the TrustManager object which is responsible for
285+
* deciding if a credential should be trusted or not.
286+
* @return a new client for making database requests
287+
*/
288+
static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type, SSLContext context, TrustManager trustManager) {
289+
return newClient(host, port, null, user, password, type, context, trustManager, SSLHostnameVerifier.COMMON);
272290
}
273291
/**
274292
* Creates a client to access the database by means of a REST server.
@@ -281,9 +299,27 @@ static public DatabaseClient newClient(String host, int port, String user, Strin
281299
* @param type the type of authentication applied to the request
282300
* @param context the SSL context for authenticating with the server
283301
* @return a new client for making database requests
302+
* @deprecated use {@link DatabaseClientFactory#newClient(String, int, String, String, String, Authentication, SSLContext, TrustManager)}}
284303
*/
285304
static public DatabaseClient newClient(String host, int port, String database, String user, String password, Authentication type, SSLContext context) {
286-
return newClient(host, port, database, user, password, type, context, SSLHostnameVerifier.COMMON);
305+
return newClient(host, port, database, user, password, type, context, null, SSLHostnameVerifier.COMMON);
306+
}
307+
/**
308+
* Creates a client to access the database by means of a REST server.
309+
*
310+
* @param host the host with the REST server
311+
* @param port the port for the REST server
312+
* @param database the database to access (default: configured database for the REST server)
313+
* @param user the user with read, write, or administrative privileges
314+
* @param password the password for the user
315+
* @param type the type of authentication applied to the request
316+
* @param context the SSL context for authenticating with the server
317+
* @param trustManager the TrustManager object which is responsible for
318+
* deciding if a credential should be trusted or not.
319+
* @return a new client for making database requests
320+
*/
321+
static public DatabaseClient newClient(String host, int port, String database, String user, String password, Authentication type, SSLContext context, TrustManager trustManager) {
322+
return newClient(host, port, database, user, password, type, context, trustManager, SSLHostnameVerifier.COMMON);
287323
}
288324
/**
289325
* Creates a client to access the database by means of a REST server.
@@ -296,9 +332,48 @@ static public DatabaseClient newClient(String host, int port, String database, S
296332
* @param context the SSL context for authenticating with the server
297333
* @param verifier a callback for checking hostnames
298334
* @return a new client for making database requests
335+
* @deprecated use {@link DatabaseClientFactory#newClient(String, int, String, String, Authentication, SSLContext, TrustManager, SSLHostnameVerifier)}
299336
*/
300337
static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) {
301-
DatabaseClientImpl client = newClientImpl(host, port, null, user, password, type, context, verifier);
338+
DatabaseClientImpl client = newClientImpl(host, port, null, user, password, type, context, null, verifier);
339+
client.setHandleRegistry(getHandleRegistry().copy());
340+
return client;
341+
}
342+
/**
343+
* Creates a client to access the database by means of a REST server.
344+
*
345+
* @param host the host with the REST server
346+
* @param port the port for the REST server
347+
* @param user the user with read, write, or administrative privileges
348+
* @param password the password for the user
349+
* @param type the type of authentication applied to the request
350+
* @param context the SSL context for authenticating with the server
351+
* @param trustManager the TrustManager object which is responsible for
352+
* deciding if a credential should be trusted or not.
353+
* @param verifier a callback for checking hostnames
354+
* @return a new client for making database requests
355+
*/
356+
static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type, SSLContext context, TrustManager trustManager, SSLHostnameVerifier verifier) {
357+
DatabaseClientImpl client = newClientImpl(host, port, null, user, password, type, context, trustManager, verifier);
358+
client.setHandleRegistry(getHandleRegistry().copy());
359+
return client;
360+
}
361+
/**
362+
* Creates a client to access the database by means of a REST server.
363+
*
364+
* @param host the host with the REST server
365+
* @param port the port for the REST server
366+
* @param database the database to access (default: configured database for the REST server)
367+
* @param user the user with read, write, or administrative privileges
368+
* @param password the password for the user
369+
* @param type the type of authentication applied to the request
370+
* @param context the SSL context for authenticating with the server
371+
* @param verifier a callback for checking hostnames
372+
* @return a new client for making database requests
373+
* @deprecated use {@link DatabaseClientFactory#newClient(String, int, String, String, String, Authentication, SSLContext, TrustManager, SSLHostnameVerifier)}}
374+
*/
375+
static public DatabaseClient newClient(String host, int port, String database, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) {
376+
DatabaseClientImpl client = newClientImpl(host, port, database, user, password, type, context, null, verifier);
302377
client.setHandleRegistry(getHandleRegistry().copy());
303378
return client;
304379
}
@@ -312,15 +387,17 @@ static public DatabaseClient newClient(String host, int port, String user, Strin
312387
* @param password the password for the user
313388
* @param type the type of authentication applied to the request
314389
* @param context the SSL context for authenticating with the server
390+
* @param trustManager the TrustManager object which is responsible for
391+
* deciding if a credential should be trusted or not.
315392
* @param verifier a callback for checking hostnames
316393
* @return a new client for making database requests
317394
*/
318-
static public DatabaseClient newClient(String host, int port, String database, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) {
319-
DatabaseClientImpl client = newClientImpl(host, port, database, user, password, type, context, verifier);
395+
static public DatabaseClient newClient(String host, int port, String database, String user, String password, Authentication type, SSLContext context, TrustManager trustManager, SSLHostnameVerifier verifier) {
396+
DatabaseClientImpl client = newClientImpl(host, port, database, user, password, type, context, trustManager, verifier);
320397
client.setHandleRegistry(getHandleRegistry().copy());
321398
return client;
322399
}
323-
static private DatabaseClientImpl newClientImpl(String host, int port, String database, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) {
400+
static private DatabaseClientImpl newClientImpl(String host, int port, String database, String user, String password, Authentication type, SSLContext context, TrustManager trustManager, SSLHostnameVerifier verifier) {
324401
logger.debug("Creating new database client for server at "+host+":"+port);
325402
OkHttpServices services = new OkHttpServices();
326403
services.connect(host, port, database, user, password, type, context, verifier);
@@ -421,6 +498,7 @@ static public class Bean implements Serializable {
421498

422499
transient private SSLContext context;
423500
transient private SSLHostnameVerifier verifier;
501+
transient private TrustManager trustManager;
424502

425503
/**
426504
* Zero-argument constructor for bean applications. Other
@@ -535,6 +613,22 @@ public SSLContext getContext() {
535613
public void setContext(SSLContext context) {
536614
this.context = context;
537615
}
616+
/**
617+
* Returns the TrustManager for SSL clients created with a
618+
* DatabaseClientFactory.Bean object.
619+
* @return the TrustManager
620+
*/
621+
public TrustManager getTrustManager() {
622+
return trustManager;
623+
}
624+
/**
625+
* Specifies the TrustManager for clients created with a
626+
* DatabaseClientFactory.Bean object that authenticate with SSL.
627+
* @param trustManager the TrustManager
628+
*/
629+
public void setTrustManager(TrustManager trustManager) {
630+
this.trustManager = trustManager;
631+
}
538632
/**
539633
* Returns the host verifier for clients created with a
540634
* DatabaseClientFactory.Bean object.
@@ -586,7 +680,7 @@ public void registerDefaultHandles() {
586680
* @return a new client for making database requests
587681
*/
588682
public DatabaseClient newClient() {
589-
DatabaseClientImpl client = newClientImpl(host, port, database, user, password, authentication, context, verifier);
683+
DatabaseClientImpl client = newClientImpl(host, port, database, user, password, authentication, context, trustManager, verifier);
590684
client.setHandleRegistry(getHandleRegistry().copy());
591685

592686
return client;

src/main/java/com/marklogic/client/impl/OkHttpServices.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@
124124
import javax.net.ssl.SSLContext;
125125
import javax.net.ssl.SSLException;
126126
import javax.net.ssl.SSLSession;
127+
import javax.net.ssl.TrustManager;
128+
import javax.net.ssl.X509TrustManager;
127129
import javax.ws.rs.core.NewCookie;
128130
import java.io.ByteArrayInputStream;
129131
import java.io.Closeable;
@@ -282,10 +284,17 @@ private FailedRequest extractErrorFields(Response response) {
282284
}
283285
}
284286

285-
@Override
287+
@Deprecated
286288
public void connect(String host, int port, String database, String user, String password,
287289
Authentication authenType, SSLContext sslContext,
288290
SSLHostnameVerifier verifier) {
291+
connect(host, port, database, user, password, authenType, sslContext, null, verifier);
292+
}
293+
@Override
294+
295+
public void connect(String host, int port, String database, String user, String password,
296+
Authentication authenType, SSLContext sslContext, TrustManager trustManager,
297+
SSLHostnameVerifier verifier) {
289298
HostnameVerifier hostnameVerifier = null;
290299
if (verifier == SSLHostnameVerifier.ANY) {
291300
hostnameVerifier = new HostnameVerifier() {
@@ -304,11 +313,11 @@ public boolean verify(String hostname, SSLSession session) {
304313
// throw new IllegalArgumentException(
305314
// "Null SSLContext but non-null SSLHostnameVerifier for client");
306315
//}
307-
connect(host, port, database, user, password, authenType, sslContext, hostnameVerifier);
316+
connect(host, port, database, user, password, authenType, sslContext, trustManager, hostnameVerifier);
308317
}
309318

310319
private void connect(String host, int port, String database, String user, String password,
311-
Authentication authenType, SSLContext sslContext,
320+
Authentication authenType, SSLContext sslContext, TrustManager trustManager,
312321
HostnameVerifier verifier) {
313322
logger.debug("Connecting to {} at {} as {}", new Object[]{host, port, user});
314323

@@ -359,6 +368,14 @@ private void connect(String host, int port, String database, String user, String
359368
.readTimeout(0, TimeUnit.SECONDS)
360369
.writeTimeout(0, TimeUnit.SECONDS);
361370

371+
if(sslContext != null) {
372+
if(trustManager == null) {
373+
clientBldr.sslSocketFactory(sslContext.getSocketFactory());
374+
} else {
375+
clientBldr.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustManager);
376+
}
377+
}
378+
362379
if ( authenticator != null ) {
363380
clientBldr = clientBldr.authenticator(new CachingAuthenticatorDecorator(authenticator, authCache));
364381
clientBldr = clientBldr.addInterceptor(new AuthenticationCacheInterceptor(authCache));

src/main/java/com/marklogic/client/impl/RESTServices.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Set;
2222

2323
import javax.net.ssl.SSLContext;
24+
import javax.net.ssl.TrustManager;
2425

2526
import com.marklogic.client.DatabaseClient;
2627
import com.marklogic.client.DatabaseClientFactory.Authentication;
@@ -102,8 +103,11 @@ public interface RESTServices {
102103
String MAX_DELAY_PROP = "com.marklogic.client.maximumRetrySeconds";
103104
String MIN_RETRY_PROP = "com.marklogic.client.minimumRetries";
104105

106+
@Deprecated
105107
public void connect(String host, int port, String database, String user, String password, Authentication type,
106108
SSLContext context, SSLHostnameVerifier verifier);
109+
public void connect(String host, int port, String database, String user, String password, Authentication type,
110+
SSLContext context, TrustManager trustManager, SSLHostnameVerifier verifier);
107111
public DatabaseClient getDatabaseClient();
108112
public void setDatabaseClient(DatabaseClient client);
109113
public void release();

0 commit comments

Comments
 (0)