2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.sdnc;
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.ObjectMapper;
26 import java.util.Collections;
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;
39 //TODO move to common location
40 public class BaseClient<I,O> {
42 private HttpHeaders httpHeader;
43 private String targetUrl;
45 public HttpHeaders getHttpHeader() {
49 public void setHttpHeader(HttpHeaders httpHeader) {
50 this.httpHeader = httpHeader;
53 public String getTargetUrl() {
57 public void setTargetUrl(String targetUrl) {
58 this.targetUrl = targetUrl;
61 public O get(I data, Object... uriVariables) throws RestClientException {
62 return run(data, HttpMethod.GET, uriVariables);
65 public O post(I data, Object... uriVariables) throws RestClientException {
66 return run(data, HttpMethod.POST, uriVariables);
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,
81 return responseEntity.getBody();