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.bpmn.common.baseclient;
23 import java.util.ArrayList;
24 import java.util.List;
25 import org.onap.so.logging.jaxrs.filter.SpringClientFilter;
26 import org.springframework.core.ParameterizedTypeReference;
27 import org.springframework.http.HttpEntity;
28 import org.springframework.http.HttpHeaders;
29 import org.springframework.http.HttpMethod;
30 import org.springframework.http.MediaType;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.http.client.BufferingClientHttpRequestFactory;
33 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
34 import org.springframework.web.client.RestClientException;
35 import org.springframework.web.client.RestTemplate;
37 // TODO move to common location
38 public class BaseClient<I, O> {
40 private HttpHeaders httpHeader;
41 private String targetUrl;
43 public HttpHeaders getHttpHeader() {
47 public HttpHeaders setDefaultHttpHeaders(String auth) {
48 httpHeader = new HttpHeaders();
49 httpHeader.set("Authorization", auth);
50 httpHeader.setContentType(MediaType.APPLICATION_JSON);
51 List<MediaType> acceptMediaTypes = new ArrayList<MediaType>();
52 acceptMediaTypes.add(MediaType.APPLICATION_JSON);
53 acceptMediaTypes.add(MediaType.TEXT_PLAIN);
54 httpHeader.setAccept(acceptMediaTypes);
58 public void setHttpHeader(HttpHeaders httpHeader) {
59 this.httpHeader = httpHeader;
62 public String getTargetUrl() {
66 public void setTargetUrl(String targetUrl) {
67 this.targetUrl = targetUrl;
70 public O get(I data, ParameterizedTypeReference<O> typeRef, Object... uriVariables) throws RestClientException {
71 return run(data, HttpMethod.GET, typeRef, uriVariables);
74 public O post(I data, ParameterizedTypeReference<O> typeRef, Object... uriVariables) throws RestClientException {
75 return run(data, HttpMethod.POST, typeRef, uriVariables);
78 public O run(I data, HttpMethod method, ParameterizedTypeReference<O> typeRef, Object... uriVariables)
79 throws RestClientException {
80 HttpEntity<I> requestEntity = new HttpEntity<I>(data, getHttpHeader());
81 RestTemplate restTemplate = new RestTemplate();
83 .setRequestFactory(new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory()));
84 restTemplate.getInterceptors().add(new SpringClientFilter());
85 ResponseEntity<O> responseEntity =
86 restTemplate.exchange(getTargetUrl(), method, requestEntity, typeRef, uriVariables);
87 return responseEntity.getBody();