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=========================================================
20 package org.onap.so.aaisimulator.service.providers;
22 import java.util.Optional;
23 import org.onap.so.aaisimulator.exception.InvalidRestRequestException;
24 import org.onap.so.aaisimulator.exception.RestProcessingException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.http.HttpEntity;
29 import org.springframework.http.HttpHeaders;
30 import org.springframework.http.HttpMethod;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.stereotype.Service;
34 import org.springframework.web.client.HttpClientErrorException;
35 import org.springframework.web.client.RestClientException;
36 import org.springframework.web.client.RestTemplate;
39 * @author Waqas Ikram (waqas.ikram@est.tech)
43 public class HttpRestServiceProviderImpl implements HttpRestServiceProvider {
44 private static final Logger LOGGER = LoggerFactory.getLogger(HttpRestServiceProviderImpl.class);
46 private final RestTemplate restTemplate;
49 public HttpRestServiceProviderImpl(final RestTemplate restTemplate) {
50 this.restTemplate = restTemplate;
54 public <T> ResponseEntity<T> invokeHttpPut(final HttpEntity<Object> httpEntity, final String url,
55 final Class<T> clazz) {
57 final HttpMethod httpMethod = HttpMethod.PUT;
58 LOGGER.trace("Will invoke HTTP {} using URL: {}", httpMethod, url);
60 return restTemplate.exchange(url, httpMethod, httpEntity, clazz);
62 } catch (final HttpClientErrorException httpClientErrorException) {
63 final String message = "Unable to invoke HTTP " + httpMethod + " using url: " + url + ", Response: "
64 + httpClientErrorException.getRawStatusCode();
65 LOGGER.error(message, 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 " + httpMethod + " using URL: " + url);
72 } catch (final RestClientException restClientException) {
73 LOGGER.error("Unable to invoke HTTP POST using url: {}", url, restClientException);
74 throw new RestProcessingException("Unable to invoke HTTP " + httpMethod + " using URL: " + url,
80 public <T> Optional<T> put(final HttpHeaders headers, final Object object, final String url, final Class<T> clazz) {
81 final HttpEntity<Object> httpEntity = new HttpEntity<Object>(object, headers);
82 final ResponseEntity<T> response = invokeHttpPut(httpEntity, url, clazz);
84 if (!response.getStatusCode().equals(HttpStatus.OK) && !response.getStatusCode().equals(HttpStatus.CREATED)
85 && !response.getStatusCode().equals(HttpStatus.ACCEPTED)) {
86 final String message = "Unable to invoke HTTP " + HttpMethod.PUT + " using URL: " + url
87 + ", Response Code: " + response.getStatusCode();
88 LOGGER.error(message);
89 return Optional.empty();
92 if (response.hasBody()) {
93 return Optional.of(response.getBody());
95 LOGGER.error("Received response without body status code: {}", response.getStatusCode());
96 return Optional.empty();