a1b428481bda7149c0e869208b1fa0bfbc6c46b8
[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 /**
38  * @author
39  *
40  */
41 public class RestTemplateWrapper implements IRestTemplate {
42     private RestTemplate restTemplate;
43
44     public RestTemplateWrapper() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
45         SSLContext sslContext =
46                 new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build();
47         CloseableHttpClient httpClient =
48                 HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier())
49                         .setRedirectStrategy(new PublishRedirectStrategy()).build();
50
51         HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
52         requestFactory.setHttpClient(httpClient);
53
54         restTemplate = new RestTemplate(requestFactory);
55         restTemplate.setInterceptors(Collections.singletonList(new RequestResponseLoggingInterceptor()));
56
57     }
58
59     @Override
60     public ResponseEntity<String> exchange(URI url, HttpMethod method, HttpEntity<byte[]> requestEntity,
61             Class<String> responseType) {
62         return restTemplate.exchange(url,  method, requestEntity, responseType);
63     }
64
65 }