Add forward proxy code
[aaf/cadi.git] / sidecar / fproxy / src / main / java / org / onap / aaf / fproxy / RestTemplateConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aaf
4  * ================================================================================
5  * Copyright © 2018 European Software Marketing Ltd.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aaf.fproxy;
21
22 import java.io.IOException;
23 import java.security.GeneralSecurityException;
24 import javax.net.ssl.SSLContext;
25 import org.apache.http.conn.ssl.NoopHostnameVerifier;
26 import org.apache.http.impl.client.HttpClientBuilder;
27 import org.apache.http.impl.client.HttpClients;
28 import org.apache.http.ssl.SSLContextBuilder;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.boot.web.client.RestTemplateBuilder;
31 import org.springframework.context.annotation.Bean;
32 import org.springframework.context.annotation.Configuration;
33 import org.springframework.context.annotation.Profile;
34 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
35 import org.springframework.util.ResourceUtils;
36 import org.springframework.web.client.RestTemplate;
37
38 @Configuration
39 public class RestTemplateConfig {
40
41     @Value("${server.ssl.client-cert}")
42     private String clientCertPath;
43
44     @Value("${server.ssl.key-store-password}")
45     private String clientCertPassword;
46
47     @Profile("secure")
48     @Bean
49     public RestTemplate restTemplate(RestTemplateBuilder builder) throws GeneralSecurityException, IOException {
50         return new RestTemplate(new HttpComponentsClientHttpRequestFactory(getClientBuilder().build()));
51     }
52
53     @Profile("noHostVerification")
54     @Bean
55     public RestTemplate restTemplateNoHostVerification(RestTemplateBuilder builder)
56             throws GeneralSecurityException, IOException {
57         return new RestTemplate(new HttpComponentsClientHttpRequestFactory(
58                 getClientBuilder().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build()));
59     }
60
61     private HttpClientBuilder getClientBuilder() throws GeneralSecurityException, IOException {
62
63         SSLContext sslContext = SSLContextBuilder.create()
64                 .loadKeyMaterial(ResourceUtils.getFile(clientCertPath), clientCertPassword.toCharArray(),
65                         clientCertPassword.toCharArray())
66                 .loadTrustMaterial(ResourceUtils.getFile(clientCertPath), clientCertPassword.toCharArray()).build();
67
68         return HttpClients.custom().setSSLContext(sslContext);
69     }
70 }