Containerization feature of SO
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / sdnc / BaseClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client.sdnc;
22
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25
26 import java.util.Collections;
27
28 import org.springframework.core.ParameterizedTypeReference;
29 import org.springframework.http.HttpEntity;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.HttpMethod;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.http.client.BufferingClientHttpRequestFactory;
34 import org.springframework.http.client.SimpleClientHttpRequestFactory;
35 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
36 import org.springframework.web.client.RestClientException;
37 import org.springframework.web.client.RestTemplate;
38
39 //TODO move to common location
40 public class BaseClient<I,O> {
41
42         private HttpHeaders httpHeader;
43         private String targetUrl;
44
45         public HttpHeaders getHttpHeader() {
46                 return httpHeader;
47         }
48
49         public void setHttpHeader(HttpHeaders httpHeader) {
50                 this.httpHeader = httpHeader;
51         }
52
53         public String getTargetUrl() {
54                 return targetUrl;
55         }
56
57         public void setTargetUrl(String targetUrl) {
58                 this.targetUrl = targetUrl;
59         }
60
61         public O get(I data, Object... uriVariables) throws RestClientException {
62                 return run(data, HttpMethod.GET, uriVariables);
63         }
64
65         public O post(I data, Object... uriVariables) throws RestClientException {
66                 return run(data, HttpMethod.POST, uriVariables);
67         }
68
69         public O run(I data, HttpMethod method, Object... uriVariables) throws RestClientException {
70                 HttpEntity<I> requestEntity = new HttpEntity<I>(data, getHttpHeader());
71                 RestTemplate restTemplate = new RestTemplate();
72                 ObjectMapper mapper = new ObjectMapper();
73                 MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
74         converter.setObjectMapper(mapper);
75         mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
76         restTemplate.getMessageConverters().add(0, converter);
77                 restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
78                 ParameterizedTypeReference<O> output = new ParameterizedTypeReference<O>() {};
79                 ResponseEntity<O> responseEntity = restTemplate.exchange(getTargetUrl(), method, requestEntity, output,
80                                 uriVariables);
81                 return responseEntity.getBody();
82         }
83
84 }