Add Certification merge logic
[oom/platform/cert-service.git] / trustStoreMerger / src / main / java / org / onap / oom / truststoremerger / certification / file / provider / CertificateStoreControllerFactory.java
1 /*============LICENSE_START=======================================================
2  * oom-truststore-merger
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.oom.truststoremerger.certification.file.provider;
21
22 import static org.onap.oom.truststoremerger.api.CertificateConstants.JKS_INSTANCE;
23 import static org.onap.oom.truststoremerger.api.CertificateConstants.PKCS12_INSTANCE;
24
25 import java.io.File;
26 import java.security.KeyStore;
27 import java.security.KeyStoreException;
28 import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException;
29 import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class CertificateStoreControllerFactory {
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(CertificateStoreControllerFactory.class);
36
37     public JavaCertificateStoreController createLoadedJksCertificateStoreController(File certFile, String certPassword)
38         throws LoadTruststoreException, KeystoreInstanceException {
39         return createLoadedCertificateStoreController(certFile, certPassword, JKS_INSTANCE);
40     }
41
42     public JavaCertificateStoreController createLoadedPkcs12CertificateStoreController(File certFile, String certPassword)
43         throws KeystoreInstanceException, LoadTruststoreException {
44         return createLoadedCertificateStoreController(certFile, certPassword, PKCS12_INSTANCE);
45     }
46
47     private JavaCertificateStoreController createLoadedCertificateStoreController(File certFile, String certPassword,
48         String instanceType)
49         throws LoadTruststoreException, KeystoreInstanceException {
50         try {
51             JavaCertificateStoreController javaCertificateStoreController = new JavaCertificateStoreController(
52                 KeyStore.getInstance(instanceType), certFile, certPassword);
53             javaCertificateStoreController.loadFile();
54             return javaCertificateStoreController;
55         } catch (KeyStoreException e) {
56             LOGGER.error("Cannot initialize Java Keystore instance");
57             throw new KeystoreInstanceException(e);
58         }
59     }
60 }
61