58bf91a975371b4b27f5ef4212e0884ba16fefa1
[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 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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.dal.rest;
24
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;
29
30 public class RestClientFactory {
31
32   public static RestClient buildClient(RestEndpointConfig restEndpointConfig)
33       throws RestClientConstructionException {
34
35     if (restEndpointConfig == null) {
36       throw new RestClientConstructionException(
37           "Failed to build RestClient because RestEndpointConfig is null.");
38     }
39
40     if (restEndpointConfig.getRestAuthenticationMode() == null) {
41       throw new RestClientConstructionException(
42           "Failed to build RestClient because RestAuthenticationMode is null.");
43     }
44
45     switch (restEndpointConfig.getRestAuthenticationMode()) {
46
47       case SSL_CERT: {
48
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();
55
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());
65       }
66
67       case SSL_BASIC: {
68
69         return new RestClient() //
70             .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
71             .basicAuthUsername(restEndpointConfig.getBasicAuthUserName()) //
72             .basicAuthPassword(restEndpointConfig.getBasicAuthPassword()) //
73             .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
74             .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
75
76       }
77
78       case HTTP_NOAUTH:
79       case UNKNOWN_MODE:
80       default: {
81
82         return new RestClient() //
83             .authenticationMode(restEndpointConfig.getRestAuthenticationMode()) //
84             .connectTimeoutMs(restEndpointConfig.getConnectTimeoutInMs()) //
85             .readTimeoutMs(restEndpointConfig.getReadTimeoutInMs());
86
87       }
88
89
90     }
91
92   }
93
94 }