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;
 
   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;
 
   46 final class ReportingConfigLoader {
 
   48     private static final Logger logger = Logger.getLogger(ReportingConfigLoader.class.getName());
 
   49     private static final String REPORT_CONFIG_FOLDER = 
"ReportingConfigs"; 
 
   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";
 
   69     @SuppressWarnings(
"unchecked")
 
   70     static synchronized ReportingConfig loadConfig(String configName) throws ReportConfigException {
 
   73         Path reportDirPath = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
 
   74         File reportDirectory = reportDirPath.toFile();
 
   77         if (!reportDirectory.exists()) {
 
   78             throw new ReportConfigException(
"Unable to find report configuration folder for " + reportDirPath.toString() + 
". Please configure in the application Options panel.");
 
   81         if (!reportDirectory.isDirectory() || !reportDirectory.canRead()) {
 
   82             throw new ReportConfigException(
"Unable to read reporting configuration directory " + reportDirPath.toString());
 
   86         ReportingConfig config = 
new ReportingConfig(configName);
 
   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);
 
   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);
 
  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);
 
  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);
 
  120         if (moduleConfigs == null || moduleConfigs.isEmpty()) {
 
  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) {
 
  137                 logger.log(Level.SEVERE, 
"Unable to read module settings " + filePath, ex);
 
  142         config.setModuleConfigs(moduleConfigs);
 
  156     static synchronized void saveConfig(ReportingConfig reportConfig) 
throws ReportConfigException {
 
  158         if (reportConfig == null) {
 
  159             throw new ReportConfigException(
"Reporting configuration is NULL");
 
  163         Path pathToConfigDir = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, reportConfig.getName());
 
  167             Files.createDirectories(pathToConfigDir); 
 
  168         } 
catch (IOException | SecurityException ex) {
 
  169             throw new ReportConfigException(
"Failed to create reporting configuration directory " + pathToConfigDir.toString(), ex);
 
  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);
 
  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);
 
  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);
 
  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);
 
  211         if (reportConfig.getModuleConfigs() == null) {
 
  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);
 
  232     static synchronized Set<String> getListOfReportConfigs() {
 
  233         File reportDirPath = 
new File(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH);
 
  234         Set<String> reportNameList = 
new TreeSet<>();
 
  236         if (!reportDirPath.exists()) {
 
  237             return reportNameList;
 
  240         for (File file : reportDirPath.listFiles()) {
 
  241             reportNameList.add(file.getName());
 
  244         return reportNameList;
 
  256     static synchronized boolean configExists(String configName) {
 
  258         Path reportDirPath = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
 
  259         File reportDirectory = reportDirPath.toFile();
 
  261         return reportDirectory.exists();