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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.oom.certservice.postprocessor.merger.model;
22 import static org.onap.oom.certservice.postprocessor.api.CertificateConstants.JKS_TYPE;
23 import static org.onap.oom.certservice.postprocessor.api.CertificateConstants.PKCS12_TYPE;
26 import org.onap.oom.certservice.postprocessor.common.ExtensionResolver;
27 import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreFileFactoryException;
29 public class TruststoreFactory {
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";
38 private TruststoreFactory() {
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));
46 return createTypedTruststore(truststoreFile, truststorePasswordPath);
49 private static Truststore createTypedTruststore(File truststoreFile, String truststorePasswordPath) {
50 String extension = ExtensionResolver.get(truststoreFile);
53 return JavaTruststoreFactory.create(truststoreFile, truststorePasswordPath, JKS_TYPE);
55 return JavaTruststoreFactory.create(truststoreFile, truststorePasswordPath, PKCS12_TYPE);
57 return new PemTruststore(truststoreFile);
59 throw new TruststoreFileFactoryException(
60 String.format(UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE, extension));