Merge "Add SO Catalog DB tables related to WFD"
[so.git] / common / src / main / java / org / onap / so / rest / service / HttpRestServiceProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.rest.service;
22
23 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
24 import org.onap.so.configuration.rest.HttpHeadersProvider;
25 import org.onap.so.rest.exceptions.InvalidRestRequestException;
26 import org.onap.so.rest.exceptions.RestProcessingException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.http.HttpEntity;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.HttpMethod;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.web.client.HttpClientErrorException;
35 import org.springframework.web.client.RestClientException;
36 import org.springframework.web.client.RestTemplate;
37
38 import com.google.common.base.Optional;
39
40 /**
41  * A Service to perform HTTP requests
42  * 
43  * @author waqas.ikram@est.tech
44  */
45 public class HttpRestServiceProviderImpl implements HttpRestServiceProvider {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(HttpRestServiceProviderImpl.class);
48     private final RestTemplate restTemplate;
49     private final HttpHeadersProvider httpHeadersProvider;
50
51     public HttpRestServiceProviderImpl(final RestTemplate restTemplate) {
52         this.restTemplate = restTemplate;
53         this.httpHeadersProvider = new BasicHttpHeadersProvider();
54     }
55
56     public HttpRestServiceProviderImpl(final RestTemplate restTemplate, final HttpHeadersProvider httpHeadersProvider) {
57         this.restTemplate = restTemplate;
58         this.httpHeadersProvider = httpHeadersProvider;
59     }
60
61     @Override
62     public <T> Optional<T> get(final String url, final Class<T> clazz) {
63         final ResponseEntity<T> response = getHttpResponse(url, clazz);
64         if (!response.getStatusCode().equals(HttpStatus.OK)) {
65             final String message =
66                     "Unable to invoke HTTP GET using URL: " + url + ", Response Code: " + response.getStatusCode();
67             LOGGER.error(message);
68             return Optional.absent();
69         }
70
71         if (response.hasBody()) {
72             return Optional.of(response.getBody());
73         }
74         return Optional.absent();
75     }
76
77
78     @Override
79     public <T> ResponseEntity<T> getHttpResponse(final String url, final Class<T> clazz) {
80         LOGGER.trace("Will invoke HTTP GET using URL: {}", url);
81         try {
82             final HttpEntity<?> request = new HttpEntity<>(getHttpHeaders());
83             return restTemplate.exchange(url, HttpMethod.GET, request, clazz);
84
85         } catch (final HttpClientErrorException httpClientErrorException) {
86             final String message = "Unable to invoke HTTP GET using url: " + url + ", Response: "
87                     + httpClientErrorException.getRawStatusCode();
88             LOGGER.error(message, httpClientErrorException);
89             final int rawStatusCode = httpClientErrorException.getRawStatusCode();
90             if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
91                 throw new InvalidRestRequestException("No result found for given url: " + url);
92             }
93             throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url);
94
95         } catch (final RestClientException restClientException) {
96             LOGGER.error("Unable to invoke HTTP GET using url: {}", url, restClientException);
97             throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url, restClientException);
98         }
99     }
100
101     @Override
102     public <T> Optional<T> post(final Object object, final String url, final Class<T> clazz) {
103         final ResponseEntity<T> response = postHttpRequest(object, url, clazz);
104         if (!response.getStatusCode().equals(HttpStatus.OK)) {
105             final String message =
106                     "Unable to invoke HTTP GET using URL: " + url + ", Response Code: " + response.getStatusCode();
107             LOGGER.error(message);
108             return Optional.absent();
109         }
110
111         if (response.hasBody()) {
112             return Optional.of(response.getBody());
113         }
114
115         return Optional.absent();
116     }
117
118
119     @Override
120     public <T> ResponseEntity<T> postHttpRequest(final Object object, final String url, final Class<T> clazz) {
121         try {
122             final HttpEntity<?> request = new HttpEntity<>(object, getHttpHeaders());
123             return restTemplate.exchange(url, HttpMethod.POST, request, clazz);
124
125         } catch (final HttpClientErrorException httpClientErrorException) {
126             final String message = "Unable to invoke HTTP POST using url: " + url + ", Response: "
127                     + httpClientErrorException.getRawStatusCode();
128             LOGGER.error(message, httpClientErrorException);
129             final int rawStatusCode = httpClientErrorException.getRawStatusCode();
130             if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
131                 throw new InvalidRestRequestException("No result found for given url: " + url);
132             }
133             throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url);
134
135         } catch (final RestClientException restClientException) {
136             LOGGER.error("Unable to invoke HTTP POST using url: {}", url, restClientException);
137             throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url, restClientException);
138         }
139     }
140
141     private HttpHeaders getHttpHeaders() {
142         return httpHeadersProvider.getHttpHeaders();
143     }
144 }