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.Iterator;
31 import java.util.Map.Entry;
32 import java.util.logging.Level;
33 import org.openide.util.io.NbObjectInputStream;
34 import org.openide.util.io.NbObjectOutputStream;
43 final class ReportingConfigLoader {
45 private static final Logger logger = Logger.getLogger(ReportingConfigLoader.class.getName());
46 private static final String REPORT_CONFIG_FOLDER =
"ReportingConfigs";
47 private static final String REPORT_CONFIG_FOLDER_PATH = Paths.get(PlatformUtil.getUserConfigDirectory(), ReportingConfigLoader.REPORT_CONFIG_FOLDER).toAbsolutePath().toString();
48 private static final String REPORT_SETTINGS_FILE_EXTENSION =
".settings";
49 private static final String TABLE_REPORT_CONFIG_FILE =
"TableReportSettings.settings";
50 private static final String FILE_REPORT_CONFIG_FILE =
"FileReportSettings.settings";
51 private static final String MODULE_CONFIG_FILE =
"ModuleConfigs.settings";
65 @SuppressWarnings(
"unchecked")
66 static synchronized ReportingConfig loadConfig(String configName) throws ReportConfigException {
69 Path reportDirPath = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
70 File reportDirectory = reportDirPath.toFile();
73 if (!reportDirectory.exists()) {
77 if (!reportDirectory.isDirectory() || !reportDirectory.canRead()) {
78 throw new ReportConfigException(
"Unable to read reporting configuration directory " + reportDirPath.toString());
82 ReportingConfig config =
new ReportingConfig(configName);
85 String filePath = reportDirPath.toString() + File.separator + TABLE_REPORT_CONFIG_FILE;
86 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
87 config.setTableReportSettings((TableReportSettings) in.readObject());
88 }
catch (IOException | ClassNotFoundException ex) {
89 throw new ReportConfigException(
"Unable to read table report settings " + filePath, ex);
93 filePath = reportDirPath.toString() + File.separator + FILE_REPORT_CONFIG_FILE;
94 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
95 config.setFileReportSettings((FileReportSettings) in.readObject());
96 }
catch (IOException | ClassNotFoundException ex) {
97 throw new ReportConfigException(
"Unable to read file report settings " + filePath, ex);
101 Map<String, ReportModuleConfig> moduleConfigs = null;
102 filePath = reportDirPath.toString() + File.separator + MODULE_CONFIG_FILE;
103 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
104 moduleConfigs = (Map<String, ReportModuleConfig>) in.readObject();
105 }
catch (IOException | ClassNotFoundException ex) {
106 throw new ReportConfigException(
"Unable to read module configurations map " + filePath, ex);
109 if (moduleConfigs == null || moduleConfigs.isEmpty()) {
114 for (Iterator<Entry<String, ReportModuleConfig>> iterator = moduleConfigs.entrySet().iterator(); iterator.hasNext();) {
115 ReportModuleConfig moduleConfig = iterator.next().getValue();
116 filePath = reportDirPath.toString() + File.separator + moduleConfig.getModuleClassName() + REPORT_SETTINGS_FILE_EXTENSION;
117 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
118 moduleConfig.setModuleSettings((ReportModuleSettings) in.readObject());
119 }
catch (IOException | ClassNotFoundException ex) {
126 logger.log(Level.SEVERE,
"Unable to read module settings " + filePath, ex);
131 config.setModuleConfigs(moduleConfigs);
145 static synchronized void saveConfig(ReportingConfig reportConfig)
throws ReportConfigException {
147 if (reportConfig == null) {
148 throw new ReportConfigException(
"Reporting configuration is NULL");
152 Path pathToConfigDir = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, reportConfig.getName());
156 Files.createDirectories(pathToConfigDir);
157 }
catch (IOException | SecurityException ex) {
158 throw new ReportConfigException(
"Failed to create reporting configuration directory " + pathToConfigDir.toString(), ex);
162 String filePath = pathToConfigDir.toString() + File.separator + TABLE_REPORT_CONFIG_FILE;
163 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
164 out.writeObject(reportConfig.getTableReportSettings());
165 }
catch (IOException ex) {
166 throw new ReportConfigException(
"Unable to save table report configuration " + filePath, ex);
170 filePath = pathToConfigDir.toString() + File.separator + FILE_REPORT_CONFIG_FILE;
171 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
172 out.writeObject(reportConfig.getFileReportSettings());
173 }
catch (IOException ex) {
174 throw new ReportConfigException(
"Unable to save file report configuration " + filePath, ex);
178 filePath = pathToConfigDir.toString() + File.separator + MODULE_CONFIG_FILE;
179 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
180 out.writeObject(reportConfig.getModuleConfigs());
181 }
catch (IOException ex) {
182 throw new ReportConfigException(
"Unable to save module configurations map " + filePath, ex);
193 if (reportConfig.getModuleConfigs() == null) {
196 for (ReportModuleConfig moduleConfig : reportConfig.getModuleConfigs().values()) {
197 ReportModuleSettings settings = moduleConfig.getModuleSettings();
198 filePath = pathToConfigDir.toString() + File.separator + moduleConfig.getModuleClassName() + REPORT_SETTINGS_FILE_EXTENSION;
199 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
200 out.writeObject(settings);
201 }
catch (IOException ex) {
202 throw new ReportConfigException(
"Unable to save module settings " + filePath, ex);