19 package org.sleuthkit.autopsy.report.infrastructure;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.Arrays;
30 import java.util.Iterator;
31 import java.util.List;
33 import java.util.Map.Entry;
35 import java.util.TreeSet;
36 import java.util.logging.Level;
37 import org.openide.util.io.NbObjectInputStream;
38 import org.openide.util.io.NbObjectOutputStream;
48 final class ReportingConfigLoader {
50 private static final Logger logger = Logger.getLogger(ReportingConfigLoader.class.getName());
51 private static final String REPORT_CONFIG_FOLDER =
"ReportingConfigs";
52 private static final String REPORT_CONFIG_FOLDER_PATH = Paths.get(PlatformUtil.getUserConfigDirectory(), ReportingConfigLoader.REPORT_CONFIG_FOLDER).toAbsolutePath().toString();
53 private static final String REPORT_SETTINGS_FILE_EXTENSION =
".settings";
54 private static final String TABLE_REPORT_CONFIG_FILE =
"TableReportSettings.settings";
55 private static final String FILE_REPORT_CONFIG_FILE =
"FileReportSettings.settings";
56 private static final String GENERAL_REPORT_CONFIG_FILE =
"GeneralReportSettings.settings";
57 private static final String MODULE_CONFIG_FILE =
"ModuleConfigs.settings";
62 private static final List<String> DELETED_REPORT_MODULES = Arrays.asList(
"org.sleuthkit.autopsy.report.modules.stix.STIXReportModule");
76 @SuppressWarnings(
"unchecked")
77 static synchronized ReportingConfig loadConfig(String configName) throws ReportConfigException {
80 Path reportDirPath = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
81 File reportDirectory = reportDirPath.toFile();
84 if (!reportDirectory.exists()) {
85 throw new ReportConfigException(
"Unable to find report configuration folder for " + reportDirPath.toString() +
". Please configure in the application Options panel.");
88 if (!reportDirectory.isDirectory() || !reportDirectory.canRead()) {
89 throw new ReportConfigException(
"Unable to read reporting configuration directory " + reportDirPath.toString());
93 ReportingConfig config =
new ReportingConfig(configName);
96 String filePath = reportDirPath.toString() + File.separator + TABLE_REPORT_CONFIG_FILE;
97 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
98 config.setTableReportSettings((TableReportSettings) in.readObject());
99 }
catch (IOException | ClassNotFoundException ex) {
100 throw new ReportConfigException(
"Unable to read table report settings " + filePath, ex);
104 filePath = reportDirPath.toString() + File.separator + FILE_REPORT_CONFIG_FILE;
105 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
106 config.setFileReportSettings((FileReportSettings) in.readObject());
107 }
catch (IOException | ClassNotFoundException ex) {
108 throw new ReportConfigException(
"Unable to read file report settings " + filePath, ex);
111 filePath = reportDirPath.resolve(GENERAL_REPORT_CONFIG_FILE).toString();
112 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
113 config.setGeneralReportSettings((GeneralReportSettings) in.readObject());
114 }
catch (IOException | ClassNotFoundException ex) {
115 throw new ReportConfigException(
"Unable to read general report settings " + filePath, ex);
119 Map<String, ReportModuleConfig> moduleConfigs = null;
120 filePath = reportDirPath.toString() + File.separator + MODULE_CONFIG_FILE;
121 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
122 moduleConfigs = (Map<String, ReportModuleConfig>) in.readObject();
123 }
catch (IOException | ClassNotFoundException ex) {
124 throw new ReportConfigException(
"Unable to read module configurations map " + filePath, ex);
127 if (moduleConfigs == null || moduleConfigs.isEmpty()) {
132 for (Iterator<Entry<String, ReportModuleConfig>> iterator = moduleConfigs.entrySet().iterator(); iterator.hasNext();) {
133 ReportModuleConfig moduleConfig = iterator.next().getValue();
134 if (DELETED_REPORT_MODULES.contains(moduleConfig.getModuleClassName())) {
138 filePath = reportDirPath.toString() + File.separator + moduleConfig.getModuleClassName() + REPORT_SETTINGS_FILE_EXTENSION;
139 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
140 moduleConfig.setModuleSettings((ReportModuleSettings) in.readObject());
141 }
catch (IOException | ClassNotFoundException ex) {
148 logger.log(Level.SEVERE,
"Unable to read module settings " + filePath, ex);
153 config.setModuleConfigs(moduleConfigs);
167 static synchronized void saveConfig(ReportingConfig reportConfig)
throws ReportConfigException {
169 if (reportConfig == null) {
170 throw new ReportConfigException(
"Reporting configuration is NULL");
174 Path pathToConfigDir = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, reportConfig.getName());
178 Files.createDirectories(pathToConfigDir);
179 }
catch (IOException | SecurityException ex) {
180 throw new ReportConfigException(
"Failed to create reporting configuration directory " + pathToConfigDir.toString(), ex);
184 String filePath = pathToConfigDir.toString() + File.separator + TABLE_REPORT_CONFIG_FILE;
185 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
186 out.writeObject(reportConfig.getTableReportSettings());
187 }
catch (IOException ex) {
188 throw new ReportConfigException(
"Unable to save table report configuration " + filePath, ex);
192 filePath = pathToConfigDir.toString() + File.separator + FILE_REPORT_CONFIG_FILE;
193 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
194 out.writeObject(reportConfig.getFileReportSettings());
195 }
catch (IOException ex) {
196 throw new ReportConfigException(
"Unable to save file report configuration " + filePath, ex);
199 filePath = pathToConfigDir.resolve(GENERAL_REPORT_CONFIG_FILE).toString();
200 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
201 out.writeObject(reportConfig.getGeneralReportSettings());
202 }
catch (IOException ex) {
203 throw new ReportConfigException(
"Unable to save general report configuration " + filePath, ex);
207 filePath = pathToConfigDir.toString() + File.separator + MODULE_CONFIG_FILE;
208 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
209 out.writeObject(reportConfig.getModuleConfigs());
210 }
catch (IOException ex) {
211 throw new ReportConfigException(
"Unable to save module configurations map " + filePath, ex);
222 if (reportConfig.getModuleConfigs() == null) {
225 for (ReportModuleConfig moduleConfig : reportConfig.getModuleConfigs().values()) {
226 ReportModuleSettings settings = moduleConfig.getModuleSettings();
227 filePath = pathToConfigDir.toString() + File.separator + moduleConfig.getModuleClassName() + REPORT_SETTINGS_FILE_EXTENSION;
228 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
229 out.writeObject(settings);
230 }
catch (IOException ex) {
231 throw new ReportConfigException(
"Unable to save module settings " + filePath, ex);
243 static synchronized Set<String> getListOfReportConfigs() {
244 File reportDirPath =
new File(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH);
245 Set<String> reportNameList =
new TreeSet<>();
247 if (!reportDirPath.exists()) {
248 return reportNameList;
251 for (File file : reportDirPath.listFiles()) {
252 reportNameList.add(file.getName());
255 return reportNameList;
267 static synchronized boolean configExists(String configName) {
269 Path reportDirPath = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
270 File reportDirectory = reportDirPath.toFile();
272 return reportDirectory.exists();