-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSpringDirectorClientBuilder.java
133 lines (115 loc) · 5.74 KB
/
SpringDirectorClientBuilder.java
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
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.bosh.client;
import static org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.DEFAULT_CHARSET;
import java.io.IOException;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
/**
* @author David Ehringer
*/
public class SpringDirectorClientBuilder {
private String host;
private String username;
private String password;
public SpringDirectorClientBuilder withCredentials(String username, String password){
this.username = username;
this.password = password;
return this;
}
public SpringDirectorClientBuilder withHost(String host){
this.host = host;
return this;
}
public SpringDirectorClient build(){
// TODO validate
URI root = UriComponentsBuilder.newInstance().scheme("https").host(host).port(25555)
.build().toUri();
RestTemplate restTemplate = new RestTemplate(createRequestFactory(host, username, password));
restTemplate.getInterceptors().add(new ContentTypeClientHttpRequestInterceptor());
handleTextHtmlResponses(restTemplate);
return new SpringDirectorClient(root, restTemplate);
}
private ClientHttpRequestFactory createRequestFactory(String host, String username,
String password) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(host, 25555),
new UsernamePasswordCredentials(username, password));
SSLContext sslContext = null;
try {
sslContext = SSLContexts.custom()
.loadTrustMaterial(null, new TrustSelfSignedStrategy()).useTLS().build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
throw new DirectorException("Unable to configure ClientHttpRequestFactory", e);
}
SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext,
new AllowAllHostnameVerifier());
// disabling redirect handling is critical for the way BOSH uses 302's
HttpClient httpClient = HttpClientBuilder.create().disableRedirectHandling()
.setDefaultCredentialsProvider(credentialsProvider)
.setSSLSocketFactory(connectionFactory).build();
return new HttpComponentsClientHttpRequestFactory(httpClient);
}
private void handleTextHtmlResponses(RestTemplate restTemplate) {
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new StringHttpMessageConverter());
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
messageConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("application", "json",
DEFAULT_CHARSET), new MediaType("application", "*+json", DEFAULT_CHARSET),
new MediaType("text", "html", DEFAULT_CHARSET)));
messageConverters.add(messageConverter);
restTemplate.setMessageConverters(messageConverters);
}
private static class ContentTypeClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
ClientHttpResponse response = execution.execute(request, body);
// some BOSH resources return text/plain and this modifies this response
// so we can use Jackson
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return response;
}
}
}