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