-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpringDirectorClientBuilder.java
More file actions
163 lines (137 loc) · 6.86 KB
/
SpringDirectorClientBuilder.java
File metadata and controls
163 lines (137 loc) · 6.86 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
/*
* 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.net.URISyntaxException;
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 io.bosh.client.authentication.Authentication;
import io.bosh.client.authentication.BasicAuth;
import io.bosh.client.authentication.OAuth;
import org.apache.http.auth.AUTH;
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.*;
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;
private Authentication auth;
private Scheme scheme;
private Integer boshPort;
public SpringDirectorClientBuilder withCredentials(String username, String password, Authentication auth,
Scheme scheme, Integer boshPort) {
this.username = username;
this.password = password;
this.auth = auth;
this.scheme = scheme;
this.boshPort = boshPort;
return this;
}
public SpringDirectorClientBuilder withHost(String host) {
this.host = host;
return this;
}
public SpringDirectorClient build() {
// TODO validate
URI root = UriComponentsBuilder.newInstance().scheme(scheme.name()).host(host).port(boshPort)
.build().toUri();
RestTemplate restTemplate = null;
try {
restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(createRequestFactory(host, username, password, auth, scheme, boshPort)));
} catch (URISyntaxException e) {
e.printStackTrace();
}
restTemplate.getInterceptors().add(new ContentTypeClientHttpRequestInterceptor());
restTemplate.getInterceptors().add(new RequestLoggingInterceptor());
handleTextHtmlResponses(restTemplate);
return new SpringDirectorClient(root, restTemplate);
}
private ClientHttpRequestFactory createRequestFactory(String host, String username,
String password, Authentication auth, Scheme scheme, Integer boshPort) throws URISyntaxException {
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());
HttpClient httpClient;
if (auth.getClass().equals(BasicAuth.class)) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(host, boshPort),
new UsernamePasswordCredentials(username, password));
// disabling redirect handling is critical for the way BOSH uses 302's
httpClient = HttpClientBuilder.create().disableRedirectHandling()
.setDefaultCredentialsProvider(credentialsProvider)
.setSSLSocketFactory(connectionFactory).build();
} else {
// disabling redirect handling is critical for the way BOSH uses 302's
httpClient = HttpClientBuilder.create().disableRedirectHandling()
.setDefaultHeaders(Arrays.asList(new OAuthCredentialsProvider(host, username, password, scheme.name(), (OAuth) auth)))
.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;
}
}
}