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.sdnc.northbound.restadpter;
23 import com.google.common.base.Optional;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.http.HttpEntity;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.http.HttpMethod;
29 import org.springframework.http.HttpStatus;
30 import org.springframework.http.MediaType;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.web.client.RestTemplate;
35 * This class provides the Generic Rest Adapter interface to the RestTemplate
37 * @author lathishbabu.ganesan@est.tech
41 public class RestAdapterImpl implements RestAdapter {
43 private final Logger log = LoggerFactory.getLogger(RestAdapterImpl.class);
45 private RestTemplate restTemplate;
47 public RestAdapterImpl() {
48 restTemplate = new RestTemplate();
51 private HttpEntity<?> getHttpEntity(final Object object) {
52 return new HttpEntity<>(object);
56 public <T> Optional<T> get(String uri, Class<?> clazz) {
57 HttpEntity<?> entity = getHttpEntity(null);
58 final ResponseEntity<T> response = invokeHttpRequest(uri, HttpMethod.GET, clazz, entity);
59 return buildOptional(response);
63 public <T> Optional<T> put(String uri, String body) {
64 HttpHeaders headers = new HttpHeaders();
65 headers.setContentType(MediaType.APPLICATION_JSON);
66 HttpEntity<String> entity = new HttpEntity<String>(body, headers);
67 final ResponseEntity<T> response = invokeHttpRequest(uri, HttpMethod.PUT, null, entity);
68 return buildOptional(response);
72 public <T> Optional<T> delete(String uri) {
73 HttpEntity<?> entity = getHttpEntity(null);
74 final ResponseEntity<T> response = invokeHttpRequest(uri, HttpMethod.DELETE, null, entity);
75 return buildOptional(response);
78 @SuppressWarnings("unchecked")
79 private <T> ResponseEntity<T> invokeHttpRequest(String uri, HttpMethod httpMethod, Class<?> clazz,
80 HttpEntity<?> entity) {
81 return (ResponseEntity<T>) restTemplate.exchange(uri, httpMethod, entity, clazz);
84 private <T> Optional<T> buildOptional(ResponseEntity<T> response) {
85 if (!response.getStatusCode().equals(HttpStatus.OK)
86 & !response.getStatusCode().equals(HttpStatus.CREATED)
87 & !response.getStatusCode().equals(HttpStatus.NO_CONTENT)) {
88 log.error("Failed to get the Response, Status Code = {}", response.getStatusCode());
89 return Optional.absent();
91 if (response.hasBody()) {
92 return Optional.of(response.getBody());
94 return Optional.absent();