Adding interfaces in documentation
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / dal / rest / RestClientFactory.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.sparky.dal.rest;
22
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;
27
28 public class RestClientFactory {
29
30   public static RestClient buildClient(RestEndpointConfig restEndpointConfig)
31       throws RestClientConstructionException {
32
33     if (restEndpointConfig == null) {
34       throw new RestClientConstructionException(
35           "Failed to build RestClient because RestEndpointConfig is null.");
36     }
37
38     if (restEndpointConfig.getRestAuthenticationMode() == null) {
39       throw new RestClientConstructionException(
40           "Failed to build RestClient because RestAuthenticationMode is null.");
41     }
42
43     switch (restEndpointConfig.getRestAuthenticationMode()) {
44
45       case SSL_CERT: {
46
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();
53
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());
63       }
64
65       case SSL_BASIC: {
66
67         return new RestClient() //
68             .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
69             .basicAuthUsername(restEndpointConfig.getBasicAuthUserName()) //
70             .basicAuthPassword(restEndpointConfig.getBasicAuthPassword()) //
71             .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
72             .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
73
74       }
75
76       case HTTP_NOAUTH:
77       case UNKNOWN_MODE:
78       default: {
79
80         return new RestClient() //
81             .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
82             .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
83             .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
84
85       }
86
87
88     }
89
90   }
91
92 }