Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-schema-abstraction / src / main / java / org / onap / aai / schemaif / json / SecureClientHttpRequestFactory.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.aai.schemaif.json;
23
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.net.HttpURLConnection;
27 import java.security.KeyStore;
28 import java.security.cert.X509Certificate;
29
30 import javax.net.ssl.HostnameVerifier;
31 import javax.net.ssl.HttpsURLConnection;
32 import javax.net.ssl.KeyManagerFactory;
33 import javax.net.ssl.SSLContext;
34 import javax.net.ssl.SSLSession;
35 import javax.net.ssl.TrustManager;
36 import javax.net.ssl.X509TrustManager;
37
38 import org.springframework.http.client.SimpleClientHttpRequestFactory;
39
40 public class SecureClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
41
42     private static final String SSL_PROTOCOL = "TLS";
43     private static final String KEYSTORE_ALGORITHM = "SunX509";
44     private static final String KEYSTORE_TYPE = "PKCS12";
45     private JsonSchemaProviderConfig config;
46
47     public SecureClientHttpRequestFactory(JsonSchemaProviderConfig config) {
48         super();
49         this.config = config;
50     }
51
52     @Override
53     protected void prepareConnection(final HttpURLConnection connection, final String httpMethod) throws IOException {
54         if (connection instanceof HttpsURLConnection) {
55             ((HttpsURLConnection) connection).setSSLSocketFactory(getSSLContext().getSocketFactory());
56             ((HttpsURLConnection) connection).setHostnameVerifier(new HostnameVerifier() {
57                 @Override
58                 public boolean verify(String str, SSLSession sslSession) {
59                     return true;
60                 }
61             });
62         } else {
63
64             throw new IOException();
65         }
66         super.prepareConnection(connection, httpMethod);
67     }
68
69     protected SSLContext getSSLContext() throws IOException {
70         try {
71             TrustManager[] trustAllCerts = null;
72
73             // We aren't validating certificates, so create a trust manager that
74             // does
75             // not validate certificate chains.
76             trustAllCerts = new TrustManager[] {new X509TrustManager() {
77                 public X509Certificate[] getAcceptedIssuers() {
78                     return null;
79                 }
80
81                 public void checkClientTrusted(X509Certificate[] certs, String authType) {
82                 }
83
84                 public void checkServerTrusted(X509Certificate[] certs, String authType) {
85                 }
86             }};
87
88             SSLContext ctx = SSLContext.getInstance(SSL_PROTOCOL);
89             KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEYSTORE_ALGORITHM);
90             KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE);
91
92             char[] pwd = null;
93             if (config.getSchemaServiceCertPwd() != null) {
94                 pwd = config.getSchemaServiceCertPwd().toCharArray();
95             }
96
97             if (config.getSchemaServiceCertFile() != null) {
98                 try (FileInputStream fin = new FileInputStream(config.getSchemaServiceCertFile())) {
99                     // Load the keystore and initialize the key manager factory.
100                     ks.load(fin, pwd);
101                     kmf.init(ks, pwd);
102
103                     ctx.init(kmf.getKeyManagers(), trustAllCerts, null);
104                 }
105             } else {
106                 ctx.init(null, trustAllCerts, null);
107             }
108
109             return ctx;
110         } catch (Exception e) {
111             throw new IOException("Problem with getting the SSL Context::" + e.getMessage(), e);
112         }
113
114     }
115
116 }