Autopsy  4.15.0
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.logging.Level;
33 import org.openide.util.io.NbObjectInputStream;
34 import org.openide.util.io.NbObjectOutputStream;
38 
44 final class ReportingConfigLoader {
45 
46  private static final Logger logger = Logger.getLogger(ReportingConfigLoader.class.getName());
47  private static final String REPORT_CONFIG_FOLDER = "ReportingConfigs"; //NON-NLS
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";
54 
67  @SuppressWarnings("unchecked")
68  static synchronized ReportingConfig loadConfig(String configName) throws ReportConfigException {
69 
70  // construct the configuration directory path
71  Path reportDirPath = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
72  File reportDirectory = reportDirPath.toFile();
73 
74  // Return null if a reporting configuration for the given name does not exist.
75  if (!reportDirectory.exists()) {
76  return null;
77  }
78 
79  if (!reportDirectory.isDirectory() || !reportDirectory.canRead()) {
80  throw new ReportConfigException("Unable to read reporting configuration directory " + reportDirPath.toString());
81  }
82 
83  // read in the configuration
84  ReportingConfig config = new ReportingConfig(configName);
85 
86  // read table report settings
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);
92  }
93 
94  // read file report settings
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);
100  }
101 
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);
107  }
108 
109  // read map of module configuration objects
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);
116  }
117 
118  if (moduleConfigs == null || moduleConfigs.isEmpty()) {
119  return config;
120  }
121 
122  // read each ReportModuleSettings object individually
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) {
129  /*
130  * NOTE: we do not want to re-throw the exception because we do
131  * not want a single error while reading in a (3rd party) report
132  * module to prevent us from reading the entire reporting
133  * configuration.
134  */
135  logger.log(Level.SEVERE, "Unable to read module settings " + filePath, ex);
136  iterator.remove();
137  }
138  }
139 
140  config.setModuleConfigs(moduleConfigs);
141 
142  return config;
143  }
144 
154  static synchronized void saveConfig(ReportingConfig reportConfig) throws ReportConfigException {
155 
156  if (reportConfig == null) {
157  throw new ReportConfigException("Reporting configuration is NULL");
158  }
159 
160  // construct the configuration directory path
161  Path pathToConfigDir = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, reportConfig.getName());
162 
163  // create configuration directory
164  try {
165  Files.createDirectories(pathToConfigDir); // does not throw if directory already exists
166  } catch (IOException | SecurityException ex) {
167  throw new ReportConfigException("Failed to create reporting configuration directory " + pathToConfigDir.toString(), ex);
168  }
169 
170  // save table report settings
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);
176  }
177 
178  // save file report settings
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);
184  }
185 
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);
191  }
192 
193  // save map of module configuration objects
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);
199  }
200 
201  // save each ReportModuleSettings object individually
202  /*
203  * NOTE: This is done to protect us from errors in reading/writing 3rd
204  * party report module settings. If we were to serialize the entire
205  * ReportingConfig object, then a single error while reading in a 3rd
206  * party report module would prevent us from reading the entire
207  * reporting configuration.
208  */
209  if (reportConfig.getModuleConfigs() == null) {
210  return;
211  }
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);
219  }
220  }
221  }
222 
223 }

Copyright © 2012-2020 Basis Technology. Generated on: Mon Jul 6 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.