Added ReportingConfigLoader

This commit is contained in:
Eugene Livis 2019-08-08 12:53:06 -04:00
parent 93648f3b9b
commit d46fc8fe2f
6 changed files with 182 additions and 9 deletions

View File

@ -18,6 +18,7 @@
*/
package org.sleuthkit.autopsy.report;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
@ -25,8 +26,9 @@ import java.util.Map;
* Class for persisting the file properties used by FileReportGenerator to drive
* report generation by FileReportModules.
*/
class FileReportSettings {
class FileReportSettings implements Serializable {
private static final long serialVersionUID = 1L;
private Map<FileReportDataTypes, Boolean> filePropertiesInfo = new HashMap<>();
/**

View File

@ -18,23 +18,32 @@
*/
package org.sleuthkit.autopsy.report;
import java.io.Serializable;
/**
* Class for persisting information about ReportModule (e.g. whether the report
* module is enabled).
*/
class ModuleStatus {
class ModuleStatus implements Serializable {
private ReportModule module;
private static final long serialVersionUID = 1L;
private final String moduleName;
private boolean enabled;
/**
* Creates ModuleStatus object.
*
* @param module implementation of a ReportModule interface
* @param enabled boolean flag whether the module is enabled
*/
ModuleStatus(ReportModule module, boolean enabled) {
this.module = module;
// ELTODO verify that this is fully qualified class name of a report module
this.moduleName = module.getClass().getCanonicalName();
this.enabled = enabled;
}
String getModuleClassName() {
// ELTODO verify that this is fully qualified class name of a report module
return module.getName();
return moduleName;
}
void setEnabled(boolean enabled) {

View File

@ -0,0 +1,47 @@
/*
* Autopsy Forensic Browser
*
* Copyright 2019 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sleuthkit.autopsy.report;
/**
* Instances of this exception class are thrown when there is an error during
* serialization and deserialization of reporting configuration.
*/
class ReportConfigException extends Exception {
private static final long serialVersionUID = 1L;
/**
* Constructs a new exception with the specified message.
*
* @param message The message.
*/
public ReportConfigException(String message) {
super(message);
}
/**
* Constructs a new exception with the specified message and cause.
*
* @param message The message.
* @param cause The cause.
*/
public ReportConfigException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -18,20 +18,27 @@
*/
package org.sleuthkit.autopsy.report;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* A bundling of all the settings objects that define a report configuration.
*/
class ReportingConfig {
class ReportingConfig implements Serializable {
private static final long serialVersionUID = 1L;
private String configName;
private List<ModuleStatus> moduleStatuses = new ArrayList<>();
private List<ReportModuleSettings> moduleSettings = new ArrayList<>();
private TableReportSettings tableReportSettings;
private FileReportSettings fileReportSettings;
/**
* Creates ReportingConfig object.
*
* @param configName Name of the reporting configuration
*/
ReportingConfig(String configName) {
this.configName = configName;
}

View File

@ -0,0 +1,106 @@
/*
* Autopsy Forensic Browser
*
* Copyright 2019 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sleuthkit.autopsy.report;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.openide.util.io.NbObjectInputStream;
import org.openide.util.io.NbObjectOutputStream;
import org.sleuthkit.autopsy.coreutils.PlatformUtil;
/**
* Utility class responsible for managing serialization and deserialization of
* all of the settings that make up a reporting configuration in an atomic,
* thread safe way.
*/
class ReportingConfigLoader {
private static final String REPORT_CONFIG_FOLDER = "ReportingConfigs"; //NON-NLS
private static final String REPORT_CONFIG_FOLDER_PATH = Paths.get(PlatformUtil.getUserConfigDirectory(), ReportingConfigLoader.REPORT_CONFIG_FOLDER).toAbsolutePath().toString();
/**
* Deserialize all of the settings that make up a reporting configuration in
* an atomic, thread safe way.
*
* @param configName Name of the reporting configuration
* @return ReportingConfig object if a persisted configuration exists, null
* otherwise
* @throws ReportConfigException if an error occurred while reading the
* configuration
*/
static synchronized ReportingConfig loadConfig(String configName) throws ReportConfigException {
// construct the file path
Path pathToConfigDir = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, configName);
File reportDir = pathToConfigDir.toFile();
// Return null if a reporting configuration for the given name does not exist.
if (!reportDir.exists()) {
return null;
}
if (!reportDir.isDirectory() || !reportDir.canRead()) {
throw new ReportConfigException("Unable to read reporting configuration directory " + pathToConfigDir.toString());
}
// read in the confuguration
ReportingConfig config = null;
try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(pathToConfigDir.toString()))) {
config = (ReportingConfig) in.readObject();
} catch (IOException | ClassNotFoundException ex) {
throw new ReportConfigException("Unable to read reporting configuration " + pathToConfigDir.toString(), ex);
}
return config;
}
/**
* Serialize all of the settings that make up a reporting configuration in
* an atomic, thread safe way.
*
* @param config ReportingConfig object to serialize to disk
* @throws ReportConfigException if an error occurred while saving the
* configuration
*/
static synchronized void saveConfig(ReportingConfig config) throws ReportConfigException {
// construct the file path
Path pathToConfigDir = Paths.get(ReportingConfigLoader.REPORT_CONFIG_FOLDER_PATH, config.getName());
// create configuration directory
try {
Files.createDirectories(pathToConfigDir); // does not throw if directory already exists
} catch (IOException | SecurityException ex) {
throw new ReportConfigException("Failed to create reporting configuration directory " + pathToConfigDir.toString(), ex);
}
// save the configuration
try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(pathToConfigDir.toString()))) {
out.writeObject(config);
} catch (IOException ex) {
throw new ReportConfigException("Unable to save reporting configuration " + pathToConfigDir.toString(), ex);
}
}
}

View File

@ -18,6 +18,7 @@
*/
package org.sleuthkit.autopsy.report;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.sleuthkit.datamodel.BlackboardArtifact;
@ -27,8 +28,9 @@ import org.sleuthkit.datamodel.BlackboardArtifact;
* by the TableReportGenerator class to drive report generation by
* TableReportModules.
*/
class TableReportSettings {
class TableReportSettings implements Serializable {
private static final long serialVersionUID = 1L;
private Map<BlackboardArtifact.Type, Boolean> artifactTypeSelections = new HashMap<>();
private Map<String, Boolean> tagNameSelections = new HashMap<>();