2fe9500f62ee10f4e8e006f392c3fb64387e605d
[aai/aai-common.git] / aai-rest / src / main / java / org / onap / aai / restclient / TwoWaySSLRestClient.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright Â© 2017-2018 AT&T Intellectual Property. All rights reserved.
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.aai.restclient;
21
22 import org.apache.http.client.HttpClient;
23 import org.apache.http.impl.client.HttpClients;
24 import org.apache.http.ssl.SSLContextBuilder;
25 import org.springframework.boot.web.client.RestTemplateBuilder;
26 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
27 import org.springframework.util.ResourceUtils;
28 import org.springframework.web.client.RestTemplate;
29
30 import javax.annotation.PostConstruct;
31 import javax.net.ssl.SSLContext;
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.InputStream;
35 import java.security.KeyStore;
36
37 public abstract class TwoWaySSLRestClient extends RestClient {
38
39     private RestTemplate restTemplate;
40
41     @PostConstruct
42     public void init() throws Exception {
43
44         char[] keyStorePassword = getKeystorePassword();
45         char[] trustStorePassword = getTruststorePassword();
46
47         String keyStore = getKeystorePath();
48         String trustStore = getTruststorePath();
49
50         SSLContext sslContext = SSLContextBuilder
51             .create()
52             .loadKeyMaterial(loadPfx(keyStore, keyStorePassword), keyStorePassword)
53             .loadTrustMaterial(ResourceUtils.getFile(trustStore), trustStorePassword)
54             .build();
55
56         HttpClient client = HttpClients.custom()
57             .setSSLContext(sslContext)
58             .setSSLHostnameVerifier((s, sslSession) -> true)
59             .build();
60
61         restTemplate = new RestTemplateBuilder()
62             .requestFactory(new HttpComponentsClientHttpRequestFactory(client))
63             .build();
64
65         restTemplate.setErrorHandler(new RestClientResponseErrorHandler(getLogger()));
66
67     }
68
69     private KeyStore loadPfx(String file, char[] password) throws Exception {
70         KeyStore keyStore = KeyStore.getInstance("PKCS12");
71         File key = ResourceUtils.getFile(file);
72         try (InputStream in = new FileInputStream(key)) {
73             keyStore.load(in, password);
74         }
75         return keyStore;
76     }
77
78     protected abstract String getKeystorePath();
79
80     protected abstract String getTruststorePath();
81
82     protected abstract char[] getTruststorePassword();
83
84     protected abstract char[] getKeystorePassword();
85
86     @Override
87     public RestTemplate getRestTemplate() {
88         return restTemplate;
89     }
90
91 }