a17880f3e98cb7e8d25da19aa4a57ad97f904738
[aai/aai-common.git] / aai-rest / src / main / java / org / onap / aai / restclient / RestClient.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.aai.restclient;
23
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.springframework.beans.factory.annotation.Value;
27 import org.springframework.core.io.Resource;
28 import org.springframework.http.HttpEntity;
29 import org.springframework.http.HttpMethod;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.util.MultiValueMap;
32 import org.springframework.web.client.RestClientException;
33 import org.springframework.web.client.RestTemplate;
34
35 import java.util.Map;
36
37 public abstract class RestClient {
38
39     private static EELFLogger log = EELFManager.getInstance().getLogger(RestClient.class);
40     @Value("${spring.application.name}")
41     protected String appName;
42
43     public ResponseEntity execute(String uri, HttpMethod method, Map<String, String> headers, String body) throws RestClientException {
44
45         HttpEntity httpEntity;
46         log.debug("Headers: " + headers.toString());
47         if (body == null) {
48             httpEntity = new HttpEntity(getHeaders(headers));
49         } else {
50             httpEntity = new HttpEntity(body, getHeaders(headers));
51         }
52         String url = getBaseUrl() + uri;
53         return getRestTemplate().exchange(url, method, httpEntity, String.class);
54     }
55
56     public ResponseEntity executeResource(String uri, HttpMethod method, Map<String, String> headers, String body) throws RestClientException {
57
58         HttpEntity httpEntity;
59         log.debug("Headers: " + headers.toString());
60         if (body == null) {
61             httpEntity = new HttpEntity(getHeaders(headers));
62         } else {
63             httpEntity = new HttpEntity(body, getHeaders(headers));
64         }
65         String url = getBaseUrl() + uri;
66         return getRestTemplate().exchange(url, method, httpEntity, Resource.class);
67     }
68
69     public ResponseEntity execute(String uri, String method, Map<String, String> headers) throws RestClientException {
70         return execute(uri, HttpMethod.valueOf(method), headers, null);
71     }
72
73     public ResponseEntity getGetRequest(String content, String uri, Map<String, String> headersMap) {
74         return this.execute(
75             uri,
76             HttpMethod.GET,
77             headersMap,
78             content);
79
80     }
81
82     public ResponseEntity getGetResource(String content, String uri, Map<String, String> headersMap) {
83         return this.executeResource(
84             uri,
85             HttpMethod.GET,
86             headersMap,
87             content);
88
89     }
90
91     public abstract RestTemplate getRestTemplate();
92
93     public abstract String getBaseUrl();
94
95     protected abstract MultiValueMap<String, String> getHeaders(Map<String, String> headers);
96
97     protected abstract EELFLogger getLogger();
98
99 }