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;
44 final class ReportingConfigLoader {
46 private static final Logger logger = Logger.getLogger(ReportingConfigLoader.class.getName());
47 private static final String REPORT_CONFIG_FOLDER =
"ReportingConfigs";
48 private static final String REPORT_CONFIG_FOLDER_PATH = Paths.get(PlatformUtil.getUserConfigDirectory(), ReportingConfigLoader.REPORT_CONFIG_FOLDER).toAbsolutePath().toString();
49 private static final String REPORT_SETTINGS_FILE_EXTENSION =
".settings";
50 private static final String TABLE_REPORT_CONFIG_FILE =
"TableReportSettings.settings";
51 private static final String FILE_REPORT_CONFIG_FILE =
"FileReportSettings.settings";
52 private static final String GENERAL_REPORT_CONFIG_FILE =
"GeneralReportSettings.settings";
53 private static final String MODULE_CONFIG_FILE =
"ModuleConfigs.settings";
67 @SuppressWarnings(
"unchecked")
68 static synchronized ReportingConfig loadConfig(String configName) throws ReportConfigException {
71 Path reportDirPath = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
72 File reportDirectory = reportDirPath.toFile();
75 if (!reportDirectory.exists()) {
79 if (!reportDirectory.isDirectory() || !reportDirectory.canRead()) {
80 throw new ReportConfigException(
"Unable to read reporting configuration directory " + reportDirPath.toString());
84 ReportingConfig config =
new ReportingConfig(configName);
87 String filePath = reportDirPath.toString() + File.separator + TABLE_REPORT_CONFIG_FILE;
88 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
89 config.setTableReportSettings((TableReportSettings) in.readObject());
90 }
catch (IOException | ClassNotFoundException ex) {
91 throw new ReportConfigException(
"Unable to read table report settings " + filePath, ex);
95 filePath = reportDirPath.toString() + File.separator + FILE_REPORT_CONFIG_FILE;
96 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
97 config.setFileReportSettings((FileReportSettings) in.readObject());
98 }
catch (IOException | ClassNotFoundException ex) {
99 throw new ReportConfigException(
"Unable to read file report settings " + filePath, ex);
102 filePath = reportDirPath.resolve(GENERAL_REPORT_CONFIG_FILE).toString();
103 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
104 config.setGeneralReportSettings((GeneralReportSettings) in.readObject());
105 }
catch (IOException | ClassNotFoundException ex) {
106 throw new ReportConfigException(
"Unable to read general report settings " + filePath, ex);
110 Map<String, ReportModuleConfig> moduleConfigs = null;
111 filePath = reportDirPath.toString() + File.separator + MODULE_CONFIG_FILE;
112 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
113 moduleConfigs = (Map<String, ReportModuleConfig>) in.readObject();
114 }
catch (IOException | ClassNotFoundException ex) {
115 throw new ReportConfigException(
"Unable to read module configurations map " + filePath, ex);
118 if (moduleConfigs == null || moduleConfigs.isEmpty()) {
123 for (Iterator<Entry<String, ReportModuleConfig>> iterator = moduleConfigs.entrySet().iterator(); iterator.hasNext();) {
124 ReportModuleConfig moduleConfig = iterator.next().getValue();
125 filePath = reportDirPath.toString() + File.separator + moduleConfig.getModuleClassName() + REPORT_SETTINGS_FILE_EXTENSION;
126 try (NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePath))) {
127 moduleConfig.setModuleSettings((ReportModuleSettings) in.readObject());
128 }
catch (IOException | ClassNotFoundException ex) {
135 logger.log(Level.SEVERE,
"Unable to read module settings " + filePath, ex);
140 config.setModuleConfigs(moduleConfigs);
154 static synchronized void saveConfig(ReportingConfig reportConfig)
throws ReportConfigException {
156 if (reportConfig == null) {
157 throw new ReportConfigException(
"Reporting configuration is NULL");
161 Path pathToConfigDir = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, reportConfig.getName());
165 Files.createDirectories(pathToConfigDir);
166 }
catch (IOException | SecurityException ex) {
167 throw new ReportConfigException(
"Failed to create reporting configuration directory " + pathToConfigDir.toString(), ex);
171 String filePath = pathToConfigDir.toString() + File.separator + TABLE_REPORT_CONFIG_FILE;
172 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
173 out.writeObject(reportConfig.getTableReportSettings());
174 }
catch (IOException ex) {
175 throw new ReportConfigException(
"Unable to save table report configuration " + filePath, ex);
179 filePath = pathToConfigDir.toString() + File.separator + FILE_REPORT_CONFIG_FILE;
180 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
181 out.writeObject(reportConfig.getFileReportSettings());
182 }
catch (IOException ex) {
183 throw new ReportConfigException(
"Unable to save file report configuration " + filePath, ex);
186 filePath = pathToConfigDir.resolve(GENERAL_REPORT_CONFIG_FILE).toString();
187 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
188 out.writeObject(reportConfig.getGeneralReportSettings());
189 }
catch (IOException ex) {
190 throw new ReportConfigException(
"Unable to save general report configuration " + filePath, ex);
194 filePath = pathToConfigDir.toString() + File.separator + MODULE_CONFIG_FILE;
195 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
196 out.writeObject(reportConfig.getModuleConfigs());
197 }
catch (IOException ex) {
198 throw new ReportConfigException(
"Unable to save module configurations map " + filePath, ex);
209 if (reportConfig.getModuleConfigs() == null) {
212 for (ReportModuleConfig moduleConfig : reportConfig.getModuleConfigs().values()) {
213 ReportModuleSettings settings = moduleConfig.getModuleSettings();
214 filePath = pathToConfigDir.toString() + File.separator + moduleConfig.getModuleClassName() + REPORT_SETTINGS_FILE_EXTENSION;
215 try (NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(filePath))) {
216 out.writeObject(settings);
217 }
catch (IOException ex) {
218 throw new ReportConfigException(
"Unable to save module settings " + filePath, ex);