2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.web;
20 import java.security.KeyManagementException;
21 import java.security.KeyStoreException;
22 import java.security.NoSuchAlgorithmException;
23 import java.util.Collections;
25 import javax.net.ssl.SSLContext;
27 import org.apache.http.conn.ssl.NoopHostnameVerifier;
28 import org.apache.http.impl.client.CloseableHttpClient;
29 import org.apache.http.impl.client.HttpClients;
30 import org.apache.http.ssl.SSLContextBuilder;
31 import org.springframework.http.HttpEntity;
32 import org.springframework.http.HttpMethod;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
35 import org.springframework.web.client.RestTemplate;
37 public class RestTemplateWrapper implements IRestTemplate {
38 private RestTemplate restTemplate;
40 public RestTemplateWrapper() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
41 SSLContext sslContext =
42 new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build();
43 CloseableHttpClient httpClient =
44 HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier())
45 .setRedirectStrategy(new PublishRedirectStrategy()).build();
47 HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
48 requestFactory.setHttpClient(httpClient);
50 restTemplate = new RestTemplate(requestFactory);
51 restTemplate.setInterceptors(Collections.singletonList(new RequestResponseLoggingInterceptor()));
56 public ResponseEntity<String> exchange(URI url, HttpMethod method, HttpEntity<byte[]> requestEntity,
57 Class<String> responseType) {
58 return restTemplate.exchange(url, method, requestEntity, responseType);