DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_catalog / commons / src / main / java / org / onap / sdc / dcae / catalog / commons / Http.java
1 package org.onap.sdc.dcae.catalog.commons;
2
3 import java.util.List;
4
5 import org.springframework.http.HttpEntity;
6 import org.springframework.http.HttpMethod;
7 import org.springframework.http.HttpStatus;
8 import org.springframework.http.ResponseEntity;
9 import org.springframework.http.client.SimpleClientHttpRequestFactory;
10 import org.springframework.http.converter.HttpMessageConverter;
11 import org.springframework.util.concurrent.ListenableFutureCallback;
12 import org.springframework.web.client.AsyncRestTemplate;
13 import org.springframework.web.client.HttpClientErrorException;
14 import org.springframework.web.client.RestClientException;
15 import org.springframework.web.client.RestTemplate;
16
17 public class Http {
18
19         protected Http() {
20         }
21         
22         
23         public static <T> Future<T> exchange(String theUri, HttpMethod theMethod, HttpEntity theRequest, Class<T> theResponseType) {
24         
25                 AsyncRestTemplate restTemplate = new AsyncRestTemplate();
26
27                 List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
28                 converters.add(0, new JSONHttpMessageConverter());
29                 restTemplate.setMessageConverters(converters);
30
31                 HttpFuture<T> result = new HttpFuture<T>();
32                 try {
33                         restTemplate
34                                 .exchange(theUri, theMethod, theRequest, theResponseType)
35                                         .addCallback(result.callback);
36                 }
37                 catch (RestClientException rcx) {
38                         return Futures.failedFuture(rcx);
39                 }
40                 catch (Exception x) {
41                         return Futures.failedFuture(x);
42                 }
43          
44                 return result;
45         }
46         
47         /**
48          * 
49          * @param theUri
50          * @param theMethod
51          * @param theRequest
52          * @param theResponseType
53          * @param readTimeOut pass -1 if you dont need to customize the read time out interval
54          * @return
55          */
56         public static <T> ResponseEntity<T> exchangeSync(String theUri, HttpMethod theMethod, HttpEntity theRequest, Class<T> theResponseType, int readTimeOut) {
57                 
58                 RestTemplate restTemplate = new RestTemplate();
59                 
60                 if(readTimeOut!=-1){
61                         SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
62                         rf.setReadTimeout(1 * readTimeOut);
63                 }
64
65                 List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
66                 converters.add(0, new JSONHttpMessageConverter());
67                 restTemplate.setMessageConverters(converters);
68                 ResponseEntity<T> result = null;
69
70                 try {
71                         result = restTemplate.exchange(theUri, theMethod, theRequest, theResponseType);
72                 }
73                 catch (RestClientException rcx) {
74                         return new ResponseEntity<T>((T) rcx.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
75                 }
76                 catch (Exception x) {
77                         return new ResponseEntity<T>((T) x.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
78                 }
79          
80                 return result;
81         }
82
83
84
85         public static class HttpFuture<T> extends Futures.BasicFuture<T> {
86
87                 HttpFuture() {
88                 }
89
90                 ListenableFutureCallback<ResponseEntity<T>> callback = new ListenableFutureCallback<ResponseEntity<T>>() {
91
92                         public void     onSuccess(ResponseEntity<T> theResult) {
93                                 HttpFuture.this.result(theResult.getBody());
94                         }
95
96                         public void     onFailure(Throwable theError) {
97                                 if (theError instanceof HttpClientErrorException) {
98                                         HttpFuture.this.cause(new Exception((HttpClientErrorException)theError));
99                                 }
100                                 else {
101                                         HttpFuture.this.cause(theError);
102                                 }
103                         }
104                 };
105
106         }
107 }