2121
2222import javax .net .ssl .SSLContext ;
2323import javax .net .ssl .SSLException ;
24+ import javax .net .ssl .TrustManager ;
2425
2526import okhttp3 .OkHttpClient ;
2627import 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 ;
0 commit comments