-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestTemplateProxyFactory.java
415 lines (368 loc) · 13.7 KB
/
RestTemplateProxyFactory.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package it.sourcery.remote.executor;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.util.UriComponentsBuilder;
public class RestTemplateProxyFactory {
public <T> T proxy(final Class<T> interfaceClass, final String baseUri){
return proxy(interfaceClass, baseUri, null, null);
}
public <T> T proxy(final Class<T> interfaceClass, final String baseUri, RestTemplate restTemplate){
return proxy(interfaceClass, baseUri, restTemplate, null);
}
public <T> T proxy(final Class<T> interfaceClass, final String baseUri, RestTemplate restTemplate, Executor asyncExecutor){
return (T) Proxy.newProxyInstance(
interfaceClass.getClassLoader(),
new Class[]{interfaceClass},
new RestTemplateInvocationHandler<T>(interfaceClass, baseUri, restTemplate, asyncExecutor)
);
}
private static ThreadFactory daemonThreadFactory() {
final ThreadFactory f = java.util.concurrent.Executors.defaultThreadFactory();
return new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = f.newThread(r);
t.setDaemon(true);
return t;
}
};
}
class RestTemplateInvocationHandler<T> implements InvocationHandler {
private Class<T> interfaceClass;
private List<MediaType> mediaTypes = new ArrayList<MediaType>();
private String baseUri;
private RestTemplate restTemplate;
private Executor asyncExecutor;
private RestTemplateInvocationHandler(Class<T> interfaceClass, String baseUri){
this(interfaceClass, baseUri, null, null);
}
private RestTemplateInvocationHandler(Class<T> interfaceClass, String baseUri, RestTemplate restTemplate, Executor asyncExecutor){
this.interfaceClass = interfaceClass;
this.baseUri = baseUri;
this.restTemplate = restTemplate;
if (this.restTemplate == null){
this.restTemplate = new RestTemplate();
}
this.asyncExecutor = asyncExecutor;
if (this.asyncExecutor == null){
this.asyncExecutor = Executors.newCachedThreadPool(daemonThreadFactory());
}
for(HttpMessageConverter<?> httpMessageConverter : this.restTemplate.getMessageConverters()){
mediaTypes.addAll(httpMessageConverter.getSupportedMediaTypes());
}
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri);
RequestMethod requestMethod = null;
Map<String, String> pathParams = new HashMap<String, String>();
Object messageBody = null;
HttpHeaders headers = new HttpHeaders();
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
List<MediaType> consumesMediaTypes = new ArrayList<MediaType>();
if (interfaceClass.isAnnotationPresent(RequestMapping.class)){
RequestMapping requestMapping = interfaceClass.getAnnotation(RequestMapping.class);
if (requestMapping.value() != null && requestMapping.value().length > 0){
uriComponentsBuilder.path(requestMapping.value()[0]);
}
if (requestMapping.method() != null && requestMapping.method().length > 0) {
requestMethod = requestMapping.method()[0];
}
if (requestMapping.consumes() != null && requestMapping.consumes().length > 0){
consumesMediaTypes.clear();
for (String stringMediaType : requestMapping.consumes()){
MediaType mediaType = MediaType.parseMediaType(stringMediaType);
consumesMediaTypes.add(mediaType);
}
}
if (requestMapping.produces() != null && requestMapping.produces().length > 0){
acceptableMediaTypes.clear();
for (String stringMediaType : requestMapping.produces()){
MediaType mediaType = MediaType.parseMediaType(stringMediaType);
acceptableMediaTypes.add(mediaType);
}
}
}
if (method.isAnnotationPresent(RequestMapping.class)){
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
if (requestMapping.value() != null && requestMapping.value().length > 0){
uriComponentsBuilder.path(requestMapping.value()[0]);
}
if (requestMapping.method() != null && requestMapping.method().length > 0) {
requestMethod = requestMapping.method()[0];
}
if (requestMapping.consumes() != null && requestMapping.consumes().length > 0){
consumesMediaTypes.clear();
for (String stringMediaType : requestMapping.consumes()){
MediaType mediaType = MediaType.parseMediaType(stringMediaType);
consumesMediaTypes.add(mediaType);
}
}
if (requestMapping.produces() != null && requestMapping.produces().length > 0){
acceptableMediaTypes.clear();
for (String stringMediaType : requestMapping.produces()){
MediaType mediaType = MediaType.parseMediaType(stringMediaType);
acceptableMediaTypes.add(mediaType);
}
}
}
int argumentId = 0;
for(Annotation[] annotations : method.getParameterAnnotations()){
for(Annotation annotation : annotations){
if (annotation.annotationType().isAssignableFrom(PathVariable.class)){
PathVariable pathVariable = (PathVariable)annotation;
pathParams.put(pathVariable.value(), getArgumentToString(args, argumentId));
}
if (annotation.annotationType().isAssignableFrom(RequestParam.class)){
RequestParam requestParam = (RequestParam)annotation;
String value = getArgumentToString(args, argumentId);
if (value == null){
value = requestParam.defaultValue();
}
if (value == null && requestParam.required()){
throw new IllegalArgumentException("argument " + argumentId + " cannot be null");
}
if (value != null){
uriComponentsBuilder.queryParam(requestParam.value(), value);
}
}
if (annotation.annotationType().isAssignableFrom(RequestHeader.class)){
RequestHeader requestHeader = (RequestHeader)annotation;
String value = getArgumentToString(args, argumentId);
if (value == null){
value = requestHeader.defaultValue();
}
if (value == null && requestHeader.required()){
throw new IllegalArgumentException("argument " + argumentId + " cannot be null");
}
if (value != null) {
headers.set(requestHeader.value(), value);
}
}
if (annotation.annotationType().isAssignableFrom(RequestBody.class)){
RequestBody requestBody = (RequestBody)annotation;
messageBody = args[argumentId];
}
}
argumentId++;
}
if (!acceptableMediaTypes.isEmpty()){
headers.setAccept(acceptableMediaTypes);
} else {
headers.setAccept(mediaTypes);
}
HttpEntity<?> requestEntity;
if (messageBody == null){
requestEntity = new HttpEntity<Object>(headers);
} else {
requestEntity = new HttpEntity<Object>(messageBody, headers);
if (!consumesMediaTypes.isEmpty()){
for (MediaType mediaType : consumesMediaTypes){
if (mediaTypes.contains(mediaType)){
headers.setContentType(mediaType);
}
}
if (headers.getContentType() == null){
String message = "Could not write request: no suitable HttpMessageConverter found for request types [" +
messageBody.getClass().getName() + "]";
message += " and content types [" + consumesMediaTypes.toString() + "]";
throw new RestClientException(message);
}
}
}
URI uri = uriComponentsBuilder.build().expand(pathParams).toUri();
Class<?> returnType = method.getReturnType();
boolean isAsync = false;
if (method.getReturnType().isAssignableFrom(DeferredResult.class)){
returnType = Object.class;
ParameterizedType type = (ParameterizedType) method.getGenericReturnType();
Type[] types = type.getActualTypeArguments();
if (types != null && types.length > 0){
returnType = (Class<?>)types[0];
}
isAsync = true;
}
if (isAsync){
return async(uri, convert(requestMethod), requestEntity, returnType);
} else {
return sync(uri, convert(requestMethod), requestEntity, returnType);
}
}
private String getArgumentToString(Object[] args, int argumentId) {
Object obj = args[argumentId];
if (obj == null) return null;
return obj.toString();
}
private HttpMethod convert(RequestMethod method){
if (method == null) return HttpMethod.GET;
switch (method){
case GET: return HttpMethod.GET;
case DELETE: return HttpMethod.DELETE;
case HEAD: return HttpMethod.HEAD;
case OPTIONS: return HttpMethod.OPTIONS;
case PATCH: return HttpMethod.PATCH;
case POST: return HttpMethod.POST;
case PUT: return HttpMethod.PUT;
case TRACE: return HttpMethod.TRACE;
default:
return HttpMethod.GET;
}
}
public <T> DeferredResult<T> async(final URI uri, final HttpMethod method, final HttpEntity<?> requestEntity, final Class<T> responseType){
if (asyncExecutor instanceof ExecutorService){
final FutureDeferredResult<T> deferredResult = new FutureDeferredResult<T>();
deferredResult.setFuture(((ExecutorService)asyncExecutor).submit(new Callable<T>() {
@Override
public T call() throws Exception {
try {
T body = sync(uri, method, requestEntity, responseType);
deferredResult.setResult(body);
return body;
} catch (RuntimeException t){
deferredResult.setErrorResult(t);
throw t;
} }
}));
return deferredResult;
} else {
final DeferredResult<T> deferredResult = new DeferredResult<T>();
asyncExecutor.execute(new Runnable() {
@Override
public void run() {
try {
T body = sync(uri, method, requestEntity, responseType);
deferredResult.setResult(body);
} catch (RuntimeException t){
deferredResult.setErrorResult(t);
}
}
});
return deferredResult;
}
}
public <T> T sync(URI uri, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType){
return restTemplate.exchange(uri, method, requestEntity, responseType).getBody();
}
}
class FutureDeferredResult<T> extends DeferredResult<T> implements Future<T> {
private Future<T> future;
private T response;
private Throwable throwable;
public void setFuture(Future<T> future){
this.future = future;
}
public boolean setResult(T result) {
this.response = result;
return super.setResult(result);
}
public boolean setErrorResult(Throwable result) {
this.throwable = result;
return super.setErrorResult(result);
}
/**
* @param mayInterruptIfRunning
* @return
* @see java.util.concurrent.Future#cancel(boolean)
*/
public boolean cancel(boolean mayInterruptIfRunning) {
if (future != null){
return future.cancel(mayInterruptIfRunning);
} else {
return false;
}
}
/**
* @return
* @see java.util.concurrent.Future#isCancelled()
*/
public boolean isCancelled() {
if (future != null){
return future.isCancelled();
} else {
return false;
}
}
/**
* @return
* @see java.util.concurrent.Future#isDone()
*/
public boolean isDone() {
if(response != null || throwable != null){
return true;
} else if (future != null){
return future.isDone();
} else {
return false;
}
}
/**
* @return
* @throws InterruptedException
* @throws ExecutionException
* @see java.util.concurrent.Future#get()
*/
public T get() throws InterruptedException, ExecutionException {
if(response != null){
return response;
} else if (throwable != null){
throw new ExecutionException(throwable);
} else if (future != null){
return future.get();
} else {
throw new ExecutionException(new IllegalStateException("You need to execute via async() or sync() call first"));
}
}
/**
* @param timeout
* @param unit
* @return
* @throws InterruptedException
* @throws ExecutionException
* @throws TimeoutException
* @see java.util.concurrent.Future#get(long, java.util.concurrent.TimeUnit)
*/
public T get(long timeout, TimeUnit unit) throws InterruptedException,
ExecutionException, TimeoutException {
if(response != null){
return response;
} else if (throwable != null){
throw new ExecutionException(throwable);
} else if (future != null){
return future.get(timeout, unit);
} else {
throw new ExecutionException(new IllegalStateException("You need to execute via async() or sync() call first"));
}
}
}
}