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
|
package org.onap.sdc.dcae.catalog.commons;
import java.util.List;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.client.AsyncRestTemplate;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
public class Http {
protected Http() {
}
public static <T> Future<T> exchange(String theUri, HttpMethod theMethod, HttpEntity theRequest, Class<T> theResponseType) {
AsyncRestTemplate restTemplate = new AsyncRestTemplate();
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
converters.add(0, new JSONHttpMessageConverter());
restTemplate.setMessageConverters(converters);
HttpFuture<T> result = new HttpFuture<T>();
try {
restTemplate
.exchange(theUri, theMethod, theRequest, theResponseType)
.addCallback(result.callback);
}
catch (RestClientException rcx) {
return Futures.failedFuture(rcx);
}
catch (Exception x) {
return Futures.failedFuture(x);
}
return result;
}
/**
*
* @param theUri
* @param theMethod
* @param theRequest
* @param theResponseType
* @param readTimeOut pass -1 if you dont need to customize the read time out interval
* @return
*/
public static <T> ResponseEntity<T> exchangeSync(String theUri, HttpMethod theMethod, HttpEntity theRequest, Class<T> theResponseType, int readTimeOut) {
RestTemplate restTemplate = new RestTemplate();
if(readTimeOut!=-1){
SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
rf.setReadTimeout(1 * readTimeOut);
}
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
converters.add(0, new JSONHttpMessageConverter());
restTemplate.setMessageConverters(converters);
ResponseEntity<T> result = null;
try {
result = restTemplate.exchange(theUri, theMethod, theRequest, theResponseType);
}
catch (RestClientException rcx) {
return new ResponseEntity<T>((T) rcx.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
catch (Exception x) {
return new ResponseEntity<T>((T) x.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
return result;
}
public static class HttpFuture<T> extends Futures.BasicFuture<T> {
HttpFuture() {
}
ListenableFutureCallback<ResponseEntity<T>> callback = new ListenableFutureCallback<ResponseEntity<T>>() {
public void onSuccess(ResponseEntity<T> theResult) {
HttpFuture.this.result(theResult.getBody());
}
public void onFailure(Throwable theError) {
if (theError instanceof HttpClientErrorException) {
HttpFuture.this.cause(new Exception((HttpClientErrorException)theError));
}
else {
HttpFuture.this.cause(theError);
}
}
};
}
}
|