Merge "[AAI] Fix doc config files"
[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-2019 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
23 package org.onap.aai.restclient;
24
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.Map;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.core.io.Resource;
33 import org.springframework.http.HttpEntity;
34 import org.springframework.http.HttpMethod;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.util.MultiValueMap;
37 import org.springframework.web.client.RestClientException;
38 import org.springframework.web.client.RestTemplate;
39
40 public abstract class RestClient {
41
42     private static Logger log = LoggerFactory.getLogger(RestClient.class);
43     @Value("${spring.application.name}")
44     protected String appName;
45
46     /**
47      * Execute the given http method against the uri with passed headers
48      *
49      * @param uri properly encoded, can include query params also properly encoded
50      * @param method http method of the request
51      * @param headers headers for the request
52      * @param body body of the request
53      * @return response of request
54      * @throws RestClientException on internal rest template exception or invalid url
55      */
56     public ResponseEntity execute(String uri, HttpMethod method, Map<String, String> headers, String body)
57             throws RestClientException {
58
59         HttpEntity<String> httpEntity;
60         log.debug("Request Headers: {}", headers);
61         if (body == null) {
62             httpEntity = new HttpEntity<>(getHeaders(headers));
63         } else {
64             httpEntity = new HttpEntity<>(body, getHeaders(headers));
65         }
66
67         // verify that either the base url ends with '/' or uri starts with '/', adjust uri accordingly.
68         if (getBaseUrl().endsWith("/") && uri.startsWith("/")) {
69             uri = uri.replaceFirst("/", "");
70         } else if (!getBaseUrl().endsWith("/") && !uri.startsWith("/")) {
71             uri = "/" + uri;
72         }
73
74         URI url;
75         try {
76             url = new URI(getBaseUrl() + uri);
77         } catch (URISyntaxException e) {
78             log.error("URL syntax error with url {}{}", getBaseUrl(), uri);
79             throw new RestClientException(e.getMessage());
80         }
81         log.debug("METHOD={}, URL={}, BODY={}", method, url, httpEntity.getBody());
82         ResponseEntity responseEntity = getRestTemplate().exchange(url, method, httpEntity, String.class);
83         log.trace("RESPONSE={}", responseEntity);
84         return responseEntity;
85     }
86
87     /**
88      * Execute the given http method against the uri with passed headers
89      *
90      * @param uri properly encoded, can include query params also properly encoded
91      * @param method http method of the request
92      * @param headers headers for the request
93      * @param body body of the request
94      * @return response of request
95      * @throws RestClientException on internal rest template exception or invalid url
96      */
97     public ResponseEntity execute(String uri, String method, Map<String, String> headers, String body)
98             throws RestClientException {
99         return execute(uri, HttpMethod.valueOf(method), headers, body);
100     }
101
102     /**
103      * Execute the given http method against the uri with passed headers
104      *
105      * @param uri properly encoded, can include query params also properly encoded
106      * @param method http method of the request
107      * @param headers headers for the request
108      * @return response of request
109      * @throws RestClientException on internal rest template exception or invalid url
110      */
111     public ResponseEntity execute(String uri, HttpMethod method, Map<String, String> headers)
112             throws RestClientException {
113         return execute(uri, method, headers, null);
114     }
115
116     /**
117      * Execute the given http method against the uri with passed headers
118      *
119      * @param uri properly encoded, can include query params also properly encoded
120      * @param method http method of the request
121      * @param headers headers for the request
122      * @return response of request
123      * @throws RestClientException on internal rest template exception or invalid url
124      */
125     public ResponseEntity execute(String uri, String method, Map<String, String> headers) throws RestClientException {
126         return execute(uri, HttpMethod.valueOf(method), headers, null);
127     }
128
129     public ResponseEntity executeResource(String uri, HttpMethod method, Map<String, String> headers, String body)
130             throws RestClientException {
131
132         HttpEntity httpEntity;
133         log.debug("Headers: " + headers.toString());
134         if (body == null) {
135             httpEntity = new HttpEntity(getHeaders(headers));
136         } else {
137             httpEntity = new HttpEntity(body, getHeaders(headers));
138         }
139         String url = getBaseUrl() + uri;
140         return getRestTemplate().exchange(url, method, httpEntity, Resource.class);
141     }
142
143     public ResponseEntity getGetRequest(String content, String uri, Map<String, String> headersMap) {
144         return this.execute(uri, HttpMethod.GET, headersMap, content);
145
146     }
147
148     public ResponseEntity getGetResource(String content, String uri, Map<String, String> headersMap) {
149         return this.executeResource(uri, HttpMethod.GET, headersMap, content);
150
151     }
152
153     public abstract RestTemplate getRestTemplate();
154
155     public abstract String getBaseUrl();
156
157     protected abstract MultiValueMap<String, String> getHeaders(Map<String, String> headers);
158
159 }