2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.dal.rest;
27 import org.onap.aai.restclient.client.RestClient;
28 import org.onap.aai.sparky.config.SparkyResourceLoader;
29 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
30 import org.onap.aai.sparky.util.Encryptor;
32 public class RestClientFactory {
34 public static RestClient buildClient(RestEndpointConfig restEndpointConfig)
35 throws RestClientConstructionException {
37 if (restEndpointConfig == null) {
38 throw new RestClientConstructionException(
39 "Failed to build RestClient because RestEndpointConfig is null.");
42 if (restEndpointConfig.getRestAuthenticationMode() == null) {
43 throw new RestClientConstructionException(
44 "Failed to build RestClient because RestAuthenticationMode is null.");
47 SparkyResourceLoader resourceLoader = restEndpointConfig.getResourceLoader();
49 switch (restEndpointConfig.getRestAuthenticationMode()) {
53 Encryptor enc = new Encryptor();
54 String certFileNameFullPath = resourceLoader.getAbsolutePath(restEndpointConfig.getCertFileName());
55 String decryptedCertPassword = enc.decryptValue(restEndpointConfig.getCertPassword());
56 String truststoreFileNameFullPath =
57 resourceLoader.getAbsolutePath(restEndpointConfig.getTruststoreFileName());
59 return new RestClient() //
60 .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
61 .validateServerCertChain(restEndpointConfig.isValidateServerCertChain()) //
62 .validateServerHostname(restEndpointConfig.isValidateServerHostname()) //
63 .clientCertFile(certFileNameFullPath) //
64 .clientCertPassword(decryptedCertPassword) //
65 .trustStore(truststoreFileNameFullPath) //
66 .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
67 .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
72 return new RestClient() //
73 .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
74 .basicAuthUsername(restEndpointConfig.getBasicAuthUserName()) //
75 .basicAuthPassword(restEndpointConfig.getBasicAuthPassword()) //
76 .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
77 .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
85 return new RestClient() //
86 .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
87 .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
88 .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());