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.o_ran_sc.nonrtric.sdnc_a1.northbound.restadapter;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.MalformedURLException;
28 import java.security.KeyManagementException;
29 import java.security.KeyStoreException;
30 import java.security.NoSuchAlgorithmException;
31 import java.security.UnrecoverableKeyException;
32 import java.security.cert.CertificateException;
33 import java.util.Properties;
34 import org.apache.http.client.HttpClient;
35 import org.apache.http.conn.ssl.NoopHostnameVerifier;
36 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
37 import org.apache.http.conn.ssl.TrustAllStrategy;
38 import org.apache.http.impl.client.HttpClients;
39 import org.apache.http.ssl.SSLContextBuilder;
40 import org.apache.http.ssl.SSLContexts;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.http.HttpEntity;
44 import org.springframework.http.HttpHeaders;
45 import org.springframework.http.HttpMethod;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.MediaType;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
50 import org.springframework.util.ResourceUtils;
51 import org.springframework.web.client.RestTemplate;
54 * This class provides the Generic Rest Adapter interface to the RestTemplate
56 * @author lathishbabu.ganesan@est.tech
60 public class RestAdapterImpl implements RestAdapter {
62 private final Logger log = LoggerFactory.getLogger(RestAdapterImpl.class);
64 private RestTemplate restTemplateHttp;
65 private RestTemplate restTemplateHttps;
67 public RestAdapterImpl() {
68 restTemplateHttp = new RestTemplate();
70 restTemplateHttps = createRestTemplateForHttps();
71 } catch (IOException | UnrecoverableKeyException | KeyManagementException | CertificateException
72 | NoSuchAlgorithmException | KeyStoreException ex) {
73 log.error("Caught exception when trying to create restTemplateHttps: {}", ex.getMessage());
77 private RestTemplate createRestTemplateForHttps() throws IOException, UnrecoverableKeyException, CertificateException,
78 NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
79 try (InputStream inputStream = new FileInputStream(ResourceUtils.getFile("/opt/onap/sdnc/data/properties/https-props.properties"))) {
80 Properties properties = new Properties();
81 properties.load(inputStream);
82 final String keyPassword = properties.getProperty("key-password");
83 final String keystorePassword = properties.getProperty("keystore-password");
84 final String truststorePassword = properties.getProperty("truststore-password");
85 final boolean isTrustStoreUsed = Boolean.parseBoolean(properties.getProperty("isTrustStoreUsed"));
86 SSLContextBuilder builder = SSLContexts.custom()
87 .loadKeyMaterial(ResourceUtils.getFile(properties.getProperty("key-store")),
88 keystorePassword.toCharArray(), keyPassword.toCharArray());
89 if (isTrustStoreUsed) {
90 builder.loadTrustMaterial(ResourceUtils.getFile(properties.getProperty("trust-store")),
91 truststorePassword.toCharArray());
93 builder.loadTrustMaterial(null, new TrustAllStrategy());
95 SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
96 HttpClient client = HttpClients.custom().setSSLSocketFactory(scsf).build();
97 HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
98 requestFactory.setHttpClient(client);
99 return new RestTemplate(requestFactory);
103 private HttpEntity<?> getHttpEntity(final Object object) {
104 return new HttpEntity<>(object);
108 public <T> ResponseEntity<T> get(String uri, Class<?> clazz) {
109 HttpEntity<?> entity = getHttpEntity(null);
110 return invokeHttpRequest(uri, HttpMethod.GET, clazz, entity);
114 public <T> ResponseEntity<T> put(String uri, String body, Class<T> clazz) {
115 HttpHeaders headers = new HttpHeaders();
116 headers.setContentType(MediaType.APPLICATION_JSON);
117 HttpEntity<String> entity = new HttpEntity<>(body, headers);
118 return invokeHttpRequest(uri, HttpMethod.PUT, clazz, entity);
122 public <T> ResponseEntity<T> delete(String uri) {
123 HttpEntity<?> entity = getHttpEntity(null);
124 return invokeHttpRequest(uri, HttpMethod.DELETE, null, entity);
127 @SuppressWarnings("unchecked")
128 private <T> ResponseEntity<T> invokeHttpRequest(String uri, HttpMethod httpMethod, Class<?> clazz,
129 HttpEntity<?> entity) {
131 URL url = new URL(uri);
132 if (url.getProtocol().equals("https")) {
133 return (ResponseEntity<T>) restTemplateHttps.exchange(uri, httpMethod, entity, clazz);
134 } else if (url.getProtocol().equals("http")) {
135 return (ResponseEntity<T>) restTemplateHttp.exchange(uri, httpMethod, entity, clazz);
137 log.error("Invalid protocol in URL");
138 return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
140 } catch (MalformedURLException ex) {
141 log.error("URL is not valid, exception: {}", ex.getMessage());
142 return new ResponseEntity<>(HttpStatus.BAD_REQUEST);