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=========================================================
22 package org.onap.aai.restclient;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.springframework.beans.factory.annotation.Value;
27 import org.springframework.core.io.Resource;
28 import org.springframework.http.HttpEntity;
29 import org.springframework.http.HttpMethod;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.util.MultiValueMap;
32 import org.springframework.web.client.RestClientException;
33 import org.springframework.web.client.RestTemplate;
36 import java.net.URISyntaxException;
39 public abstract class RestClient {
41 private static EELFLogger log = EELFManager.getInstance().getLogger(RestClient.class);
42 @Value("${spring.application.name}")
43 protected String appName;
47 * Execute the given http method against the uri with passed headers
48 * @param uri properly encoded, can include query params also properly encoded
49 * @param method http method of the request
50 * @param headers headers for the request
51 * @param body body of the request
52 * @return response of request
53 * @throws RestClientException on internal rest template exception or invalid url
55 public ResponseEntity execute(String uri, HttpMethod method, Map<String,String> headers, String body) throws RestClientException {
57 HttpEntity<String> httpEntity;
58 log.debug ("Headers: {}", headers);
60 httpEntity = new HttpEntity<>(getHeaders(headers));
62 httpEntity = new HttpEntity<>(body, getHeaders(headers));
65 // verify that either the base url ends with '/' or uri starts with '/', adjust uri accordingly.
66 if (getBaseUrl().endsWith("/") && uri.startsWith("/")) {
67 uri = uri.replaceFirst("/", "");
68 } else if (!getBaseUrl().endsWith("/") && !uri.startsWith("/")) {
74 url = new URI(getBaseUrl() + uri);
75 } catch (URISyntaxException e) {
76 log.error("URL syntax error with url {}{}", getBaseUrl(), uri);
77 throw new RestClientException(e.getMessage());
79 log.debug("METHOD={},URL={},http={}" + method, url, httpEntity);
81 ResponseEntity responseEntity = getRestTemplate().exchange(url, method, httpEntity, String.class);
82 log.debug("RESPONSE={}", responseEntity);
83 return responseEntity;
87 * Execute the given http method against the uri with passed headers
88 * @param uri properly encoded, can include query params also properly encoded
89 * @param method http method of the request
90 * @param headers headers for the request
91 * @param body body of the request
92 * @return response of request
93 * @throws RestClientException on internal rest template exception or invalid url
95 public ResponseEntity execute(String uri, String method, Map<String,String> headers, String body) throws RestClientException{
96 return execute(uri, HttpMethod.valueOf(method), headers, body);
100 * Execute the given http method against the uri with passed headers
101 * @param uri properly encoded, can include query params also properly encoded
102 * @param method http method of the request
103 * @param headers headers for the request
104 * @return response of request
105 * @throws RestClientException on internal rest template exception or invalid url
107 public ResponseEntity execute(String uri, HttpMethod method, Map<String,String> headers) throws RestClientException{
108 return execute(uri, method, headers, null);
112 * Execute the given http method against the uri with passed headers
113 * @param uri properly encoded, can include query params also properly encoded
114 * @param method http method of the request
115 * @param headers headers for the request
116 * @return response of request
117 * @throws RestClientException on internal rest template exception or invalid url
119 public ResponseEntity execute(String uri, String method, Map<String,String> headers) throws RestClientException{
120 return execute(uri, HttpMethod.valueOf(method), headers, null);
123 public ResponseEntity executeResource(String uri, HttpMethod method, Map<String, String> headers, String body) throws RestClientException {
125 HttpEntity httpEntity;
126 log.debug("Headers: " + headers.toString());
128 httpEntity = new HttpEntity(getHeaders(headers));
130 httpEntity = new HttpEntity(body, getHeaders(headers));
132 String url = getBaseUrl() + uri;
133 return getRestTemplate().exchange(url, method, httpEntity, Resource.class);
136 public ResponseEntity getGetRequest(String content, String uri, Map<String, String> headersMap) {
145 public ResponseEntity getGetResource(String content, String uri, Map<String, String> headersMap) {
146 return this.executeResource(
154 public abstract RestTemplate getRestTemplate();
156 public abstract String getBaseUrl();
158 protected abstract MultiValueMap<String, String> getHeaders(Map<String, String> headers);