Update schema service to fail to start
[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 package org.onap.aai.restclient;
23
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;
34
35 import java.net.URI;
36 import java.net.URISyntaxException;
37 import java.util.Map;
38
39 public abstract class RestClient {
40
41     private static EELFLogger log = EELFManager.getInstance().getLogger(RestClient.class);
42     @Value("${spring.application.name}")
43     protected String appName;
44
45
46     /**
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
54      */
55     public ResponseEntity execute(String uri, HttpMethod method, Map<String,String> headers, String body) throws RestClientException {
56
57         HttpEntity<String> httpEntity;
58         log.debug ("Headers: {}", headers);
59         if (body == null) {
60             httpEntity = new HttpEntity<>(getHeaders(headers));
61         } else {
62             httpEntity = new HttpEntity<>(body, getHeaders(headers));
63         }
64
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("/")) {
69             uri = "/" + uri;
70         }
71
72         URI url;
73         try {
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());
78         }
79         log.debug("METHOD={},URL={},http={}" + method, url, httpEntity);
80
81         ResponseEntity responseEntity = getRestTemplate().exchange(url, method, httpEntity, String.class);
82         log.debug("RESPONSE={}", responseEntity);
83         return responseEntity;
84     }
85
86     /**
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
94      */
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);
97     }
98
99     /**
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
106      */
107     public ResponseEntity execute(String uri, HttpMethod method, Map<String,String> headers) throws RestClientException{
108         return execute(uri, method, headers, null);
109     }
110
111     /**
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
118      */
119     public ResponseEntity execute(String uri, String method, Map<String,String> headers) throws RestClientException{
120         return execute(uri, HttpMethod.valueOf(method), headers, null);
121     }
122
123     public ResponseEntity executeResource(String uri, HttpMethod method, Map<String, String> headers, String body) throws RestClientException {
124
125         HttpEntity httpEntity;
126         log.debug("Headers: " + headers.toString());
127         if (body == null) {
128             httpEntity = new HttpEntity(getHeaders(headers));
129         } else {
130             httpEntity = new HttpEntity(body, getHeaders(headers));
131         }
132         String url = getBaseUrl() + uri;
133         return getRestTemplate().exchange(url, method, httpEntity, Resource.class);
134     }
135
136     public ResponseEntity getGetRequest(String content, String uri, Map<String, String> headersMap) {
137         return this.execute(
138             uri,
139             HttpMethod.GET,
140             headersMap,
141             content);
142
143     }
144
145     public ResponseEntity getGetResource(String content, String uri, Map<String, String> headersMap) {
146         return this.executeResource(
147             uri,
148             HttpMethod.GET,
149             headersMap,
150             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 }