-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathDatabaseClientBuilder.java
More file actions
382 lines (330 loc) · 10.8 KB
/
DatabaseClientBuilder.java
File metadata and controls
382 lines (330 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
package com.marklogic.client;
import com.marklogic.client.impl.ConnectionString;
import com.marklogic.client.impl.DatabaseClientPropertySource;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
/**
* Intended to support programmatically building a {@code DatabaseClient} via chained "with" methods for setting
* each possible input allowed for connecting to and authenticating with MarkLogic. While the
* {@code DatabaseClientFactory.Bean} class is intended for use in a context such as a Spring container, it requires
* that a user have already assembled the appropriate {@code DatabaseClientFactory.SecurityContext}. This builder
* instead is intended for a more dynamic environment - in particular, one where the desired authentication strategy is
* not known until runtime. A client can then collect inputs from a user at runtime and call the appropriate methods on
* this builder. The builder will handle constructing the correct {@code DatabaseClientFactory.SecurityContext} and
* using that to construct a {@code DatabaseClient}.
*
* @since 6.1.0
*/
public class DatabaseClientBuilder {
public final static String PREFIX = "marklogic.client.";
public final static String AUTH_TYPE_BASIC = "basic";
public final static String AUTH_TYPE_DIGEST = "digest";
public final static String AUTH_TYPE_CLOUD = "cloud";
/**
* @deprecated as of 8.1.0, use AUTH_TYPE_CLOUD instead
*/
@Deprecated
public final static String AUTH_TYPE_MARKLOGIC_CLOUD = AUTH_TYPE_CLOUD;
public final static String AUTH_TYPE_KERBEROS = "kerberos";
public final static String AUTH_TYPE_CERTIFICATE = "certificate";
public final static String AUTH_TYPE_SAML = "saml";
public final static String AUTH_TYPE_OAUTH = "oauth";
private final Map<String, Object> props;
public DatabaseClientBuilder() {
this.props = new HashMap<>();
}
/**
* Initialize the builder with the given set of properties.
*
* @param props
*/
public DatabaseClientBuilder(Map<String, Object> props) {
this();
this.props.putAll(props);
}
/**
* @return a {@code DatabaseClient} based on the inputs that have been provided via the "with" builder methods
* and any inputs provided via this instance's constructor
*/
public DatabaseClient build() {
return DatabaseClientFactory.newClient(getPropertySource());
}
/**
* @return an instance of {@code DatabaseClientFactory.Bean} based on the inputs that have been provided via
* the "with" builder methods and any inputs provided via this instance's constructor
*/
public DatabaseClientFactory.Bean buildBean() {
return new DatabaseClientPropertySource(getPropertySource()).newClientBean();
}
/**
* @return a function that acts as a property source, specifically for use with the
* {@code DatabaseClientFactory.newClient} method that accepts this type of function.
*/
private Function<String, Object> getPropertySource() {
return propertyName -> props.get(propertyName);
}
public DatabaseClientBuilder withHost(String host) {
props.put(PREFIX + "host", host);
return this;
}
public DatabaseClientBuilder withPort(int port) {
props.put(PREFIX + "port", port);
return this;
}
/**
* @param connectionString of the form "username:password@host:port/optionalDatabaseName".
* @since 7.1.0
*/
public DatabaseClientBuilder withConnectionString(String connectionString) {
ConnectionString cs = new ConnectionString(connectionString, "connection string");
if (!props.containsKey(PREFIX + "authType")) {
withAuthType("digest");
}
if (cs.getDatabase() != null && cs.getDatabase().trim().length() > 0) {
withDatabase(cs.getDatabase());
}
return withHost(cs.getHost())
.withPort(cs.getPort())
.withUsername(cs.getUsername())
.withPassword(cs.getPassword());
}
public DatabaseClientBuilder withBasePath(String basePath) {
props.put(PREFIX + "basePath", basePath);
return this;
}
public DatabaseClientBuilder withDatabase(String database) {
props.put(PREFIX + "database", database);
return this;
}
public DatabaseClientBuilder withUsername(String username) {
props.put(PREFIX + "username", username);
return this;
}
public DatabaseClientBuilder withPassword(String password) {
props.put(PREFIX + "password", password);
return this;
}
public DatabaseClientBuilder withSecurityContext(DatabaseClientFactory.SecurityContext securityContext) {
props.put(PREFIX + "securityContext", securityContext);
return this;
}
/**
* @param type must be one of "basic", "digest", "cloud", "kerberos", "certificate", or "saml"
* @return
*/
public DatabaseClientBuilder withAuthType(String type) {
props.put(PREFIX + "authType", type);
return this;
}
public DatabaseClientBuilder withBasicAuth(String username, String password) {
return withAuthType(AUTH_TYPE_BASIC)
.withUsername(username)
.withPassword(password);
}
public DatabaseClientBuilder withDigestAuth(String username, String password) {
return withAuthType(AUTH_TYPE_DIGEST)
.withUsername(username)
.withPassword(password);
}
public DatabaseClientBuilder withCloudAuth(String apiKey, String basePath) {
return withAuthType(AUTH_TYPE_CLOUD)
.withCloudApiKey(apiKey)
.withBasePath(basePath);
}
/**
* @param apiKey
* @param basePath
* @param tokenDuration length in minutes until the generated access token expires
* @return
* @since 6.3.0
*/
public DatabaseClientBuilder withCloudAuth(String apiKey, String basePath, Integer tokenDuration) {
return withAuthType(AUTH_TYPE_CLOUD)
.withCloudApiKey(apiKey)
.withBasePath(basePath)
.withCloudTokenDuration(tokenDuration != null ? tokenDuration.toString() : null);
}
public DatabaseClientBuilder withKerberosAuth(String principal) {
return withAuthType(AUTH_TYPE_KERBEROS)
.withKerberosPrincipal(principal);
}
public DatabaseClientBuilder withCertificateAuth(String file, String password) {
return withAuthType(AUTH_TYPE_CERTIFICATE)
.withCertificateFile(file)
.withCertificatePassword(password);
}
/**
* @param sslContext
* @param trustManager
* @return
* @since 6.2.2
*/
public DatabaseClientBuilder withCertificateAuth(SSLContext sslContext, X509TrustManager trustManager) {
return withAuthType(AUTH_TYPE_CERTIFICATE)
.withSSLContext(sslContext)
.withTrustManager(trustManager);
}
public DatabaseClientBuilder withSAMLAuth(String token) {
return withAuthType(AUTH_TYPE_SAML)
.withSAMLToken(token);
}
/**
* @param token
* @return
* @since 6.6.0
*/
public DatabaseClientBuilder withOAuth(String token) {
return withAuthType(AUTH_TYPE_OAUTH).withOAuthToken(token);
}
/**
* @param token
* @return
* @since 6.6.0
*/
public DatabaseClientBuilder withOAuthToken(String token) {
props.put(PREFIX + "oauth.token", token);
return this;
}
public DatabaseClientBuilder withConnectionType(DatabaseClient.ConnectionType type) {
props.put(PREFIX + "connectionType", type);
return this;
}
public DatabaseClientBuilder withCloudApiKey(String cloudApiKey) {
props.put(PREFIX + "cloud.apiKey", cloudApiKey);
return this;
}
/**
* @param tokenDuration length in minutes until the generated access token expires
* @return
* @since 6.3.0
*/
public DatabaseClientBuilder withCloudTokenDuration(String tokenDuration) {
props.put(PREFIX + "cloud.tokenDuration", tokenDuration);
return this;
}
public DatabaseClientBuilder withCertificateFile(String file) {
props.put(PREFIX + "certificate.file", file);
return this;
}
public DatabaseClientBuilder withCertificatePassword(String password) {
props.put(PREFIX + "certificate.password", password);
return this;
}
public DatabaseClientBuilder withKerberosPrincipal(String principal) {
props.put(PREFIX + "kerberos.principal", principal);
return this;
}
public DatabaseClientBuilder withSAMLToken(String token) {
props.put(PREFIX + "saml.token", token);
return this;
}
public DatabaseClientBuilder withSSLContext(SSLContext sslContext) {
props.put(PREFIX + "sslContext", sslContext);
return this;
}
public DatabaseClientBuilder withSSLProtocol(String sslProtocol) {
props.put(PREFIX + "sslProtocol", sslProtocol);
return this;
}
public DatabaseClientBuilder withTrustManager(X509TrustManager trustManager) {
props.put(PREFIX + "trustManager", trustManager);
return this;
}
public DatabaseClientBuilder withSSLHostnameVerifier(DatabaseClientFactory.SSLHostnameVerifier sslHostnameVerifier) {
props.put(PREFIX + "sslHostnameVerifier", sslHostnameVerifier);
return this;
}
/**
* Prevents the underlying OkHttp library from sending an "Accept-Encoding-gzip" request header on each request.
*
* @return
* @since 6.3.0
*/
public DatabaseClientBuilder withGzippedResponsesDisabled() {
props.put(PREFIX + "disableGzippedResponses", true);
return this;
}
/**
* Enables 2-way SSL by creating an SSL context based on the given key store path.
*
* @param path
* @return
* @since 6.4.0
*/
public DatabaseClientBuilder withKeyStorePath(String path) {
props.put(PREFIX + "ssl.keystore.path", path);
return this;
}
/**
* @param password optional password for a key store
* @return
* @since 6.4.0
*/
public DatabaseClientBuilder withKeyStorePassword(String password) {
props.put(PREFIX + "ssl.keystore.password", password);
return this;
}
/**
* @param type e.g. "JKS"
* @return
* @since 6.4.0
*/
public DatabaseClientBuilder withKeyStoreType(String type) {
props.put(PREFIX + "ssl.keystore.type", type);
return this;
}
/**
* @param algorithm e.g. "SunX509"
* @return
* @since 6.4.0
*/
public DatabaseClientBuilder withKeyStoreAlgorithm(String algorithm) {
props.put(PREFIX + "ssl.keystore.algorithm", algorithm);
return this;
}
/**
* Supports constructing an {@code X509TrustManager} based on the given file path, which should point to a Java
* key store or trust store.
*
* @param path
* @return
* @since 6.5.0
*/
public DatabaseClientBuilder withTrustStorePath(String path) {
props.put(PREFIX + "ssl.truststore.path", path);
return this;
}
/**
* @param password optional password for a trust store
* @return
* @since 6.5.0
*/
public DatabaseClientBuilder withTrustStorePassword(String password) {
props.put(PREFIX + "ssl.truststore.password", password);
return this;
}
/**
* @param type e.g. "JKS"
* @return
* @since 6.5.0
*/
public DatabaseClientBuilder withTrustStoreType(String type) {
props.put(PREFIX + "ssl.truststore.type", type);
return this;
}
/**
* @param algorithm e.g. "SunX509"
* @return
* @since 6.5.0
*/
public DatabaseClientBuilder withTrustStoreAlgorithm(String algorithm) {
props.put(PREFIX + "ssl.truststore.algorithm", algorithm);
return this;
}
}