1903 added wrapper for serialization of FileIngestFilters to avoid unchecked casting warning

This commit is contained in:
William Schaefer 2017-01-19 19:17:54 -05:00
parent 9530b7978a
commit 89fff3813a

View File

@ -22,6 +22,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
@ -38,7 +39,6 @@ import org.sleuthkit.autopsy.coreutils.PlatformUtil;
import org.sleuthkit.autopsy.modules.interestingitems.FilesSet.Rule; import org.sleuthkit.autopsy.modules.interestingitems.FilesSet.Rule;
import org.sleuthkit.autopsy.modules.interestingitems.FilesSet.Rule.MetaTypeCondition; import org.sleuthkit.autopsy.modules.interestingitems.FilesSet.Rule.MetaTypeCondition;
/** /**
* Provides access to collections of FilesSet definitions persisted to disk. * Provides access to collections of FilesSet definitions persisted to disk.
* Clients receive copies of the most recent FilesSet definitions for * Clients receive copies of the most recent FilesSet definitions for
@ -151,9 +151,8 @@ public final class FilesSetsManager extends Observable {
if (fileSetFile.exists()) { if (fileSetFile.exists()) {
try { try {
try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(filePathStr))) { try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(filePathStr))) {
@SuppressWarnings("unchecked") FileIngestFiltersSerializable filesSetsSettings = (FileIngestFiltersSerializable)in.readObject();
Map<String, FilesSet> filesSetsSettings = (Map<String, FilesSet>)in.readObject(); return filesSetsSettings.getFilesSets();
return filesSetsSettings;
} }
} catch (IOException | ClassNotFoundException ex) { } catch (IOException | ClassNotFoundException ex) {
throw new FilesSetsManagerException(String.format("Failed to read settings from %s", filePathStr), ex); throw new FilesSetsManagerException(String.format("Failed to read settings from %s", filePathStr), ex);
@ -189,13 +188,34 @@ public final class FilesSetsManager extends Observable {
void setCustomFileIngestFilters(Map<String, FilesSet> filesSets) throws FilesSetsManagerException { void setCustomFileIngestFilters(Map<String, FilesSet> filesSets) throws FilesSetsManagerException {
synchronized (FILE_INGEST_FILTER_LOCK) { synchronized (FILE_INGEST_FILTER_LOCK) {
try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(Paths.get(PlatformUtil.getUserConfigDirectory(), FILE_INGEST_FILTER_DEFS_NAME).toString()))) { try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(Paths.get(PlatformUtil.getUserConfigDirectory(), FILE_INGEST_FILTER_DEFS_NAME).toString()))) {
out.writeObject(filesSets); out.writeObject(new FileIngestFiltersSerializable(filesSets));
} catch (IOException ex) { } catch (IOException ex) {
throw new FilesSetsManagerException(String.format("Failed to write settings to %s", FILE_INGEST_FILTER_DEFS_NAME), ex); throw new FilesSetsManagerException(String.format("Failed to write settings to %s", FILE_INGEST_FILTER_DEFS_NAME), ex);
} }
} }
} }
/**
* Class for storage of FileIngestFilters as serialized objects.
*/
private class FileIngestFiltersSerializable implements Serializable {
private static final long serialVersionUID = 1L;
private Map<String, FilesSet> filesSets;
FileIngestFiltersSerializable(Map<String, FilesSet> filesSets) {
this.filesSets = filesSets;
}
/**
* @return the filesSets
*/
Map<String, FilesSet> getFilesSets() {
return filesSets;
}
}
public static class FilesSetsManagerException extends Exception { public static class FilesSetsManagerException extends Exception {
FilesSetsManagerException() { FilesSetsManagerException() {