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.truststoremerger.certification.file.provider;
22 import org.onap.oom.truststoremerger.certification.file.JksTruststore;
23 import org.onap.oom.truststoremerger.certification.file.P12Truststore;
24 import org.onap.oom.truststoremerger.certification.file.PemTruststore;
25 import org.onap.oom.truststoremerger.certification.file.TruststoreFile;
29 public class TruststoreFileFactory {
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";
37 private final FileManager fileManager;
38 private final PasswordReader passwordReader;
40 public TruststoreFileFactory(FileManager fileManager, PasswordReader passwordReader) {
41 this.fileManager = fileManager;
42 this.passwordReader = passwordReader;
45 TruststoreFile create(String truststoreFilePath, String truststorePasswordPath)
46 throws TruststoreFileFactoryException, PasswordReaderException {
47 File truststoreFile = new File(truststoreFilePath);
48 if (!fileManager.checkIfFileExists(truststoreFile)) {
49 throw new TruststoreFileFactoryException(String.format(FILE_DOES_NOT_EXIST_MSG_TEMPLATE, truststoreFile));
51 return createTypedTruststore(truststoreFile, truststorePasswordPath);
54 private TruststoreFile createTypedTruststore(File truststoreFile, String truststorePasswordPath)
55 throws PasswordReaderException, TruststoreFileFactoryException {
56 String extension = fileManager.getExtension(truststoreFile);
59 return createJksTruststore(truststoreFile, truststorePasswordPath);
61 return createP12Truststore(truststoreFile, truststorePasswordPath);
63 return createPemTruststore(truststoreFile);
65 throw new TruststoreFileFactoryException(String.format(UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE, extension));
69 private JksTruststore createJksTruststore(File truststoreFile, String truststorePasswordPath)
70 throws PasswordReaderException {
71 String password = passwordReader.readPassword(new File(truststorePasswordPath));
72 return new JksTruststore(truststoreFile, password);
75 private P12Truststore createP12Truststore(File truststoreFile, String truststorePasswordPath)
76 throws PasswordReaderException {
77 String password = passwordReader.readPassword(new File(truststorePasswordPath));
78 return new P12Truststore(truststoreFile, password);
81 private PemTruststore createPemTruststore(File truststoreFile) {
82 return new PemTruststore(truststoreFile);