2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.aai.restclient;
26 import java.net.URISyntaxException;
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;
40 public abstract class RestClient {
42 private static Logger log = LoggerFactory.getLogger(RestClient.class);
43 @Value("${spring.application.name}")
44 protected String appName;
47 * Execute the given http method against the uri with passed headers
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
56 public ResponseEntity execute(String uri, HttpMethod method, Map<String, String> headers, String body)
57 throws RestClientException {
59 HttpEntity<String> httpEntity;
60 log.debug("Request Headers: {}", headers);
62 httpEntity = new HttpEntity<>(getHeaders(headers));
64 httpEntity = new HttpEntity<>(body, getHeaders(headers));
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("/")) {
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());
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;
88 * Execute the given http method against the uri with passed headers
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
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);
103 * Execute the given http method against the uri with passed headers
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
111 public ResponseEntity execute(String uri, HttpMethod method, Map<String, String> headers)
112 throws RestClientException {
113 return execute(uri, method, headers, null);
117 * Execute the given http method against the uri with passed headers
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
125 public ResponseEntity execute(String uri, String method, Map<String, String> headers) throws RestClientException {
126 return execute(uri, HttpMethod.valueOf(method), headers, null);
129 public ResponseEntity executeResource(String uri, HttpMethod method, Map<String, String> headers, String body)
130 throws RestClientException {
132 HttpEntity httpEntity;
133 log.debug("Headers: " + headers.toString());
135 httpEntity = new HttpEntity(getHeaders(headers));
137 httpEntity = new HttpEntity(body, getHeaders(headers));
139 String url = getBaseUrl() + uri;
140 return getRestTemplate().exchange(url, method, httpEntity, Resource.class);
143 public ResponseEntity getGetRequest(String content, String uri, Map<String, String> headersMap) {
144 return this.execute(uri, HttpMethod.GET, headersMap, content);
148 public ResponseEntity getGetResource(String content, String uri, Map<String, String> headersMap) {
149 return this.executeResource(uri, HttpMethod.GET, headersMap, content);
153 public abstract RestTemplate getRestTemplate();
155 public abstract String getBaseUrl();
157 protected abstract MultiValueMap<String, String> getHeaders(Map<String, String> headers);