Merge "fix to add SO VNFM adapter architecture images"
[so.git] / common / src / main / java / org / onap / so / client / 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;
22
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;
36
37 public class BaseClient<I, O> {
38
39     private HttpHeaders httpHeader;
40     private String targetUrl;
41
42     public HttpHeaders getHttpHeader() {
43         return httpHeader;
44     }
45
46     public HttpHeaders setDefaultHttpHeaders(String auth) {
47         httpHeader = new HttpHeaders();
48         httpHeader.set("Authorization", auth);
49         httpHeader.setContentType(MediaType.APPLICATION_JSON);
50         List<MediaType> acceptMediaTypes = new ArrayList<MediaType>();
51         acceptMediaTypes.add(MediaType.APPLICATION_JSON);
52         acceptMediaTypes.add(MediaType.TEXT_PLAIN);
53         httpHeader.setAccept(acceptMediaTypes);
54         return httpHeader;
55     }
56
57     public void setHttpHeader(HttpHeaders httpHeader) {
58         this.httpHeader = httpHeader;
59     }
60
61     public String getTargetUrl() {
62         return targetUrl;
63     }
64
65     public void setTargetUrl(String targetUrl) {
66         this.targetUrl = targetUrl;
67     }
68
69     public O get(I data, ParameterizedTypeReference<O> typeRef, Object... uriVariables) throws RestClientException {
70         return run(data, HttpMethod.GET, typeRef, uriVariables);
71     }
72
73     public O post(I data, ParameterizedTypeReference<O> typeRef, Object... uriVariables) throws RestClientException {
74         return run(data, HttpMethod.POST, typeRef, uriVariables);
75     }
76
77     public O run(I data, HttpMethod method, ParameterizedTypeReference<O> typeRef, Object... uriVariables)
78             throws RestClientException {
79         HttpEntity<I> requestEntity = new HttpEntity<I>(data, getHttpHeader());
80         RestTemplate restTemplate = new RestTemplate();
81         restTemplate
82                 .setRequestFactory(new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory()));
83         restTemplate.getInterceptors().add(new SpringClientFilter());
84         ResponseEntity<O> responseEntity =
85                 restTemplate.exchange(getTargetUrl(), method, requestEntity, typeRef, uriVariables);
86         return responseEntity.getBody();
87     }
88
89 }