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.onap.so.client;
23 import java.io.FileInputStream;
25 import java.security.KeyStore;
26 import java.security.NoSuchAlgorithmException;
27 import java.util.Optional;
29 import javax.net.ssl.SSLContext;
30 import javax.ws.rs.client.Client;
31 import javax.ws.rs.client.ClientBuilder;
33 public abstract class RestClientSSL extends RestClient {
35 private static final String TRUE = "true";
36 public static final String SSL_KEY_STORE_KEY = "javax.net.ssl.keyStore";
37 public static final String SSL_KEY_STORE_PASSWORD_KEY = "javax.net.ssl.keyStorePassword";
38 public static final String MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY = "mso.load.ssl.client.keystore";
41 protected RestClientSSL(RestProperties props, Optional<URI> path) {
45 protected RestClientSSL(RestProperties props, Optional<URI> path, String accept, String contentType) {
46 super(props, path, accept, contentType);
50 protected Client getClient() {
54 String loadSSLKeyStore = System.getProperty(RestClientSSL.MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY);
55 if(loadSSLKeyStore != null && loadSSLKeyStore.equalsIgnoreCase(TRUE)) {
56 KeyStore ks = getKeyStore();
58 client = ClientBuilder.newBuilder().keyStore(ks, System.getProperty(RestClientSSL.SSL_KEY_STORE_PASSWORD_KEY)).build();
59 logger.debug("RestClientSSL not using default SSL context - setting keystore here.");
63 //Use default SSL context
64 client = ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()).build();
65 logger.info("RestClientSSL using default SSL context!");
66 } catch (NoSuchAlgorithmException e) {
67 throw new RuntimeException(e);
72 private KeyStore getKeyStore() {
74 char[] password = System.getProperty(RestClientSSL.SSL_KEY_STORE_PASSWORD_KEY).toCharArray();
75 try(FileInputStream fis = new FileInputStream(System.getProperty(RestClientSSL.SSL_KEY_STORE_KEY))) {
76 ks = KeyStore.getInstance(KeyStore.getDefaultType());
78 ks.load(fis, password);