2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.client.policy;
23 import java.io.FileInputStream;
24 import java.io.IOException;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.KeyStore;
28 import java.util.Optional;
29 import java.util.Properties;
30 import java.util.UUID;
32 import javax.net.ssl.SSLContext;
33 import javax.ws.rs.client.Client;
34 import javax.ws.rs.client.ClientBuilder;
36 import org.openecomp.mso.client.RestProperties;
37 import org.openecomp.mso.logger.MessageEnum;
38 import org.openecomp.mso.logger.MsoLogger;
40 public abstract class RestClientSSL extends RestClient {
42 public static final String SSL_KEY_STORE_KEY = "javax.net.ssl.keyStore";
43 public static String SSL_KEY_STORE_PASSWORD_KEY;
44 public static final String MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY = "mso.load.ssl.client.keystore";
47 protected RestClientSSL(RestProperties props, UUID requestId, Optional<URI> path) {
48 super(props, requestId, path);
51 protected RestClientSSL(RestProperties props, UUID requestId, Optional<URI> path, String accept, String contentType) {
52 super(props, requestId, path, accept, contentType);
56 protected Client getClient() {
58 Properties keyProp = new Properties ();
60 keyProp.load (Thread.currentThread ().getContextClassLoader ().getResourceAsStream ("Policy.properties"));
61 SSL_KEY_STORE_PASSWORD_KEY=(String) keyProp.get ("ssl.key.store.password.key");
62 String loadSSLKeyStore = System.getProperty(RestClientSSL.MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY);
63 if(loadSSLKeyStore != null && loadSSLKeyStore.equalsIgnoreCase("true")) {
64 KeyStore ks = getKeyStore();
66 client = ClientBuilder.newBuilder().keyStore(ks, System.getProperty(RestClientSSL.SSL_KEY_STORE_PASSWORD_KEY)).build();
67 this.msoLogger.debug("RestClientSSL not using default SSL context - setting keystore here.");
71 //Use default SSL context
72 client = ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()).build();
73 this.msoLogger.debug("RestClientSSL using default SSL context!");
74 } catch (NoSuchAlgorithmException | IOException e) {
75 this.msoLogger.error(MessageEnum.APIH_GENERAL_EXCEPTION, "AAI", "Client init", MsoLogger.ErrorCode.UnknownError, "could not create SSL client", e);
76 throw new RuntimeException(e);
81 private KeyStore getKeyStore() throws IOException {
83 Properties keyProp = new Properties ();
85 keyProp.load (Thread.currentThread ().getContextClassLoader ().getResourceAsStream ("Policy.properties"));
86 SSL_KEY_STORE_PASSWORD_KEY=(String) keyProp.get ("ssl.key.store.password.key");
87 char[] password = System.getProperty(RestClientSSL.SSL_KEY_STORE_PASSWORD_KEY).toCharArray();
88 FileInputStream fis = null;
91 ks = KeyStore.getInstance(KeyStore.getDefaultType());
92 fis = new FileInputStream(System.getProperty(RestClientSSL.SSL_KEY_STORE_KEY));
93 ks.load(fis, password);
104 catch(Exception e) {}