2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.dal.rest;
25 import org.onap.aai.restclient.client.RestClient;
26 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
27 import org.onap.aai.sparky.util.Encryptor;
28 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
30 public class RestClientFactory {
32 public static RestClient buildClient(RestEndpointConfig restEndpointConfig)
33 throws RestClientConstructionException {
35 if (restEndpointConfig == null) {
36 throw new RestClientConstructionException(
37 "Failed to build RestClient because RestEndpointConfig is null.");
40 if (restEndpointConfig.getRestAuthenticationMode() == null) {
41 throw new RestClientConstructionException(
42 "Failed to build RestClient because RestAuthenticationMode is null.");
45 switch (restEndpointConfig.getRestAuthenticationMode()) {
49 Encryptor enc = new Encryptor();
50 String certFileNameFullPath =
51 SparkyConstants.CONFIG_AUTH_LOCATION + restEndpointConfig.getCertFileName();
52 String decryptedCertPassword = enc.decryptValue(restEndpointConfig.getCertPassword());
53 String truststoreFileNameFullPath =
54 SparkyConstants.CONFIG_AUTH_LOCATION + restEndpointConfig.getTruststoreFileName();
56 return new RestClient() //
57 .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
58 .validateServerCertChain(restEndpointConfig.isValidateServerCertChain()) //
59 .validateServerHostname(restEndpointConfig.isValidateServerHostname()) //
60 .clientCertFile(certFileNameFullPath) //
61 .clientCertPassword(decryptedCertPassword) //
62 .trustStore(truststoreFileNameFullPath) //
63 .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
64 .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
69 return new RestClient() //
70 .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
71 .basicAuthUsername(restEndpointConfig.getBasicAuthUserName()) //
72 .basicAuthPassword(restEndpointConfig.getBasicAuthPassword()) //
73 .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
74 .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
82 return new RestClient() //
83 .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
84 .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
85 .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());