5167c301976e1ebfc666021e962849603833e020
[oom/platform/cert-service.git] / trustStoreMerger / src / main / java / org / onap / oom / certservice / postprocessor / merger / model / TruststoreFactory.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.certservice.postprocessor.merger.model;
21
22 import static org.onap.oom.certservice.postprocessor.api.CertificateConstants.JKS_TYPE;
23 import static org.onap.oom.certservice.postprocessor.api.CertificateConstants.PKCS12_TYPE;
24
25 import java.io.File;
26 import org.onap.oom.certservice.postprocessor.common.ExtensionResolver;
27 import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreFileFactoryException;
28
29 public class TruststoreFactory {
30
31     private static final String JKS_EXTENSION = ".jks";
32     private static final String P12_EXTENSION = ".p12";
33     private static final String PEM_EXTENSION = ".pem";
34     private static final String FILE_DOES_NOT_EXIST_MSG_TEMPLATE = "File: %s does not exist";
35     private static final String UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE = "Unknown truststore extension type: %s";
36
37
38     private TruststoreFactory() {
39     }
40
41     public static Truststore create(String truststoreFilePath, String truststorePasswordPath) {
42         File truststoreFile = new File(truststoreFilePath);
43         if (!ExtensionResolver.checkIfFileExists(truststoreFile)) {
44             throw new TruststoreFileFactoryException(String.format(FILE_DOES_NOT_EXIST_MSG_TEMPLATE, truststoreFile));
45         }
46         return createTypedTruststore(truststoreFile, truststorePasswordPath);
47     }
48
49     private static Truststore createTypedTruststore(File truststoreFile, String truststorePasswordPath) {
50         String extension = ExtensionResolver.get(truststoreFile);
51         switch (extension) {
52             case JKS_EXTENSION:
53                 return JavaTruststoreFactory.create(truststoreFile, truststorePasswordPath, JKS_TYPE);
54             case P12_EXTENSION:
55                 return JavaTruststoreFactory.create(truststoreFile, truststorePasswordPath, PKCS12_TYPE);
56             case PEM_EXTENSION:
57                 return new PemTruststore(truststoreFile);
58             default:
59                 throw new TruststoreFileFactoryException(
60                     String.format(UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE, extension));
61         }
62     }
63
64 }