AAI-1523 Batch reformat aai-rest
[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
23 package org.onap.aai.restclient;
24
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.util.Map;
31
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;
40
41 public abstract class RestClient {
42
43     private static EELFLogger log = EELFManager.getInstance().getLogger(RestClient.class);
44     @Value("${spring.application.name}")
45     protected String appName;
46
47     /**
48      * Execute the given http method against the uri with passed headers
49      * 
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
56      */
57     public ResponseEntity execute(String uri, HttpMethod method, Map<String, String> headers, String body)
58             throws RestClientException {
59
60         HttpEntity<String> httpEntity;
61         log.debug("Headers: {}", headers);
62         if (body == null) {
63             httpEntity = new HttpEntity<>(getHeaders(headers));
64         } else {
65             httpEntity = new HttpEntity<>(body, getHeaders(headers));
66         }
67
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("/")) {
72             uri = "/" + uri;
73         }
74
75         URI url;
76         try {
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());
81         }
82         log.debug("METHOD={},URL={},http={}" + method, url, httpEntity);
83
84         ResponseEntity responseEntity = getRestTemplate().exchange(url, method, httpEntity, String.class);
85         log.debug("RESPONSE={}", responseEntity);
86         return responseEntity;
87     }
88
89     /**
90      * Execute the given http method against the uri with passed headers
91      * 
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
98      */
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);
102     }
103
104     /**
105      * Execute the given http method against the uri with passed headers
106      * 
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
112      */
113     public ResponseEntity execute(String uri, HttpMethod method, Map<String, String> headers)
114             throws RestClientException {
115         return execute(uri, method, headers, null);
116     }
117
118     /**
119      * Execute the given http method against the uri with passed headers
120      * 
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
126      */
127     public ResponseEntity execute(String uri, String method, Map<String, String> headers) throws RestClientException {
128         return execute(uri, HttpMethod.valueOf(method), headers, null);
129     }
130
131     public ResponseEntity executeResource(String uri, HttpMethod method, Map<String, String> headers, String body)
132             throws RestClientException {
133
134         HttpEntity httpEntity;
135         log.debug("Headers: " + headers.toString());
136         if (body == null) {
137             httpEntity = new HttpEntity(getHeaders(headers));
138         } else {
139             httpEntity = new HttpEntity(body, getHeaders(headers));
140         }
141         String url = getBaseUrl() + uri;
142         return getRestTemplate().exchange(url, method, httpEntity, Resource.class);
143     }
144
145     public ResponseEntity getGetRequest(String content, String uri, Map<String, String> headersMap) {
146         return this.execute(uri, HttpMethod.GET, headersMap, content);
147
148     }
149
150     public ResponseEntity getGetResource(String content, String uri, Map<String, String> headersMap) {
151         return this.executeResource(uri, HttpMethod.GET, headersMap, content);
152
153     }
154
155     public abstract RestTemplate getRestTemplate();
156
157     public abstract String getBaseUrl();
158
159     protected abstract MultiValueMap<String, String> getHeaders(Map<String, String> headers);
160
161 }