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.restadapter;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.http.HttpEntity;
26 import org.springframework.http.HttpHeaders;
27 import org.springframework.http.HttpMethod;
28 import org.springframework.http.MediaType;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.web.client.RestTemplate;
33 * This class provides the Generic Rest Adapter interface to the RestTemplate
35 * @author lathishbabu.ganesan@est.tech
39 public class RestAdapterImpl implements RestAdapter {
41 private final Logger log = LoggerFactory.getLogger(RestAdapterImpl.class);
43 private RestTemplate restTemplate;
45 public RestAdapterImpl() {
46 restTemplate = new RestTemplate();
49 private HttpEntity<?> getHttpEntity(final Object object) {
50 return new HttpEntity<>(object);
54 public <T> ResponseEntity<T> get(String uri, Class<?> clazz) {
55 HttpEntity<?> entity = getHttpEntity(null);
56 return invokeHttpRequest(uri, HttpMethod.GET, clazz, entity);
60 public <T> ResponseEntity<T> put(String uri, String body, Class<T> clazz) {
61 HttpHeaders headers = new HttpHeaders();
62 headers.setContentType(MediaType.APPLICATION_JSON);
63 HttpEntity<String> entity = new HttpEntity<String>(body, headers);
64 return invokeHttpRequest(uri, HttpMethod.PUT, clazz, entity);
68 public <T> ResponseEntity<T> delete(String uri) {
69 HttpEntity<?> entity = getHttpEntity(null);
70 return invokeHttpRequest(uri, HttpMethod.DELETE, null, entity);
73 @SuppressWarnings("unchecked")
74 private <T> ResponseEntity<T> invokeHttpRequest(String uri, HttpMethod httpMethod, Class<?> clazz,
75 HttpEntity<?> entity) {
76 return (ResponseEntity<T>) restTemplate.exchange(uri, httpMethod, entity, clazz);