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 com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
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 EELFLogger log = EELFManager.getInstance().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("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={},http={}" + method, url, httpEntity);
84 ResponseEntity responseEntity = getRestTemplate().exchange(url, method, httpEntity, String.class);
85 log.debug("RESPONSE={}", responseEntity);
86 return responseEntity;
90 * Execute the given http method against the uri with passed headers
92 * @param uri properly encoded, can include query params also properly encoded
93 * @param method http method of the request
94 * @param headers headers for the request
95 * @param body body of the request
96 * @return response of request
97 * @throws RestClientException on internal rest template exception or invalid url
99 public ResponseEntity execute(String uri, String method, Map<String, String> headers, String body)
100 throws RestClientException {
101 return execute(uri, HttpMethod.valueOf(method), headers, body);
105 * Execute the given http method against the uri with passed headers
107 * @param uri properly encoded, can include query params also properly encoded
108 * @param method http method of the request
109 * @param headers headers for the request
110 * @return response of request
111 * @throws RestClientException on internal rest template exception or invalid url
113 public ResponseEntity execute(String uri, HttpMethod method, Map<String, String> headers)
114 throws RestClientException {
115 return execute(uri, method, headers, null);
119 * Execute the given http method against the uri with passed headers
121 * @param uri properly encoded, can include query params also properly encoded
122 * @param method http method of the request
123 * @param headers headers for the request
124 * @return response of request
125 * @throws RestClientException on internal rest template exception or invalid url
127 public ResponseEntity execute(String uri, String method, Map<String, String> headers) throws RestClientException {
128 return execute(uri, HttpMethod.valueOf(method), headers, null);
131 public ResponseEntity executeResource(String uri, HttpMethod method, Map<String, String> headers, String body)
132 throws RestClientException {
134 HttpEntity httpEntity;
135 log.debug("Headers: " + headers.toString());
137 httpEntity = new HttpEntity(getHeaders(headers));
139 httpEntity = new HttpEntity(body, getHeaders(headers));
141 String url = getBaseUrl() + uri;
142 return getRestTemplate().exchange(url, method, httpEntity, Resource.class);
145 public ResponseEntity getGetRequest(String content, String uri, Map<String, String> headersMap) {
146 return this.execute(uri, HttpMethod.GET, headersMap, content);
150 public ResponseEntity getGetResource(String content, String uri, Map<String, String> headersMap) {
151 return this.executeResource(uri, HttpMethod.GET, headersMap, content);
155 public abstract RestTemplate getRestTemplate();
157 public abstract String getBaseUrl();
159 protected abstract MultiValueMap<String, String> getHeaders(Map<String, String> headers);