2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. 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.vid.aai.util;
23 import org.eclipse.jetty.util.security.Password;
24 import org.onap.portalsdk.core.util.SystemProperties;
25 import org.onap.vid.aai.exceptions.InvalidPropertyException;
26 import org.onap.vid.utils.Unchecked;
28 import java.io.UnsupportedEncodingException;
30 import java.util.Base64;
31 import java.util.Optional;
33 public class SystemPropertyHelper {
35 public Optional<String> getAAIUseClientCert(){
36 return getSystemProperty(AAIProperties.AAI_USE_CLIENT_CERT);
39 public Optional<String> getAAIServerUrl(){
40 return getSystemProperty(AAIProperties.AAI_SERVER_URL);
43 public Optional<String> getAAIServerBaseUrl(){
44 return getSystemProperty(AAIProperties.AAI_SERVER_URL_BASE);
47 public Optional<String> getAAIVIDUsername(){
48 return getSystemProperty(AAIProperties.AAI_VID_USERNAME);
51 public Optional<String> getAAIVIDPasswd(){
52 return getSystemProperty(AAIProperties.AAI_VID_PASSWD_X);
55 public Optional<String> getAAITruststorePasswd(){
56 return getSystemProperty(AAIProperties.AAI_TRUSTSTORE_PASSWD_X);
59 public Optional<String> getAAITruststoreFilename(){
60 return getSystemProperty(AAIProperties.AAI_TRUSTSTORE_FILENAME);
63 public Optional<String> getAAIKeystoreFilename(){
64 return getSystemProperty(AAIProperties.AAI_KEYSTORE_FILENAME);
67 public Optional<String> getAAIKeystorePasswd(){
68 return getSystemProperty(AAIProperties.AAI_KEYSTORE_PASSWD_X);
71 public boolean isClientCertEnabled() {
72 return getAAIUseClientCert().orElse("false").equalsIgnoreCase("true");
75 public String getFullServicePath(String path) {
76 return getAAIServerUrl().orElse("") + path;
79 public String getFullServicePath(URI requestUri) {
80 // resolve() will merge two paths, handling the restiveness:
81 // Especially if requestUri starts with a '/' -- result will be
82 // AAI_SERVER_URL host, post, etc., and the path will be just
84 return Unchecked.toURI(getAAIServerUrl().orElse(""))
85 .resolve(requestUri).toASCIIString();
88 public String getServiceBasePath(String path) {
89 return getAAIServerBaseUrl().orElse("") + path;
92 public String getEncodedCredentials() throws InvalidPropertyException, UnsupportedEncodingException {
93 String vidUsername = getAAIVIDUsername().orElseThrow(InvalidPropertyException::new);
94 String vidPassword = Password.deobfuscate(getAAIVIDPasswd().orElseThrow(InvalidPropertyException::new));
95 return Base64.getEncoder().encodeToString((vidUsername + ":" + vidPassword).getBytes("utf-8"));
98 public String getDecryptedTruststorePassword(){
99 return Password.deobfuscate(getAAITruststorePasswd().orElse(""));
102 public String getDecryptedKeystorePassword(){
103 return Password.deobfuscate(getAAIKeystorePasswd().orElse(""));
106 private Optional<String> getSystemProperty(String propertyKey){
107 return Optional.ofNullable(SystemProperties.getProperty(propertyKey));