Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ReportingConfigLoader.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019-2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.report.infrastructure;
20 
22 import java.io.File;
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;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.Set;
33 import java.util.TreeSet;
34 import java.util.logging.Level;
35 import org.openide.util.io.NbObjectInputStream;
36 import org.openide.util.io.NbObjectOutputStream;
40 
46 final class ReportingConfigLoader {
47 
48  private static final Logger logger = Logger.getLogger(ReportingConfigLoader.class.getName());
49  private static final String REPORT_CONFIG_FOLDER = "ReportingConfigs"; //NON-NLS
50  private static final String REPORT_CONFIG_FOLDER_PATH = Paths.get(PlatformUtil.getUserConfigDirectory(), ReportingConfigLoader.REPORT_CONFIG_FOLDER).toAbsolutePath().toString();
51  private static final String REPORT_SETTINGS_FILE_EXTENSION = ".settings";
52  private static final String TABLE_REPORT_CONFIG_FILE = "TableReportSettings.settings";
53  private static final String FILE_REPORT_CONFIG_FILE = "FileReportSettings.settings";
54  private static final String GENERAL_REPORT_CONFIG_FILE = "GeneralReportSettings.settings";
55  private static final String MODULE_CONFIG_FILE = "ModuleConfigs.settings";
56 
69  @SuppressWarnings("unchecked")
70  static synchronized ReportingConfig loadConfig(String configName) throws ReportConfigException {
71 
72  // construct the configuration directory path
73  Path reportDirPath = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
74  File reportDirectory = reportDirPath.toFile();
75 
76  // Return null if a reporting configuration for the given name does not exist.
77  if (!reportDirectory.exists()) {
78  throw new ReportConfigException("Unable to find report configuration folder for " + reportDirPath.toString() + ". Please configure in the application Options panel.");
79  }
80 
81  if (!reportDirectory.isDirectory() || !reportDirectory.canRead()) {
82  throw new ReportConfigException("Unable to read reporting configuration directory " + reportDirPath.toString());
83  }
84 
85  // read in the configuration
86  ReportingConfig config = new ReportingConfig(configName);
87 
88  // read table report settings
89  String filePath = reportDirPath.toString() + File.separator + TABLE_REPORT_CONFIG_FILE;
90  try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(filePath))) {
91  config.setTableReportSettings((TableReportSettings) in.readObject());
92  } catch (IOException | ClassNotFoundException ex) {
93  throw new ReportConfigException("Unable to read table report settings " + filePath, ex);
94  }
95 
96  // read file report settings
97  filePath = reportDirPath.toString() + File.separator + FILE_REPORT_CONFIG_FILE;
98  try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(filePath))) {
99  config.setFileReportSettings((FileReportSettings) in.readObject());
100  } catch (IOException | ClassNotFoundException ex) {
101  throw new ReportConfigException("Unable to read file report settings " + filePath, ex);
102  }
103 
104  filePath = reportDirPath.resolve(GENERAL_REPORT_CONFIG_FILE).toString();
105  try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(filePath))) {
106  config.setGeneralReportSettings((GeneralReportSettings) in.readObject());
107  } catch (IOException | ClassNotFoundException ex) {
108  throw new ReportConfigException("Unable to read general report settings " + filePath, ex);
109  }
110 
111  // read map of module configuration objects
112  Map<String, ReportModuleConfig> moduleConfigs = null;
113  filePath = reportDirPath.toString() + File.separator + MODULE_CONFIG_FILE;
114  try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(filePath))) {
115  moduleConfigs = (Map<String, ReportModuleConfig>) in.readObject();
116  } catch (IOException | ClassNotFoundException ex) {
117  throw new ReportConfigException("Unable to read module configurations map " + filePath, ex);
118  }
119 
120  if (moduleConfigs == null || moduleConfigs.isEmpty()) {
121  return config;
122  }
123 
124  // read each ReportModuleSettings object individually
125  for (Iterator<Entry<String, ReportModuleConfig>> iterator = moduleConfigs.entrySet().iterator(); iterator.hasNext();) {
126  ReportModuleConfig moduleConfig = iterator.next().getValue();
127  filePath = reportDirPath.toString() + File.separator + moduleConfig.getModuleClassName() + REPORT_SETTINGS_FILE_EXTENSION;
128  try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(filePath))) {
129  moduleConfig.setModuleSettings((ReportModuleSettings) in.readObject());
130  } catch (IOException | ClassNotFoundException ex) {
131  /*
132  * NOTE: we do not want to re-throw the exception because we do
133  * not want a single error while reading in a (3rd party) report
134  * module to prevent us from reading the entire reporting
135  * configuration.
136  */
137  logger.log(Level.SEVERE, "Unable to read module settings " + filePath, ex);
138  iterator.remove();
139  }
140  }
141 
142  config.setModuleConfigs(moduleConfigs);
143 
144  return config;
145  }
146 
156  static synchronized void saveConfig(ReportingConfig reportConfig) throws ReportConfigException {
157 
158  if (reportConfig == null) {
159  throw new ReportConfigException("Reporting configuration is NULL");
160  }
161 
162  // construct the configuration directory path
163  Path pathToConfigDir = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, reportConfig.getName());
164 
165  // create configuration directory
166  try {
167  Files.createDirectories(pathToConfigDir); // does not throw if directory already exists
168  } catch (IOException | SecurityException ex) {
169  throw new ReportConfigException("Failed to create reporting configuration directory " + pathToConfigDir.toString(), ex);
170  }
171 
172  // save table report settings
173  String filePath = pathToConfigDir.toString() + File.separator + TABLE_REPORT_CONFIG_FILE;
174  try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(filePath))) {
175  out.writeObject(reportConfig.getTableReportSettings());
176  } catch (IOException ex) {
177  throw new ReportConfigException("Unable to save table report configuration " + filePath, ex);
178  }
179 
180  // save file report settings
181  filePath = pathToConfigDir.toString() + File.separator + FILE_REPORT_CONFIG_FILE;
182  try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(filePath))) {
183  out.writeObject(reportConfig.getFileReportSettings());
184  } catch (IOException ex) {
185  throw new ReportConfigException("Unable to save file report configuration " + filePath, ex);
186  }
187 
188  filePath = pathToConfigDir.resolve(GENERAL_REPORT_CONFIG_FILE).toString();
189  try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(filePath))) {
190  out.writeObject(reportConfig.getGeneralReportSettings());
191  } catch (IOException ex) {
192  throw new ReportConfigException("Unable to save general report configuration " + filePath, ex);
193  }
194 
195  // save map of module configuration objects
196  filePath = pathToConfigDir.toString() + File.separator + MODULE_CONFIG_FILE;
197  try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(filePath))) {
198  out.writeObject(reportConfig.getModuleConfigs());
199  } catch (IOException ex) {
200  throw new ReportConfigException("Unable to save module configurations map " + filePath, ex);
201  }
202 
203  // save each ReportModuleSettings object individually
204  /*
205  * NOTE: This is done to protect us from errors in reading/writing 3rd
206  * party report module settings. If we were to serialize the entire
207  * ReportingConfig object, then a single error while reading in a 3rd
208  * party report module would prevent us from reading the entire
209  * reporting configuration.
210  */
211  if (reportConfig.getModuleConfigs() == null) {
212  return;
213  }
214  for (ReportModuleConfig moduleConfig : reportConfig.getModuleConfigs().values()) {
215  ReportModuleSettings settings = moduleConfig.getModuleSettings();
216  filePath = pathToConfigDir.toString() + File.separator + moduleConfig.getModuleClassName() + REPORT_SETTINGS_FILE_EXTENSION;
217  try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(filePath))) {
218  out.writeObject(settings);
219  } catch (IOException ex) {
220  throw new ReportConfigException("Unable to save module settings " + filePath, ex);
221  }
222  }
223  }
224 
232  static synchronized Set<String> getListOfReportConfigs() {
233  File reportDirPath = new File(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH);
234  Set<String> reportNameList = new TreeSet<>();
235 
236  if (!reportDirPath.exists()) {
237  return reportNameList;
238  }
239 
240  for (File file : reportDirPath.listFiles()) {
241  reportNameList.add(file.getName());
242  }
243 
244  return reportNameList;
245  }
246 
256  static synchronized boolean configExists(String configName) {
257  // construct the configuration directory path
258  Path reportDirPath = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
259  File reportDirectory = reportDirPath.toFile();
260 
261  return reportDirectory.exists();
262  }
263 
264 }

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.