|
1 | 1 | package com.marklogic.rest.util; |
2 | 2 |
|
3 | | -import org.apache.http.auth.AuthScope; |
4 | | -import org.apache.http.auth.UsernamePasswordCredentials; |
| 3 | +import com.marklogic.rest.util.configurer.BasicAuthConfigurer; |
| 4 | +import com.marklogic.rest.util.configurer.NoConnectionReuseConfigurer; |
| 5 | +import com.marklogic.rest.util.configurer.SslConfigurer; |
| 6 | +import com.marklogic.rest.util.configurer.UseSystemPropertiesConfigurer; |
5 | 7 | import org.apache.http.client.HttpClient; |
6 | | -import org.apache.http.conn.ssl.SSLContextBuilder; |
7 | | -import org.apache.http.conn.ssl.TrustStrategy; |
8 | | -import org.apache.http.conn.ssl.X509HostnameVerifier; |
9 | | -import org.apache.http.impl.NoConnectionReuseStrategy; |
10 | | -import org.apache.http.impl.client.BasicCredentialsProvider; |
11 | 8 | import org.apache.http.impl.client.HttpClientBuilder; |
12 | 9 | import org.slf4j.Logger; |
13 | 10 | import org.slf4j.LoggerFactory; |
14 | 11 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
15 | 12 | import org.springframework.web.client.RestTemplate; |
16 | 13 |
|
17 | | -import javax.net.ssl.SSLContext; |
18 | | -import javax.net.ssl.SSLSession; |
19 | | -import javax.net.ssl.SSLSocket; |
20 | | -import java.security.cert.X509Certificate; |
21 | | - |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +/** |
| 18 | + * Factory class for constructing a Spring RestTemplate for communicating with the MarkLogic Manage API. |
| 19 | + * <p> |
| 20 | + * This class uses an org.apache.http.impl.client.HttpClientBuilder to construct an |
| 21 | + * org.apache.http.client.HttpClient that is then used to construct a RestTemplate. Instances of the interface |
| 22 | + * HttpClientBuilderConfigurer can be passed in to customize how the HttpClient is constructed. If no instances are passed |
| 23 | + * in, then the configurers defined by DEFAULT_CONFIGURERS are used. |
| 24 | + * </p> |
| 25 | + * <p> |
| 26 | + * The DEFAULT_CONFIGURERS variable is public and static but not final so that clients of ml-app-deployer can |
| 27 | + * fiddle with it to customize how classes within ml-app-deployer construct a RestTemplate. |
| 28 | + * </p> |
| 29 | + */ |
22 | 30 | public class RestTemplateUtil { |
23 | 31 |
|
24 | 32 | private final static Logger logger = LoggerFactory.getLogger(RestTemplateUtil.class); |
25 | 33 |
|
| 34 | + public static List<HttpClientBuilderConfigurer> DEFAULT_CONFIGURERS = new ArrayList<>(); |
| 35 | + |
| 36 | + static { |
| 37 | + DEFAULT_CONFIGURERS.add(new BasicAuthConfigurer()); |
| 38 | + DEFAULT_CONFIGURERS.add(new SslConfigurer()); |
| 39 | + DEFAULT_CONFIGURERS.add(new NoConnectionReuseConfigurer()); |
| 40 | + DEFAULT_CONFIGURERS.add(new UseSystemPropertiesConfigurer()); |
| 41 | + } |
| 42 | + |
26 | 43 | public static RestTemplate newRestTemplate(String host, int port, String username, String password) { |
27 | 44 | return newRestTemplate(new RestConfig(host, port, username, password)); |
28 | 45 | } |
29 | 46 |
|
30 | | - public static RestTemplate newRestTemplate(RestConfig config) { |
31 | | - HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); |
| 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); |
| 49 | + } |
32 | 50 |
|
33 | | - String username = config.getUsername(); |
34 | | - if (username != null) { |
35 | | - BasicCredentialsProvider prov = new BasicCredentialsProvider(); |
36 | | - prov.setCredentials(new AuthScope(config.getHost(), config.getPort(), AuthScope.ANY_REALM), |
37 | | - new UsernamePasswordCredentials(username, config.getPassword())); |
38 | | - httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(prov); |
39 | | - } |
| 51 | + public static RestTemplate newRestTemplate(RestConfig config) { |
| 52 | + return newRestTemplate(config, DEFAULT_CONFIGURERS); |
| 53 | + } |
40 | 54 |
|
41 | | - if (config.isConfigureSimpleSsl()) { |
42 | | - if (logger.isInfoEnabled()) { |
43 | | - logger.info("Configuring simple SSL approach for connecting to: " + config.getBaseUrl()); |
44 | | - } |
45 | | - configureSimpleSsl(httpClientBuilder); |
46 | | - } |
| 55 | + public static RestTemplate newRestTemplate(RestConfig config, List<HttpClientBuilderConfigurer> configurers) { |
| 56 | + return newRestTemplate(config, configurers.toArray(new HttpClientBuilderConfigurer[]{})); |
| 57 | + } |
47 | 58 |
|
48 | | - if (config.getSslContext() != null) { |
49 | | - if (logger.isInfoEnabled()) { |
50 | | - logger.info("Using custom SSLContext for connecting to: " + config.getBaseUrl()); |
51 | | - } |
52 | | - httpClientBuilder.setSslcontext(config.getSslContext()); |
53 | | - } |
| 59 | + public static RestTemplate newRestTemplate(RestConfig config, HttpClientBuilderConfigurer... configurers) { |
| 60 | + HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); |
54 | 61 |
|
55 | | - if (config.getHostnameVerifier() != null) { |
56 | | - if (logger.isInfoEnabled()) { |
57 | | - logger.info("Using custom X509HostnameVerifier for connecting to: " + config.getBaseUrl()); |
| 62 | + if (configurers != null) { |
| 63 | + for (HttpClientBuilderConfigurer configurer : configurers) { |
| 64 | + if (logger.isDebugEnabled()) { |
| 65 | + logger.debug("Applying HttpClientBuilderConfigurer: " + configurer); |
| 66 | + } |
| 67 | + httpClientBuilder = configurer.configureHttpClientBuilder(config, httpClientBuilder); |
58 | 68 | } |
59 | | - httpClientBuilder.setHostnameVerifier(config.getHostnameVerifier()); |
60 | 69 | } |
61 | 70 |
|
62 | | - httpClientBuilder.setConnectionReuseStrategy(new NoConnectionReuseStrategy()); |
63 | 71 | HttpClient client = httpClientBuilder.build(); |
64 | | - |
65 | 72 | RestTemplate rt = new RestTemplate(new HttpComponentsClientHttpRequestFactory(client)); |
66 | 73 | rt.setErrorHandler(new MgmtResponseErrorHandler()); |
67 | 74 | return rt; |
68 | 75 | } |
69 | 76 |
|
70 | | - private static void configureSimpleSsl(HttpClientBuilder httpClientBuilder) { |
71 | | - try { |
72 | | - SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { |
73 | | - @Override |
74 | | - public boolean isTrusted(X509Certificate[] chain, String authType) { |
75 | | - return true; |
76 | | - } |
77 | | - }).build(); |
78 | | - httpClientBuilder.setSslcontext(sslContext); |
79 | | - } catch (Exception ex) { |
80 | | - throw new RuntimeException("Unable to configure simple SSLContext, cause: " + ex.getMessage(), ex); |
81 | | - } |
82 | | - |
83 | | - httpClientBuilder.setHostnameVerifier(new X509HostnameVerifier() { |
84 | | - @Override |
85 | | - public void verify(String host, SSLSocket ssl) { |
86 | | - } |
87 | | - |
88 | | - @Override |
89 | | - public void verify(String host, X509Certificate cert) { |
90 | | - } |
91 | | - |
92 | | - @Override |
93 | | - public void verify(String host, String[] cns, String[] subjectAlts) { |
94 | | - } |
95 | | - |
96 | | - @Override |
97 | | - public boolean verify(String s, SSLSession sslSession) { |
98 | | - return false; |
99 | | - } |
100 | | - }); |
101 | | - } |
102 | 77 | } |
0 commit comments