Skip to content

Commit 80ae55b

Browse files
authored
Merge pull request #1522 from marklogic/feature/more-builder-methods
Added more builder methods
2 parents 5b54179 + 49a1b67 commit 80ae55b

File tree

7 files changed

+63
-39
lines changed

7 files changed

+63
-39
lines changed

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/fastfunctest/AbstractFunctionalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static void initializeClients() throws Exception {
5757
isML11OrHigher = version.getMajor() >= 11;
5858

5959
client = newDatabaseClientBuilder().build();
60-
schemasClient = newClientForDatabase("java-functest-schemas");
60+
schemasClient = newDatabaseClientBuilder().withDatabase("java-functest-schemas").build();
6161
adminModulesClient = newAdminModulesClient();
6262

6363
// Required to ensure that tests using the "/ext/" prefix work reliably. Expand to other directories as needed.

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/fastfunctest/TestDatabaseAuthentication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void testAuthenticationBasic() throws IOException
8181
String filename = "text-original.txt";
8282

8383
// connect the client
84-
DatabaseClient client = newBasicAuthClient("rest-writer", "x");
84+
DatabaseClient client = newDatabaseClientBuilder().withBasicAuth("rest-writer", "x").build();
8585

8686
// write doc
8787
writeDocumentUsingStringHandle(client, filename, "/write-text-doc-basic/", "Text");

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/fastfunctest/TestRuntimeDBselection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void testRuntimeDBclientWithDifferentAuthType() {
4343
String originalServerAuthentication = getServerAuthentication(getRestServerName());
4444
try {
4545
setAuthentication("basic", getRestServerName());
46-
client = newBasicAuthClient("eval-user", "x");
46+
client = newDatabaseClientBuilder().withBasicAuth("eval-user", "x").build();
4747
String insertJSON = "xdmp:document-insert(\"test2.json\",object-node {\"test\":\"hello\"})";
4848
client.newServerEval().xquery(insertJSON).eval();
4949
String query1 = "fn:count(fn:doc())";

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/functionaltest/ConnectedRESTQA.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,27 +2027,13 @@ public static DatabaseClientBuilder newDatabaseClientBuilder() {
20272027
return new DatabaseClientBuilder(props);
20282028
}
20292029

2030-
public static DatabaseClient newBasicAuthClient(String username, String password) {
2031-
return newDatabaseClientBuilder()
2032-
.withUsername(username)
2033-
.withPassword(password)
2034-
.withSecurityContextType("basic")
2035-
.build();
2036-
}
2037-
20382030
public static DatabaseClient newClientAsUser(String username, String password) {
20392031
return newDatabaseClientBuilder()
20402032
.withUsername(username)
20412033
.withPassword(password)
20422034
.build();
20432035
}
20442036

2045-
public static DatabaseClient newClientForDatabase(String database) {
2046-
return newDatabaseClientBuilder()
2047-
.withDatabase(database)
2048-
.build();
2049-
}
2050-
20512037
public static DatabaseClient newAdminModulesClient() {
20522038
return newDatabaseClientBuilder()
20532039
.withUsername(getAdminUser())

marklogic-client-api/src/main/java/com/marklogic/client/DatabaseClientBuilder.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@
3838
public class DatabaseClientBuilder {
3939

4040
public final static String PREFIX = "marklogic.client.";
41+
public final static String SECURITY_CONTEXT_TYPE_BASIC = "basic";
42+
public final static String SECURITY_CONTEXT_TYPE_DIGEST = "digest";
43+
public final static String SECURITY_CONTEXT_TYPE_MARKLOGIC_CLOUD = "cloud";
44+
public final static String SECURITY_CONTEXT_TYPE_KERBEROS = "kerberos";
45+
public final static String SECURITY_CONTEXT_TYPE_CERTIFICATE = "certificate";
46+
public final static String SECURITY_CONTEXT_TYPE_SAML = "saml";
47+
4148
private final Map<String, Object> props;
4249

4350
public DatabaseClientBuilder() {
@@ -113,11 +120,50 @@ public DatabaseClientBuilder withSecurityContext(DatabaseClientFactory.SecurityC
113120
return this;
114121
}
115122

123+
/**
124+
*
125+
* @param type must be one of "basic", "digest", "cloud", "kerberos", "certificate", or "saml"
126+
* @return
127+
*/
116128
public DatabaseClientBuilder withSecurityContextType(String type) {
117129
props.put(PREFIX + "securityContextType", type);
118130
return this;
119131
}
120132

133+
public DatabaseClientBuilder withBasicAuth(String username, String password) {
134+
return withSecurityContextType(SECURITY_CONTEXT_TYPE_BASIC)
135+
.withUsername(username)
136+
.withPassword(password);
137+
}
138+
139+
public DatabaseClientBuilder withDigestAuth(String username, String password) {
140+
return withSecurityContextType(SECURITY_CONTEXT_TYPE_DIGEST)
141+
.withUsername(username)
142+
.withPassword(password);
143+
}
144+
145+
public DatabaseClientBuilder withMarkLogicCloudAuth(String apiKey, String basePath) {
146+
return withSecurityContextType(SECURITY_CONTEXT_TYPE_MARKLOGIC_CLOUD)
147+
.withCloudApiKey(apiKey)
148+
.withBasePath(basePath);
149+
}
150+
151+
public DatabaseClientBuilder withKerberosAuth(String principal) {
152+
return withSecurityContextType(SECURITY_CONTEXT_TYPE_KERBEROS)
153+
.withKerberosPrincipal(principal);
154+
}
155+
156+
public DatabaseClientBuilder withCertificateAuth(String file, String password) {
157+
return withSecurityContextType(SECURITY_CONTEXT_TYPE_CERTIFICATE)
158+
.withCertificateFile(file)
159+
.withCertificatePassword(password);
160+
}
161+
162+
public DatabaseClientBuilder withSAMLAuth(String token) {
163+
return withSecurityContextType(SECURITY_CONTEXT_TYPE_SAML)
164+
.withSAMLToken(token);
165+
}
166+
121167
public DatabaseClientBuilder withConnectionType(DatabaseClient.ConnectionType type) {
122168
props.put(PREFIX + "connectionType", type);
123169
return this;

marklogic-client-api/src/main/java/com/marklogic/client/impl/DatabaseClientPropertySource.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
* Contains the implementation for the {@code DatabaseClientFactory.newClient} method that accepts a function as a
3232
* property source. Implementation is here primarily to ease readability and avoid making the factory class any larger
3333
* than it already is.
34+
*
35+
* @since 6.1.0
3436
*/
3537
public class DatabaseClientPropertySource {
3638

@@ -117,17 +119,17 @@ private DatabaseClientFactory.SecurityContext newSecurityContext() {
117119

118120
private DatabaseClientFactory.SecurityContext newSecurityContext(String type) {
119121
switch (type.toLowerCase()) {
120-
case "basic":
122+
case DatabaseClientBuilder.SECURITY_CONTEXT_TYPE_BASIC:
121123
return newBasicAuthContext();
122-
case "digest":
124+
case DatabaseClientBuilder.SECURITY_CONTEXT_TYPE_DIGEST:
123125
return newDigestAuthContext();
124-
case "cloud":
126+
case DatabaseClientBuilder.SECURITY_CONTEXT_TYPE_MARKLOGIC_CLOUD:
125127
return newCloudAuthContext();
126-
case "kerberos":
128+
case DatabaseClientBuilder.SECURITY_CONTEXT_TYPE_KERBEROS:
127129
return newKerberosAuthContext();
128-
case "certificate":
130+
case DatabaseClientBuilder.SECURITY_CONTEXT_TYPE_CERTIFICATE:
129131
return newCertificateAuthContext();
130-
case "saml":
132+
case DatabaseClientBuilder.SECURITY_CONTEXT_TYPE_SAML:
131133
return newSAMLAuthContext();
132134
default:
133135
throw new IllegalArgumentException("Unrecognized security context type: " + type);

marklogic-client-api/src/test/java/com/marklogic/client/test/DatabaseClientBuilderTest.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ void invalidSecurityContextType() {
7878
@Test
7979
void digest() {
8080
bean = Common.newClientBuilder()
81-
.withUsername("my-user")
82-
.withPassword("my-password")
83-
.withSecurityContextType("digest")
81+
.withDigestAuth("my-user", "my-password")
8482
.buildBean();
8583

8684
DatabaseClientFactory.DigestAuthContext context = (DatabaseClientFactory.DigestAuthContext) bean.getSecurityContext();
@@ -91,9 +89,7 @@ void digest() {
9189
@Test
9290
void basic() {
9391
bean = Common.newClientBuilder()
94-
.withUsername("my-user")
95-
.withPassword("my-password")
96-
.withSecurityContextType("basic")
92+
.withBasicAuth("my-user", "my-password")
9793
.buildBean();
9894

9995
DatabaseClientFactory.BasicAuthContext context = (DatabaseClientFactory.BasicAuthContext) bean.getSecurityContext();
@@ -104,9 +100,7 @@ void basic() {
104100
@Test
105101
void cloudWithBasePath() {
106102
bean = Common.newClientBuilder()
107-
.withSecurityContextType("cloud")
108-
.withCloudApiKey("my-key")
109-
.withBasePath("/my/path")
103+
.withMarkLogicCloudAuth("my-key", "/my/path")
110104
.buildBean();
111105

112106
DatabaseClientFactory.MarkLogicCloudAuthContext context =
@@ -127,8 +121,7 @@ void cloudNoApiKey() {
127121
@Test
128122
void kerberos() {
129123
bean = Common.newClientBuilder()
130-
.withSecurityContextType("kerberos")
131-
.withKerberosPrincipal("someone")
124+
.withKerberosAuth("someone")
132125
.buildBean();
133126

134127
DatabaseClientFactory.KerberosAuthContext context = (DatabaseClientFactory.KerberosAuthContext) bean.getSecurityContext();
@@ -138,9 +131,7 @@ void kerberos() {
138131
@Test
139132
void certificate() {
140133
DatabaseClientBuilder builder = Common.newClientBuilder()
141-
.withSecurityContextType("CERTificate")
142-
.withCertificateFile("not.found")
143-
.withCertificatePassword("passwd");
134+
.withCertificateAuth("not.found", "passwd");
144135

145136
Exception ex = assertThrows(Exception.class, () -> builder.buildBean());
146137
assertTrue(ex.getMessage().contains("Unable to create CertificateAuthContext"),
@@ -151,8 +142,7 @@ void certificate() {
151142
@Test
152143
void saml() {
153144
bean = Common.newClientBuilder()
154-
.withSecurityContextType("saml")
155-
.withSAMLToken("my-token")
145+
.withSAMLAuth("my-token")
156146
.buildBean();
157147

158148
DatabaseClientFactory.SAMLAuthContext context = (DatabaseClientFactory.SAMLAuthContext) bean.getSecurityContext();

0 commit comments

Comments
 (0)