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.montoring.rest.service;
22 import org.onap.so.montoring.exception.InvalidRestRequestException;
23 import org.onap.so.montoring.exception.RestProcessingException;
24 import org.slf4j.ext.XLogger;
25 import org.slf4j.ext.XLoggerFactory;
26 import org.springframework.http.HttpEntity;
27 import org.springframework.http.HttpMethod;
28 import org.springframework.http.HttpStatus;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.web.client.HttpClientErrorException;
31 import org.springframework.web.client.RestClientException;
32 import org.springframework.web.client.RestTemplate;
34 import com.google.common.base.Optional;
37 * @author waqas.ikram@ericsson.com
39 public class HttpRestServiceProviderImpl implements HttpRestServiceProvider {
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(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 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: {}", url,
56 response.getStatusCode());
57 return Optional.absent();
60 if (response.hasBody()) {
61 return Optional.of(response.getBody());
63 } catch (final HttpClientErrorException httpClientErrorException) {
64 LOGGER.error("Unable to invoke HTTP GET using url: {}, Response: {}", url,
65 httpClientErrorException.getRawStatusCode(), httpClientErrorException);
66 final int rawStatusCode = httpClientErrorException.getRawStatusCode();
67 if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
68 throw new InvalidRestRequestException("No result found for given url: " + url);
70 throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url);
72 } catch (final RestClientException restClientException) {
73 LOGGER.error("Unable to invoke HTTP GET using url: {}", url, restClientException);
74 throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url, restClientException);
77 return Optional.absent();
81 public <T> Optional<T> postHttpRequest(final Object object, final String url, final Class<T> clazz) {
83 final HttpEntity<?> request = new HttpEntity<>(object);
84 final ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.POST, request, clazz);
85 if (!response.getStatusCode().equals(HttpStatus.OK)) {
86 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: {}", url,
87 response.getStatusCode());
88 return Optional.absent();
91 if (response.hasBody()) {
92 return Optional.of(response.getBody());
95 } catch (final HttpClientErrorException httpClientErrorException) {
96 LOGGER.error("Unable to invoke HTTP POST using url: {}, Response: {}", url,
97 httpClientErrorException.getRawStatusCode(), httpClientErrorException);
98 final int rawStatusCode = httpClientErrorException.getRawStatusCode();
99 if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
100 throw new InvalidRestRequestException("No result found for given url: " + url);
102 throw new RestProcessingException("Unable to invoke HTTP POST using URL: " + url);
104 } catch (final RestClientException restClientException) {
105 LOGGER.error("Unable to invoke HTTP POST using url: {}", url, restClientException);
106 throw new RestProcessingException("Unable to invoke HTTP POST using URL: " + url, restClientException);
109 return Optional.absent();