Add HttpClient
[oom/platform/cert-service.git] / certServiceClient / src / main / java / org / onap / aaf / certservice / client / CertServiceClient.java
1 /*============LICENSE_START=======================================================
2  * aaf-certservice-client
3  * ================================================================================
4  * Copyright (C) 2020 Nokia. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.aaf.certservice.client;
21
22 import org.onap.aaf.certservice.client.api.ExitableException;
23 import org.onap.aaf.certservice.client.certification.CsrFactory;
24 import org.onap.aaf.certservice.client.certification.KeyPairFactory;
25 import org.onap.aaf.certservice.client.configuration.EnvsForClient;
26 import org.onap.aaf.certservice.client.configuration.EnvsForCsr;
27 import org.onap.aaf.certservice.client.configuration.factory.ClientConfigurationFactory;
28 import org.onap.aaf.certservice.client.configuration.factory.CsrConfigurationFactory;
29 import org.onap.aaf.certservice.client.configuration.model.ClientConfiguration;
30 import org.onap.aaf.certservice.client.configuration.model.CsrConfiguration;
31 import org.onap.aaf.certservice.client.httpclient.CloseableHttpClientProvider;
32 import org.onap.aaf.certservice.client.httpclient.HttpClient;
33 import org.onap.aaf.certservice.client.httpclient.model.CertServiceResponse;
34
35 import java.security.KeyPair;
36
37 import static org.onap.aaf.certservice.client.api.ExitCode.SUCCESS_EXIT_CODE;
38 import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.KEY_SIZE;
39 import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM;
40 import static org.onap.aaf.certservice.client.common.Base64Coder.encode;
41
42 public class CertServiceClient {
43     private AppExitHandler appExitHandler;
44
45     public CertServiceClient(AppExitHandler appExitHandler) {
46         this.appExitHandler = appExitHandler;
47     }
48
49     public void run() {
50         KeyPairFactory keyPairFactory = new KeyPairFactory(RSA_ENCRYPTION_ALGORITHM, KEY_SIZE);
51         try {
52             ClientConfiguration clientConfiguration = new ClientConfigurationFactory(new EnvsForClient()).create();
53             CsrConfiguration csrConfiguration = new CsrConfigurationFactory(new EnvsForCsr()).create();
54             KeyPair keyPair = keyPairFactory.create();
55             CsrFactory csrFactory = new CsrFactory(csrConfiguration);
56
57             CloseableHttpClientProvider provider = new CloseableHttpClientProvider(clientConfiguration.getRequestTimeout());
58             HttpClient httpClient = new HttpClient(provider, clientConfiguration.getUrlToCertService());
59
60             CertServiceResponse certServiceData =
61                     httpClient.retrieveCertServiceData(
62                             clientConfiguration.getCaName(),
63                             csrFactory.createEncodedCsr(keyPair),
64                             encode(keyPair.getPrivate().toString()));
65
66         } catch (ExitableException e) {
67             appExitHandler.exit(e.applicationExitCode());
68         }
69         appExitHandler.exit(SUCCESS_EXIT_CODE.getValue());
70     }
71 }