Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 30b87b4

Browse files
authored
Merge pull request #462 from marklogic-community/feature/170-okhttp-refactor
DEVEXP-170 Using OkHttp to connect to Manage API
2 parents 7f49e91 + a302594 commit 30b87b4

File tree

11 files changed

+139
-12
lines changed

11 files changed

+139
-12
lines changed

build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ configurations.all {
3030
}
3131

3232
dependencies {
33-
api ('com.marklogic:ml-javaclient-util:4.5-SNAPSHOT') {
33+
api('com.marklogic:ml-javaclient-util:4.5-SNAPSHOT') {
3434
changing = true
3535
}
36-
api 'org.springframework:spring-web:5.3.24'
36+
api 'org.springframework:spring-web:5.3.24'
3737
api 'com.fasterxml.jackson.core:jackson-databind:2.14.1'
3838

3939
implementation 'jaxen:jaxen:1.2.0'
40+
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
41+
implementation 'io.github.rburgst:okhttp-digest:2.7'
4042
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
4143
implementation 'org.jdom:jdom2:2.0.6.1'
4244

src/main/java/com/marklogic/rest/util/HttpClientBuilderConfigurer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import org.apache.http.impl.client.HttpClientBuilder;
44

5+
/**
6+
* @deprecated since 4.5.0; OkHttp is now the preferred client
7+
*/
8+
@Deprecated
59
public interface HttpClientBuilderConfigurer {
610

711
HttpClientBuilder configureHttpClientBuilder(RestConfig restConfig, HttpClientBuilder httpClientBuilder);

src/main/java/com/marklogic/rest/util/RestConfig.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package com.marklogic.rest.util;
22

3+
import com.marklogic.client.DatabaseClientBuilder;
4+
import com.marklogic.client.DatabaseClientFactory;
5+
import com.marklogic.client.ext.modulesloader.ssl.SimpleX509TrustManager;
6+
import com.marklogic.client.ext.ssl.SslConfig;
7+
import com.marklogic.client.ext.ssl.SslUtil;
38
import org.apache.http.conn.ssl.X509HostnameVerifier;
9+
import org.springframework.util.StringUtils;
410

511
import javax.net.ssl.SSLContext;
612
import java.net.URI;
@@ -18,8 +24,9 @@ public class RestConfig {
1824
private boolean useDefaultKeystore;
1925
private String sslProtocol;
2026
private String trustManagementAlgorithm;
21-
27+
private DatabaseClientFactory.SSLHostnameVerifier sslHostnameVerifier;
2228
private SSLContext sslContext;
29+
@Deprecated
2330
private X509HostnameVerifier hostnameVerifier;
2431

2532
public RestConfig() {
@@ -37,12 +44,50 @@ public RestConfig(RestConfig other) {
3744
if (other.scheme != null) {
3845
this.scheme = other.scheme;
3946
}
47+
4048
this.configureSimpleSsl = other.configureSimpleSsl;
4149
this.useDefaultKeystore = other.useDefaultKeystore;
4250
this.sslProtocol = other.sslProtocol;
4351
this.trustManagementAlgorithm = other.trustManagementAlgorithm;
4452
this.sslContext = other.sslContext;
4553
this.hostnameVerifier = other.hostnameVerifier;
54+
this.sslHostnameVerifier = other.sslHostnameVerifier;
55+
}
56+
57+
public DatabaseClientBuilder newDatabaseClientBuilder() {
58+
DatabaseClientBuilder builder = new DatabaseClientBuilder()
59+
.withHost(getHost())
60+
.withPort(getPort())
61+
// TODO Will support all types before the 4.5.0 release
62+
.withSecurityContextType("digest")
63+
.withUsername(getUsername())
64+
.withPassword(getPassword());
65+
66+
if (getSslContext() != null) {
67+
builder.withSSLContext(getSslContext());
68+
} else {
69+
String sslProtocol = getSslProtocol();
70+
// The MarkLogic Java Client will default to using the JVM's default trust manager if none is specified.
71+
// This block though honors the existing "use default keystore" option, which allows for the user to also
72+
// specify a trust management algorithm.
73+
if (isUseDefaultKeystore()) {
74+
sslProtocol = StringUtils.hasText(sslProtocol) ? sslProtocol : SslUtil.DEFAULT_SSL_PROTOCOL;
75+
SslConfig sslConfig = SslUtil.configureUsingTrustManagerFactory(sslProtocol, getTrustManagementAlgorithm());
76+
builder
77+
.withSSLContext(sslConfig.getSslContext())
78+
.withTrustManager(sslConfig.getTrustManager());
79+
} else if (isConfigureSimpleSsl()) {
80+
builder
81+
.withSSLContext(StringUtils.hasText(sslProtocol) ?
82+
SimpleX509TrustManager.newSSLContext(sslProtocol) :
83+
SimpleX509TrustManager.newSSLContext())
84+
.withTrustManager(new SimpleX509TrustManager());
85+
} else {
86+
builder.withSSLProtocol(sslProtocol);
87+
}
88+
}
89+
90+
return builder;
4691
}
4792

4893
@Override
@@ -127,10 +172,12 @@ public void setSslContext(SSLContext sslContext) {
127172
this.sslContext = sslContext;
128173
}
129174

175+
@Deprecated
130176
public X509HostnameVerifier getHostnameVerifier() {
131177
return hostnameVerifier;
132178
}
133179

180+
@Deprecated
134181
public void setHostnameVerifier(X509HostnameVerifier hostnameVerifier) {
135182
this.hostnameVerifier = hostnameVerifier;
136183
}
@@ -158,4 +205,12 @@ public boolean isUseDefaultKeystore() {
158205
public void setUseDefaultKeystore(boolean useDefaultKeystore) {
159206
this.useDefaultKeystore = useDefaultKeystore;
160207
}
208+
209+
public DatabaseClientFactory.SSLHostnameVerifier getSslHostnameVerifier() {
210+
return sslHostnameVerifier;
211+
}
212+
213+
public void setSslHostnameVerifier(DatabaseClientFactory.SSLHostnameVerifier sslHostnameVerifier) {
214+
this.sslHostnameVerifier = sslHostnameVerifier;
215+
}
161216
}

src/main/java/com/marklogic/rest/util/RestTemplateUtil.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.marklogic.rest.util;
22

3+
import com.marklogic.client.DatabaseClientFactory;
4+
import com.marklogic.client.extra.okhttpclient.OkHttpClientBuilderFactory;
35
import com.marklogic.rest.util.configurer.BasicAuthConfigurer;
46
import com.marklogic.rest.util.configurer.NoConnectionReuseConfigurer;
57
import com.marklogic.rest.util.configurer.SslConfigurer;
68
import com.marklogic.rest.util.configurer.UseSystemPropertiesConfigurer;
9+
import okhttp3.OkHttpClient;
710
import org.apache.http.client.HttpClient;
811
import org.apache.http.impl.client.HttpClientBuilder;
912
import org.slf4j.Logger;
1013
import org.slf4j.LoggerFactory;
1114
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
15+
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
1216
import org.springframework.web.client.RestTemplate;
1317

1418
import java.util.ArrayList;
@@ -31,6 +35,10 @@ public class RestTemplateUtil {
3135

3236
private final static Logger logger = LoggerFactory.getLogger(RestTemplateUtil.class);
3337

38+
/**
39+
* @deprecated since 4.5.0; OkHttp is now the preferred client
40+
*/
41+
@Deprecated
3442
public static List<HttpClientBuilderConfigurer> DEFAULT_CONFIGURERS = new ArrayList<>();
3543

3644
static {
@@ -40,22 +48,52 @@ public class RestTemplateUtil {
4048
DEFAULT_CONFIGURERS.add(new UseSystemPropertiesConfigurer());
4149
}
4250

51+
/**
52+
* @deprecated since 4.5.0; OkHttp is now the preferred client
53+
*/
54+
@Deprecated
4355
public static RestTemplate newRestTemplate(String host, int port, String username, String password) {
4456
return newRestTemplate(new RestConfig(host, port, username, password));
4557
}
4658

47-
public static RestTemplate newRestTemplate(String host, int port, String username, String password, HttpClientBuilderConfigurer... configurers) {
48-
return newRestTemplate(new RestConfig(host, port, username, password), configurers);
59+
/**
60+
* As of 4.5.0, use this method for constructing a {@code RestTemplate} that supports all authentication types
61+
* supported by MarkLogic. ml-app-deployer is expected to use this for all calls as well starting in 4.5.0.
62+
*
63+
* @param config
64+
* @return
65+
*/
66+
public static RestTemplate newRestTemplate(RestConfig config) {
67+
DatabaseClientFactory.Bean bean = config.newDatabaseClientBuilder().buildBean();
68+
OkHttpClient client = OkHttpClientBuilderFactory
69+
.newOkHttpClientBuilder(bean.getHost(), bean.getPort(), bean.getSecurityContext())
70+
.build();
71+
72+
RestTemplate rt = new RestTemplate(new OkHttp3ClientHttpRequestFactory(client));
73+
rt.setErrorHandler(new MgmtResponseErrorHandler());
74+
return rt;
4975
}
5076

51-
public static RestTemplate newRestTemplate(RestConfig config) {
52-
return newRestTemplate(config, DEFAULT_CONFIGURERS);
77+
/**
78+
* @deprecated since 4.5.0; OkHttp is now the preferred client
79+
*/
80+
@Deprecated
81+
public static RestTemplate newRestTemplate(String host, int port, String username, String password, HttpClientBuilderConfigurer... configurers) {
82+
return newRestTemplate(new RestConfig(host, port, username, password), configurers);
5383
}
5484

85+
/**
86+
* @deprecated since 4.5.0; OkHttp is now the preferred client
87+
*/
88+
@Deprecated
5589
public static RestTemplate newRestTemplate(RestConfig config, List<HttpClientBuilderConfigurer> configurers) {
5690
return newRestTemplate(config, configurers.toArray(new HttpClientBuilderConfigurer[]{}));
5791
}
5892

93+
/**
94+
* @deprecated since 4.5.0; OkHttp is now the preferred client
95+
*/
96+
@Deprecated
5997
public static RestTemplate newRestTemplate(RestConfig config, HttpClientBuilderConfigurer... configurers) {
6098
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
6199

src/main/java/com/marklogic/rest/util/configurer/BasicAuthConfigurer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import org.apache.http.impl.client.BasicCredentialsProvider;
88
import org.apache.http.impl.client.HttpClientBuilder;
99

10+
/**
11+
* @deprecated since 4.5.0; OkHttp is now the preferred client
12+
*/
13+
@Deprecated
1014
public class BasicAuthConfigurer implements HttpClientBuilderConfigurer {
1115

1216
@Override

src/main/java/com/marklogic/rest/util/configurer/NoConnectionReuseConfigurer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import org.apache.http.impl.NoConnectionReuseStrategy;
66
import org.apache.http.impl.client.HttpClientBuilder;
77

8+
/**
9+
* @deprecated since 4.5.0; OkHttp is now the preferred client
10+
*/
11+
@Deprecated
812
public class NoConnectionReuseConfigurer implements HttpClientBuilderConfigurer {
913

1014
@Override

src/main/java/com/marklogic/rest/util/configurer/SslConfigurer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
import javax.net.ssl.SSLContext;
1313

14+
/**
15+
* @deprecated since 4.5.0; OkHttp is now the preferred client
16+
*/
17+
@Deprecated
1418
public class SslConfigurer extends LoggingObject implements HttpClientBuilderConfigurer {
1519

1620
/**

src/main/java/com/marklogic/rest/util/configurer/UseSystemPropertiesConfigurer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import com.marklogic.rest.util.RestConfig;
55
import org.apache.http.impl.client.HttpClientBuilder;
66

7+
/**
8+
* @deprecated since 4.5.0; OkHttp is now the preferred client
9+
*/
10+
@Deprecated
711
public class UseSystemPropertiesConfigurer implements HttpClientBuilderConfigurer {
812

913
@Override

src/test/java/com/marklogic/appdeployer/command/viewschemas/ManageViewSchemasTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.marklogic.mgmt.resource.ResourceManager;
99
import com.marklogic.mgmt.resource.clusters.ClusterManager;
1010
import com.marklogic.mgmt.resource.viewschemas.ViewSchemaManager;
11+
import com.marklogic.rest.util.RestConfig;
1112
import com.marklogic.rest.util.RestTemplateUtil;
1213
import org.jdom2.Namespace;
1314
import org.springframework.http.ResponseEntity;
@@ -56,8 +57,9 @@ protected void verifyResourcesWereDeleted(ResourceManager mgr) {
5657
*/
5758
@Override
5859
protected void afterResourcesCreated() {
59-
RestTemplate clientTemplate = RestTemplateUtil.newRestTemplate(appConfig.getHost(), appConfig.getRestPort(),
60+
RestConfig restConfig = new RestConfig(appConfig.getHost(), appConfig.getRestPort(),
6061
appConfig.getRestAdminUsername(), appConfig.getRestAdminPassword());
62+
RestTemplate clientTemplate = RestTemplateUtil.newRestTemplate(restConfig);
6163

6264
String baseUrl = format("http://%s:%d", appConfig.getHost(), appConfig.getRestPort());
6365

src/test/java/com/marklogic/mgmt/ManageClientTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class ManageClientTest {
99
public void determineUsernameForSecurityUserRequest() {
1010
ManageConfig config = new ManageConfig();
1111
config.setSecurityUsername("admin");
12+
config.setSecurityPassword("admin");
1213
config.setUsername("someone");
1314

1415
ManageClient client = new ManageClient(config);

0 commit comments

Comments
 (0)