Merge "Add TruststoreFile provider"
[oom/platform/cert-service.git] / trustStoreMerger / src / main / java / org / onap / oom / truststoremerger / certification / file / provider / TruststoreFileFactory.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 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;
26
27 import java.io.File;
28
29 public class TruststoreFileFactory {
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     private final FileManager fileManager;
38     private final PasswordReader passwordReader;
39
40     public TruststoreFileFactory(FileManager fileManager, PasswordReader passwordReader) {
41         this.fileManager = fileManager;
42         this.passwordReader = passwordReader;
43     }
44
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));
50         }
51         return createTypedTruststore(truststoreFile, truststorePasswordPath);
52     }
53
54     private TruststoreFile createTypedTruststore(File truststoreFile, String truststorePasswordPath)
55             throws PasswordReaderException, TruststoreFileFactoryException {
56         String extension = fileManager.getExtension(truststoreFile);
57         switch (extension) {
58             case JKS_EXTENSION:
59                 return createJksTruststore(truststoreFile, truststorePasswordPath);
60             case P12_EXTENSION:
61                 return createP12Truststore(truststoreFile, truststorePasswordPath);
62             case PEM_EXTENSION:
63                 return createPemTruststore(truststoreFile);
64             default:
65                 throw new TruststoreFileFactoryException(String.format(UNKNOWN_TRUSTSTORE_TYPE_MSG_TEMPLATE, extension));
66         }
67     }
68
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);
73     }
74
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);
79     }
80
81     private PemTruststore createPemTruststore(File truststoreFile) {
82         return new PemTruststore(truststoreFile);
83     }
84 }