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