2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sparky.dal.rest;
23 import org.onap.aai.restclient.client.RestClient;
24 import org.onap.aai.sparky.config.SparkyResourceLoader;
25 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
26 import org.onap.aai.sparky.util.Encryptor;
28 public class RestClientFactory {
30 public static RestClient buildClient(RestEndpointConfig restEndpointConfig)
31 throws RestClientConstructionException {
33 if (restEndpointConfig == null) {
34 throw new RestClientConstructionException(
35 "Failed to build RestClient because RestEndpointConfig is null.");
38 if (restEndpointConfig.getRestAuthenticationMode() == null) {
39 throw new RestClientConstructionException(
40 "Failed to build RestClient because RestAuthenticationMode is null.");
43 SparkyResourceLoader resourceLoader = restEndpointConfig.getResourceLoader();
45 switch (restEndpointConfig.getRestAuthenticationMode()) {
49 Encryptor enc = new Encryptor();
50 String certFileNameFullPath = resourceLoader.getAbsolutePath(restEndpointConfig.getCertFileName());
51 String decryptedCertPassword = enc.decryptValue(restEndpointConfig.getCertPassword());
52 String truststoreFileNameFullPath =
53 resourceLoader.getAbsolutePath(restEndpointConfig.getTruststoreFileName());
55 return new RestClient() //
56 .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
57 .validateServerCertChain(restEndpointConfig.isValidateServerCertChain()) //
58 .validateServerHostname(restEndpointConfig.isValidateServerHostname()) //
59 .clientCertFile(certFileNameFullPath) //
60 .clientCertPassword(decryptedCertPassword) //
61 .trustStore(truststoreFileNameFullPath) //
62 .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
63 .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
68 return new RestClient() //
69 .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
70 .basicAuthUsername(restEndpointConfig.getBasicAuthUserName()) //
71 .basicAuthPassword(restEndpointConfig.getBasicAuthPassword()) //
72 .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
73 .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
81 return new RestClient() //
82 .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
83 .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
84 .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());