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.dal.rest.config.RestEndpointConfig;
25 import org.onap.aai.sparky.util.Encryptor;
26 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
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 switch (restEndpointConfig.getRestAuthenticationMode()) {
47 Encryptor enc = new Encryptor();
48 String certFileNameFullPath =
49 SparkyConstants.CONFIG_AUTH_LOCATION + restEndpointConfig.getCertFileName();
50 String decryptedCertPassword = enc.decryptValue(restEndpointConfig.getCertPassword());
51 String truststoreFileNameFullPath =
52 SparkyConstants.CONFIG_AUTH_LOCATION + restEndpointConfig.getTruststoreFileName();
54 return new RestClient() //
55 .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
56 .validateServerCertChain(restEndpointConfig.isValidateServerCertChain()) //
57 .validateServerHostname(restEndpointConfig.isValidateServerHostname()) //
58 .clientCertFile(certFileNameFullPath) //
59 .clientCertPassword(decryptedCertPassword) //
60 .trustStore(truststoreFileNameFullPath) //
61 .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
62 .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
67 return new RestClient() //
68 .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
69 .basicAuthUsername(restEndpointConfig.getBasicAuthUserName()) //
70 .basicAuthPassword(restEndpointConfig.getBasicAuthPassword()) //
71 .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
72 .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
80 return new RestClient() //
81 .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
82 .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
83 .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());