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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.rest.service;
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;
38 import com.google.common.base.Optional;
41 * A Service to perform HTTP requests
43 * @author waqas.ikram@est.tech
45 public class HttpRestServiceProviderImpl implements HttpRestServiceProvider {
47 private static final Logger LOGGER = LoggerFactory.getLogger(HttpRestServiceProviderImpl.class);
48 private final RestTemplate restTemplate;
49 private final HttpHeadersProvider httpHeadersProvider;
51 public HttpRestServiceProviderImpl(final RestTemplate restTemplate) {
52 this.restTemplate = restTemplate;
53 this.httpHeadersProvider = new BasicHttpHeadersProvider();
56 public HttpRestServiceProviderImpl(final RestTemplate restTemplate, final HttpHeadersProvider httpHeadersProvider) {
57 this.restTemplate = restTemplate;
58 this.httpHeadersProvider = httpHeadersProvider;
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();
71 if (response.hasBody()) {
72 return Optional.of(response.getBody());
74 return Optional.absent();
79 public <T> ResponseEntity<T> getHttpResponse(final String url, final Class<T> clazz) {
80 LOGGER.trace("Will invoke HTTP GET using URL: {}", url);
82 final HttpEntity<?> request = new HttpEntity<>(getHttpHeaders());
83 return restTemplate.exchange(url, HttpMethod.GET, request, clazz);
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);
93 throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url);
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);
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();
111 if (response.hasBody()) {
112 return Optional.of(response.getBody());
115 return Optional.absent();
120 public <T> ResponseEntity<T> postHttpRequest(final Object object, final String url, final Class<T> clazz) {
122 final HttpEntity<?> request = new HttpEntity<>(object, getHttpHeaders());
123 return restTemplate.exchange(url, HttpMethod.POST, request, clazz);
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);
133 throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url);
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);
141 private HttpHeaders getHttpHeaders() {
142 return httpHeadersProvider.getHttpHeaders();