99ead8468d4aa3c79aefb4f66e2678216cb5329b
[dcaegen2/collectors/datafile.git] /
1 /*
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.web;
18
19 import java.net.URI;
20 import java.security.KeyManagementException;
21 import java.security.KeyStoreException;
22 import java.security.NoSuchAlgorithmException;
23 import java.util.Collections;
24
25 import javax.net.ssl.SSLContext;
26
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;
36
37 public class RestTemplateWrapper implements IRestTemplate {
38     private RestTemplate restTemplate;
39
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();
46
47         HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
48         requestFactory.setHttpClient(httpClient);
49
50         restTemplate = new RestTemplate(requestFactory);
51         restTemplate.setInterceptors(Collections.singletonList(new RequestResponseLoggingInterceptor()));
52
53     }
54
55     @Override
56     public ResponseEntity<String> exchange(URI url, HttpMethod method, HttpEntity<byte[]> requestEntity,
57             Class<String> responseType) {
58         return restTemplate.exchange(url,  method, requestEntity, responseType);
59     }
60
61 }