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;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 import java.net.URISyntaxException;
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;
41 public abstract class RestClient {
43 private static Logger log = LoggerFactory.getLogger(RestClient.class);
44 @Value("${spring.application.name}")
45 protected String appName;
48 * Execute the given http method against the uri with passed headers
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
57 public ResponseEntity execute(String uri, HttpMethod method, Map<String, String> headers, String body)
58 throws RestClientException {
60 HttpEntity<String> httpEntity;
61 log.debug("Request Headers: {}", headers);
63 httpEntity = new HttpEntity<>(getHeaders(headers));
65 httpEntity = new HttpEntity<>(body, getHeaders(headers));
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("/")) {
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());
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;
89 * Execute the given http method against the uri with passed headers
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
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);
104 * Execute the given http method against the uri with passed headers
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
112 public ResponseEntity execute(String uri, HttpMethod method, Map<String, String> headers)
113 throws RestClientException {
114 return execute(uri, method, headers, null);
118 * Execute the given http method against the uri with passed headers
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
126 public ResponseEntity execute(String uri, String method, Map<String, String> headers) throws RestClientException {
127 return execute(uri, HttpMethod.valueOf(method), headers, null);
130 public ResponseEntity executeResource(String uri, HttpMethod method, Map<String, String> headers, String body)
131 throws RestClientException {
133 HttpEntity httpEntity;
134 log.debug("Headers: " + headers.toString());
136 httpEntity = new HttpEntity(getHeaders(headers));
138 httpEntity = new HttpEntity(body, getHeaders(headers));
140 String url = getBaseUrl() + uri;
141 return getRestTemplate().exchange(url, method, httpEntity, Resource.class);
144 public ResponseEntity getGetRequest(String content, String uri, Map<String, String> headersMap) {
145 return this.execute(uri, HttpMethod.GET, headersMap, content);
149 public ResponseEntity getGetResource(String content, String uri, Map<String, String> headersMap) {
150 return this.executeResource(uri, HttpMethod.GET, headersMap, content);
154 public abstract RestTemplate getRestTemplate();
156 public abstract String getBaseUrl();
158 protected abstract MultiValueMap<String, String> getHeaders(Map<String, String> headers);