2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
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=========================================================
20 package org.onap.so.monitoring.rest.service;
22 import org.onap.so.monitoring.exception.InvalidRestRequestException;
23 import org.onap.so.monitoring.exception.RestProcessingException;
24 import org.springframework.http.HttpEntity;
25 import org.springframework.http.HttpMethod;
26 import org.springframework.http.HttpStatus;
27 import org.springframework.http.ResponseEntity;
28 import org.springframework.web.client.HttpClientErrorException;
29 import org.springframework.web.client.RestClientException;
30 import org.springframework.web.client.RestTemplate;
32 import com.google.common.base.Optional;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * @author waqas.ikram@ericsson.com
40 public class HttpRestServiceProviderImpl implements HttpRestServiceProvider {
42 private static final Logger LOGGER = LoggerFactory.getLogger(HttpRestServiceProviderImpl.class);
43 private final RestTemplate restTemplate;
45 public HttpRestServiceProviderImpl(final RestTemplate restTemplate) {
46 this.restTemplate = restTemplate;
50 public <T> Optional<T> getHttpResponse(final String url, final Class<T> clazz) {
51 LOGGER.trace("Will invoke HTTP GET using URL: " + url);
53 final ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.GET, null, clazz);
54 if (!response.getStatusCode().equals(HttpStatus.OK)) {
55 final String message = "Unable to invoke HTTP GET using URL: " + url +
56 ", Response Code: " + response.getStatusCode();
57 LOGGER.error(message);
58 return Optional.absent();
61 if (response.hasBody()) {
62 return Optional.of(response.getBody());
64 } catch (final HttpClientErrorException httpClientErrorException) {
65 final String message = "Unable to invoke HTTP GET using url: " + url + ", Response: " +
66 httpClientErrorException.getRawStatusCode();
67 LOGGER.error(message, httpClientErrorException);
68 final int rawStatusCode = httpClientErrorException.getRawStatusCode();
69 if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
70 throw new InvalidRestRequestException("No result found for given url: " + url);
72 throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url);
74 } catch (final RestClientException restClientException) {
75 LOGGER.error("Unable to invoke HTTP GET using url: " + url, restClientException);
76 throw new RestProcessingException("Unable to invoke HTTP GET using URL: " +
77 url, restClientException);
80 return Optional.absent();
84 public <T> Optional<T> postHttpRequest(final Object object, final String url, final Class<T> clazz) {
86 final HttpEntity<?> request = new HttpEntity<>(object);
87 final ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.POST, request, clazz);
88 if (!response.getStatusCode().equals(HttpStatus.OK)) {
89 final String message = "Unable to invoke HTTP GET using URL: " + url +
90 ", Response Code: " + response.getStatusCode();
91 LOGGER.error(message);
92 return Optional.absent();
95 if (response.hasBody()) {
96 return Optional.of(response.getBody());
99 } catch (final HttpClientErrorException httpClientErrorException) {
100 final String message = "Unable to invoke HTTP POST using url: " + url +
101 ", Response: " + httpClientErrorException.getRawStatusCode();
102 LOGGER.error(message, httpClientErrorException);
103 final int rawStatusCode = httpClientErrorException.getRawStatusCode();
104 if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
105 throw new InvalidRestRequestException("No result found for given url: " + url);
107 throw new RestProcessingException("Unable to invoke HTTP POST using URL: " + url);
109 } catch (final RestClientException restClientException) {
110 LOGGER.error("Unable to invoke HTTP POST using url: " + url, restClientException);
111 throw new RestProcessingException("Unable to invoke HTTP POST using URL: "
112 + url, restClientException);
115 return Optional.absent();