Autopsy  4.14.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 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;
37 
43 final class ReportingConfigLoader {
44 
45  private static final Logger logger = Logger.getLogger(ReportingConfigLoader.class.getName());
46  private static final String REPORT_CONFIG_FOLDER = "ReportingConfigs"; //NON-NLS
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";
52 
65  @SuppressWarnings("unchecked")
66  static synchronized ReportingConfig loadConfig(String configName) throws ReportConfigException {
67 
68  // construct the configuration directory path
69  Path reportDirPath = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
70  File reportDirectory = reportDirPath.toFile();
71 
72  // Return null if a reporting configuration for the given name does not exist.
73  if (!reportDirectory.exists()) {
74  return null;
75  }
76 
77  if (!reportDirectory.isDirectory() || !reportDirectory.canRead()) {
78  throw new ReportConfigException("Unable to read reporting configuration directory " + reportDirPath.toString());
79  }
80 
81  // read in the configuration
82  ReportingConfig config = new ReportingConfig(configName);
83 
84  // read table report settings
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);
90  }
91 
92  // read file report settings
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);
98  }
99 
100  // read map of module configuration objects
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);
107  }
108 
109  if (moduleConfigs == null || moduleConfigs.isEmpty()) {
110  return config;
111  }
112 
113  // read each ReportModuleSettings object individually
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) {
120  /*
121  * NOTE: we do not want to re-throw the exception because we do
122  * not want a single error while reading in a (3rd party) report
123  * module to prevent us from reading the entire reporting
124  * configuration.
125  */
126  logger.log(Level.SEVERE, "Unable to read module settings " + filePath, ex);
127  iterator.remove();
128  }
129  }
130 
131  config.setModuleConfigs(moduleConfigs);
132 
133  return config;
134  }
135 
145  static synchronized void saveConfig(ReportingConfig reportConfig) throws ReportConfigException {
146 
147  if (reportConfig == null) {
148  throw new ReportConfigException("Reporting configuration is NULL");
149  }
150 
151  // construct the configuration directory path
152  Path pathToConfigDir = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, reportConfig.getName());
153 
154  // create configuration directory
155  try {
156  Files.createDirectories(pathToConfigDir); // does not throw if directory already exists
157  } catch (IOException | SecurityException ex) {
158  throw new ReportConfigException("Failed to create reporting configuration directory " + pathToConfigDir.toString(), ex);
159  }
160 
161  // save table report settings
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);
167  }
168 
169  // save file report settings
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);
175  }
176 
177  // save map of module configuration objects
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);
183  }
184 
185  // save each ReportModuleSettings object individually
186  /*
187  * NOTE: This is done to protect us from errors in reading/writing 3rd
188  * party report module settings. If we were to serialize the entire
189  * ReportingConfig object, then a single error while reading in a 3rd
190  * party report module would prevent us from reading the entire
191  * reporting configuration.
192  */
193  if (reportConfig.getModuleConfigs() == null) {
194  return;
195  }
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);
203  }
204  }
205  }
206 
207 }

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