Adding interfaces in documentation
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / dal / rest / RestClientFactory.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.dal.rest;
26
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;
31
32 public class RestClientFactory {
33
34   public static RestClient buildClient(RestEndpointConfig restEndpointConfig)
35       throws RestClientConstructionException {
36
37     if (restEndpointConfig == null) {
38       throw new RestClientConstructionException(
39           "Failed to build RestClient because RestEndpointConfig is null.");
40     }
41
42     if (restEndpointConfig.getRestAuthenticationMode() == null) {
43       throw new RestClientConstructionException(
44           "Failed to build RestClient because RestAuthenticationMode is null.");
45     }
46
47     SparkyResourceLoader resourceLoader = restEndpointConfig.getResourceLoader();
48     
49     switch (restEndpointConfig.getRestAuthenticationMode()) {
50
51       case SSL_CERT: {
52
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());
58
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());
68       }
69
70       case SSL_BASIC: {
71
72         return new RestClient() //
73             .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
74             .basicAuthUsername(restEndpointConfig.getBasicAuthUserName()) //
75             .basicAuthPassword(restEndpointConfig.getBasicAuthPassword()) //
76             .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
77             .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
78
79       }
80
81       case HTTP_NOAUTH:
82       case UNKNOWN_MODE:
83       default: {
84
85         return new RestClient() //
86             .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
87             .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
88             .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
89
90       }
91
92
93     }
94
95   }
96
97 }