/
MicroprofileClientBuilder.java
380 lines (310 loc) · 12.6 KB
/
MicroprofileClientBuilder.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package org.jboss.resteasy.client.microprofile;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Priorities;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.ext.ParamConverterProvider;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.RestClientDefinitionException;
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
import org.jboss.logging.Logger;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.specimpl.ResteasyUriBuilder;
/**
* Created by hbraun on 15.01.18.
*/
class MicroprofileClientBuilder implements RestClientBuilder {
private static final String DEFAULT_MAPPER_PROP = "microprofile.rest.client.disable.default.mapper";
private static final Logger LOG = Logger.getLogger(MicroprofileClientBuilder.class);
MicroprofileClientBuilder() {
ClientBuilder availableBuilder = ClientBuilder.newBuilder();
if (availableBuilder instanceof ResteasyClientBuilder) {
this.builderDelegate = (ResteasyClientBuilder) availableBuilder;
this.configurationWrapper = new ConfigurationWrapper(this.builderDelegate.getConfiguration());
Config cfg = null;
try {
ConfigProviderResolver.instance();
cfg = ConfigProvider.getConfig();
} catch (IllegalStateException ise) {
//ignore
}
this.config = cfg;
} else {
throw new IllegalStateException("Incompatible client builder found " + availableBuilder.getClass());
}
}
public Configuration getConfigurationWrapper() {
return this.configurationWrapper;
}
@Override
public RestClientBuilder baseUrl(URL url) {
try {
this.baseURI = url.toURI();
return this;
} catch (URISyntaxException e) {
throw new RuntimeException(e.getMessage());
}
}
@SuppressWarnings("unchecked")
@Override
public <T> T build(Class<T> aClass) throws IllegalStateException, RestClientDefinitionException {
// Interface validity
verifyInterface(aClass);
// Provider annotations
RegisterProvider[] providers = aClass.getAnnotationsByType(RegisterProvider.class);
for (RegisterProvider provider : providers) {
register(provider.value(), provider.priority());
}
// Default exception mapper
if (!isMapperDisabled()) {
register(DefaultResponseExceptionMapper.class);
}
this.builderDelegate.register(new ExceptionMapping(localProviderInstances), 1);
ClassLoader classLoader = aClass.getClassLoader();
List<String> noProxyHosts = Arrays.asList(
System.getProperty("http.nonProxyHosts", "localhost|127.*|[::1]").split("\\|"));
final T actualClient;
final String proxyHost = System.getProperty("http.proxyHost");
if (proxyHost != null && !noProxyHosts.contains(this.baseURI.getHost())) {
// Use proxy, if defined
actualClient = this.builderDelegate.defaultProxy(
proxyHost,
Integer.parseInt(System.getProperty("http.proxyPort", "80")))
.build()
.target(this.baseURI)
.proxyBuilder(aClass)
.classloader(classLoader)
.defaultConsumes(MediaType.TEXT_PLAIN)
.defaultProduces(MediaType.TEXT_PLAIN)
.build();
} else {
actualClient = this.builderDelegate.build()
.target(this.baseURI)
.proxyBuilder(aClass)
.classloader(classLoader)
.defaultConsumes(MediaType.TEXT_PLAIN)
.defaultProduces(MediaType.TEXT_PLAIN)
.build();
}
return (T) Proxy.newProxyInstance(
classLoader,
new Class[] {aClass},
new ProxyInvocationHandler(actualClient, getLocalProviderInstances())
);
}
private boolean isMapperDisabled() {
boolean disabled = false;
Optional<Boolean> defaultMapperProp = this.config != null ? this.config.getOptionalValue(DEFAULT_MAPPER_PROP, Boolean.class) : Optional.empty();
// disabled through config api
if (defaultMapperProp.isPresent() && defaultMapperProp.get().equals(Boolean.TRUE)) {
disabled = true;
} else if (!defaultMapperProp.isPresent()) {
// disabled through jaxrs property
try {
Object property = this.builderDelegate.getConfiguration().getProperty(DEFAULT_MAPPER_PROP);
if (property != null) {
disabled = (Boolean)property;
}
} catch (Throwable e) {
// ignore cast exception
}
}
return disabled;
}
private <T> void verifyInterface(Class<T> typeDef) {
Method[] methods = typeDef.getMethods();
// multiple verbs
for (Method method : methods) {
boolean hasHttpMethod = false;
for (Annotation annotation : method.getAnnotations()) {
boolean isHttpMethod = (annotation.annotationType().getAnnotation(HttpMethod.class) != null);
if (!hasHttpMethod && isHttpMethod) {
hasHttpMethod = true;
} else if (hasHttpMethod && isHttpMethod) {
throw new RestClientDefinitionException("Ambiguous @Httpmethod defintion on type " + typeDef);
}
}
}
// invalid parameter
Path classPathAnno = typeDef.getAnnotation(Path.class);
final Set<String> classLevelVariables = new HashSet<>();
ResteasyUriBuilder classTemplate = null;
if (classPathAnno != null) {
classTemplate = (ResteasyUriBuilder) UriBuilder.fromUri(classPathAnno.value());
classLevelVariables.addAll(classTemplate.getPathParamNamesInDeclarationOrder());
}
ResteasyUriBuilder template;
for (Method method : methods) {
Path methodPathAnno = method.getAnnotation(Path.class);
if (methodPathAnno != null) {
template = classPathAnno == null ? (ResteasyUriBuilder)UriBuilder.fromUri(methodPathAnno.value())
: (ResteasyUriBuilder)UriBuilder.fromUri(classPathAnno.value() + "/" + methodPathAnno.value());
} else {
template = classTemplate;
}
if (template == null) {
continue;
}
// it's not executed, so this can be anything - but a hostname needs to present
template.host("localhost");
Set<String> allVariables = new HashSet<>(template.getPathParamNamesInDeclarationOrder());
Map<String, Object> paramMap = new HashMap<>();
for (Parameter p : method.getParameters()) {
PathParam pathParam = p.getAnnotation(PathParam.class);
org.jboss.resteasy.annotations.jaxrs.PathParam pathParam2 = p.getAnnotation(org.jboss.resteasy.annotations.jaxrs.PathParam.class);
if (pathParam != null) {
paramMap.put(pathParam.value(), "foobar");
} else if (pathParam2 != null) {
paramMap.put((pathParam2.value() != null && pathParam2.value().length() > 0) ?
pathParam2.value() : p.getName(), "foobar");
}
}
if (allVariables.size() != paramMap.size()) {
throw new RestClientDefinitionException("Parameters and variables don't match on " + typeDef + "::" + method.getName());
}
try {
template.resolveTemplates(paramMap, false).build();
} catch (IllegalArgumentException ex) {
throw new RestClientDefinitionException("Parameter names don't match variable names on " + typeDef + "::" + method.getName(), ex);
}
}
}
@Override
public Configuration getConfiguration() {
return getConfigurationWrapper();
}
@Override
public RestClientBuilder property(String name, Object value) {
this.builderDelegate.property(name, value);
return this;
}
private static Object newInstanceOf(Class clazz) {
try {
return clazz.newInstance();
} catch (Throwable t) {
throw new RuntimeException("Failed to register " + clazz, t);
}
}
@Override
public RestClientBuilder register(Class<?> aClass) {
this.register(newInstanceOf(aClass));
return this;
}
@Override
public RestClientBuilder register(Class<?> aClass, int i) {
this.register(newInstanceOf(aClass), i);
return this;
}
@Override
public RestClientBuilder register(Class<?> aClass, Class<?>[] classes) {
this.register(newInstanceOf(aClass), classes);
return this;
}
@Override
public RestClientBuilder register(Class<?> aClass, Map<Class<?>, Integer> map) {
this.register(newInstanceOf(aClass), map);
return this;
}
@Override
public RestClientBuilder register(Object o) {
if (o instanceof ResponseExceptionMapper) {
ResponseExceptionMapper mapper = (ResponseExceptionMapper)o;
register(mapper, mapper.getPriority());
} else if (o instanceof ParamConverterProvider) {
register(o, Priorities.USER);
} else {
this.builderDelegate.register(o);
}
return this;
}
@Override
public RestClientBuilder register(Object o, int i) {
if (o instanceof ResponseExceptionMapper) {
// local
ResponseExceptionMapper mapper = (ResponseExceptionMapper)o;
HashMap<Class<?>, Integer> contracts = new HashMap<>();
contracts.put(ResponseExceptionMapper.class, i);
registerLocalProviderInstance(mapper, contracts);
// delegate
this.builderDelegate.register(mapper, i);
} else if (o instanceof ParamConverterProvider) {
// local
ParamConverterProvider converter = (ParamConverterProvider)o;
HashMap<Class<?>, Integer> contracts = new HashMap<>();
contracts.put(ParamConverterProvider.class, i);
registerLocalProviderInstance(converter, contracts);
// delegate
this.builderDelegate.register(converter, i);
} else {
this.builderDelegate.register(o, i);
}
return this;
}
@Override
public RestClientBuilder register(Object o, Class<?>[] classes) {
// local
for (Class<?> aClass : classes) {
if (aClass.isAssignableFrom(ResponseExceptionMapper.class)) {
register(o);
}
}
// other
this.builderDelegate.register(o, classes);
return this;
}
@Override
public RestClientBuilder register(Object o, Map<Class<?>, Integer> map) {
if (o instanceof ResponseExceptionMapper) {
//local
ResponseExceptionMapper mapper = (ResponseExceptionMapper)o;
HashMap<Class<?>, Integer> contracts = new HashMap<>();
contracts.put(ResponseExceptionMapper.class, map.get(ResponseExceptionMapper.class));
registerLocalProviderInstance(mapper, contracts);
// other
this.builderDelegate.register(o, map);
} else {
this.builderDelegate.register(o, map);
}
return this;
}
public Set<Object> getLocalProviderInstances() {
return localProviderInstances;
}
public void registerLocalProviderInstance(Object provider, Map<Class<?>, Integer> contracts) {
for (Object registered : getLocalProviderInstances()) {
if (registered == provider) {
LOG.infov("Provider already registered {0}", provider.getClass().getName());
return;
}
}
localProviderInstances.add(provider);
configurationWrapper.registerLocalContract(provider.getClass(), contracts);
}
private final ResteasyClientBuilder builderDelegate;
private final ConfigurationWrapper configurationWrapper;
private final Config config;
private URI baseURI;
private Set<Object> localProviderInstances = new HashSet<Object>();
}