c3511e69a46a5d85a2fa3cb3278a14d5afc2ed92
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / common / baseclient / 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.bpmn.common.baseclient;
22
23 import org.springframework.core.ParameterizedTypeReference;
24 import org.springframework.http.HttpEntity;
25 import org.springframework.http.HttpHeaders;
26 import org.springframework.http.HttpMethod;
27 import org.springframework.http.ResponseEntity;
28 import org.springframework.http.client.BufferingClientHttpRequestFactory;
29 import org.springframework.http.client.SimpleClientHttpRequestFactory;
30 import org.springframework.web.client.RestClientException;
31 import org.springframework.web.client.RestTemplate;
32
33 //TODO move to common location
34 public class BaseClient<I,O> {
35
36         private HttpHeaders httpHeader;
37         private String targetUrl;
38
39         public HttpHeaders getHttpHeader() {
40                 return httpHeader;
41         }
42
43         public void setHttpHeader(HttpHeaders httpHeader) {
44                 this.httpHeader = httpHeader;
45         }
46
47         public String getTargetUrl() {
48                 return targetUrl;
49         }
50
51         public void setTargetUrl(String targetUrl) {
52                 this.targetUrl = targetUrl;
53         }
54
55         public O get(I data, ParameterizedTypeReference<O> typeRef, Object... uriVariables) throws RestClientException {
56                 return run(data, HttpMethod.GET, typeRef, uriVariables);
57         }
58
59         public O post(I data, ParameterizedTypeReference<O> typeRef, Object... uriVariables) throws RestClientException {
60                 return run(data, HttpMethod.POST, typeRef, uriVariables);
61         }
62
63         public O run(I data, HttpMethod method, ParameterizedTypeReference<O> typeRef, Object... uriVariables) throws RestClientException {
64                 HttpEntity<I> requestEntity = new HttpEntity<I>(data, getHttpHeader());
65                 RestTemplate restTemplate = new RestTemplate();
66                 restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
67                 ResponseEntity<O> responseEntity = restTemplate.exchange(getTargetUrl(), method, requestEntity, typeRef,
68                                 uriVariables);
69                 return responseEntity.getBody();
70         }
71
72 }