Fix for env names
[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.merger.exception.KeystoreInstanceException;
27 import org.onap.oom.certservice.postprocessor.merger.exception.LoadTruststoreException;
28 import org.onap.oom.certservice.postprocessor.merger.exception.PasswordReaderException;
29 import org.onap.oom.certservice.postprocessor.common.ExtensionResolver;
30 import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreFileFactoryException;
31
32 public class TruststoreFactory {
33
34     private static final String JKS_EXTENSION = ".jks";
35     private static final String P12_EXTENSION = ".p12";
36     private static final String PEM_EXTENSION = ".pem";
37     private static final String FILE_DOES_NOT_EXIST_MSG_TEMPLATE = "File: %s does not exist";
38     private static final String UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE = "Unknown truststore extension type: %s";
39
40
41     private TruststoreFactory() {
42     }
43
44     public static Truststore create(String truststoreFilePath, String truststorePasswordPath)
45         throws TruststoreFileFactoryException, PasswordReaderException, KeystoreInstanceException, LoadTruststoreException {
46         File truststoreFile = new File(truststoreFilePath);
47         if (!ExtensionResolver.checkIfFileExists(truststoreFile)) {
48             throw new TruststoreFileFactoryException(String.format(FILE_DOES_NOT_EXIST_MSG_TEMPLATE, truststoreFile));
49         }
50         return createTypedTruststore(truststoreFile, truststorePasswordPath);
51     }
52
53     private static Truststore createTypedTruststore(File truststoreFile, String truststorePasswordPath)
54         throws KeystoreInstanceException, PasswordReaderException, LoadTruststoreException, TruststoreFileFactoryException {
55         String extension = ExtensionResolver.get(truststoreFile);
56         switch (extension) {
57             case JKS_EXTENSION:
58                 return JavaTruststoreFactory.create(truststoreFile, truststorePasswordPath, JKS_TYPE);
59             case P12_EXTENSION:
60                 return JavaTruststoreFactory.create(truststoreFile, truststorePasswordPath, PKCS12_TYPE);
61             case PEM_EXTENSION:
62                 return new PemTruststore(truststoreFile);
63             default:
64                 throw new TruststoreFileFactoryException(
65                     String.format(UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE, extension));
66         }
67     }
68
69 }