From b20a3ca40c7fb575df1a6468301d26d4ecf79aad Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Thu, 10 Mar 2016 15:58:42 -0500 Subject: [PATCH 01/14] Moved towards serialization --- .../autopsy/modules/fileextmismatch/FileExtMismatchXML.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java index a85964f921..062f4e175b 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java @@ -54,6 +54,8 @@ class FileExtMismatchXML { private static final String SIG_MIMETYPE_ATTR = "mimetype"; //NON-NLS private static final String DEFAULT_CONFIG_FILE_NAME = "mismatch_config.xml"; //NON-NLS + private static final String DEFAULT_SERIALIZED_FILE_NAME = "mismatch_config.settings"; + private static final String DEFAULT_SERIALIZED_FILE_PATH = PlatformUtil.getUserConfigDirectory() + File.separator + DEFAULT_SERIALIZED_FILE_NAME; protected String filePath; @@ -86,6 +88,10 @@ class FileExtMismatchXML { */ public HashMap load() { HashMap sigTypeToExtMap = new HashMap<>(); + File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH); + if (serializedFile.exists()) { + + } try { final Document doc = XMLUtil.loadDoc(FileExtMismatchXML.class, filePath); From d67d81dcb5c7ae73918acea24146ec8a0b2ca748 Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Thu, 10 Mar 2016 16:20:35 -0500 Subject: [PATCH 02/14] Moved towards serialization --- .../FileExtMismatchSettings.java | 40 +++++++++++++++++++ .../FileExtMismatchSettingsPanel.java | 2 +- .../fileextmismatch/FileExtMismatchXML.java | 32 ++++++++++++++- 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100755 Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java new file mode 100755 index 0000000000..b798f860f0 --- /dev/null +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java @@ -0,0 +1,40 @@ +/* + * Autopsy Forensic Browser + * + * Copyright 2011-2016 Basis Technology Corp. + * Contact: carrier sleuthkit 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.modules.fileextmismatch; + +import java.io.Serializable; +import java.util.HashMap; + +class FileExtMismatchSettings implements Serializable { + private static final long serialVersionUID = 1L; + private final HashMap sigTypeToExtMap; + + FileExtMismatchSettings(HashMap sigTypeToExtMap) { + this.sigTypeToExtMap = sigTypeToExtMap; + } + + /** + * @return the sig type to extension map + */ + HashMap getSigTypeToExtMap() { + return sigTypeToExtMap; + } + + +} diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java index fa0a090fdc..0fd686da93 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java @@ -1,7 +1,7 @@ /* * Autopsy Forensic Browser * - * Copyright 2011-2015 Basis Technology Corp. + * Copyright 2011-2016 Basis Technology Corp. * Contact: carrier sleuthkit org * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java index 062f4e175b..f2b9039ebc 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java @@ -19,6 +19,7 @@ package org.sleuthkit.autopsy.modules.fileextmismatch; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -29,6 +30,7 @@ import java.util.logging.Level; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import org.openide.util.io.NbObjectInputStream; import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.PlatformUtil; import org.sleuthkit.autopsy.coreutils.XMLUtil; @@ -90,7 +92,15 @@ class FileExtMismatchXML { HashMap sigTypeToExtMap = new HashMap<>(); File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH); if (serializedFile.exists()) { - + try { + try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(serializedFile))) { + FileExtMismatchSettings fileExtMismatchSettings = (FileExtMismatchSettings) in.readObject(); + return fileExtMismatchSettings.getSigTypeToExtMap(); + } + } catch (IOException | ClassNotFoundException ex) { + throw new FileExtMismatchException("Couldn't read serialized settings.", ex); + } + return sigTypeToExtMap; } try { @@ -177,6 +187,7 @@ class FileExtMismatchXML { } } rootEl.appendChild(sigEl); + } success = XMLUtil.saveDoc(FileExtMismatchXML.class, filePath, ENCODING, doc); @@ -187,5 +198,24 @@ class FileExtMismatchXML { } return success; } + + /** + * Used to translate more implementation-details-specific exceptions (which + * are logged by this class) into more generic exceptions for propagation to + * clients of the user-defined file types manager. + */ + static class FileExtMismatchException extends Exception { + + private static final long serialVersionUID = 1L; + + FileExtMismatchException(String message) { + super(message); + } + + FileExtMismatchException(String message, Throwable throwable) { + super(message, throwable); + } + } + } From 3d84bf1dfb70087b7efb0a9ba7a2be1a7f21ef8c Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Fri, 11 Mar 2016 09:02:50 -0500 Subject: [PATCH 03/14] Added saving of serialization, working towards exceptions catching. --- .../AddFileExtensionAction.java | 25 +++++++-- .../fileextmismatch/FileExtMismatchXML.java | 54 ++++--------------- 2 files changed, 32 insertions(+), 47 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java index 7be2e30983..2ad7398b8a 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.HashMap; import javax.swing.AbstractAction; import javax.swing.JOptionPane; +import org.openide.util.Exceptions; /** * Do the context menu action for adding a new filename extension to the @@ -43,19 +44,35 @@ class AddFileExtensionAction extends AbstractAction { @Override public void actionPerformed(ActionEvent event) { - HashMap editableMap = FileExtMismatchXML.getDefault().load(); + HashMap editableMap; + try { + editableMap = FileExtMismatchXML.getDefault().load(); + } catch (FileExtMismatchXML.FileExtMismatchException ex) { + JOptionPane.showMessageDialog(null, + NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"), + NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), + JOptionPane.ERROR_MESSAGE); + return; + } ArrayList editedExtensions = new ArrayList<>(Arrays.asList(editableMap.get(mimeTypeStr))); editedExtensions.add(extStr); // Old array will be replaced by new array for this key editableMap.put(mimeTypeStr, editedExtensions.toArray(new String[0])); - if (!FileExtMismatchXML.getDefault().save(editableMap)) { - //error + try { + if (!FileExtMismatchXML.getDefault().save(editableMap)) { + //error + JOptionPane.showMessageDialog(null, + NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"), + NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), + JOptionPane.ERROR_MESSAGE); + } // else //in the future we might want to update the statusbar to give feedback to the user + } catch (FileExtMismatchXML.FileExtMismatchException ex) { JOptionPane.showMessageDialog(null, NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), JOptionPane.ERROR_MESSAGE); - } // else //in the future we might want to update the statusbar to give feedback to the user + } } } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java index f2b9039ebc..10f6c03da7 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java @@ -20,6 +20,7 @@ package org.sleuthkit.autopsy.modules.fileextmismatch; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -31,6 +32,7 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.openide.util.io.NbObjectInputStream; +import org.openide.util.io.NbObjectOutputStream; import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.PlatformUtil; import org.sleuthkit.autopsy.coreutils.XMLUtil; @@ -88,7 +90,7 @@ class FileExtMismatchXML { * * @return Loaded hash map or null on error or null if data does not exist */ - public HashMap load() { + public HashMap load() throws FileExtMismatchException { HashMap sigTypeToExtMap = new HashMap<>(); File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH); if (serializedFile.exists()) { @@ -100,7 +102,6 @@ class FileExtMismatchXML { } catch (IOException | ClassNotFoundException ex) { throw new FileExtMismatchException("Couldn't read serialized settings.", ex); } - return sigTypeToExtMap; } try { @@ -157,48 +158,16 @@ class FileExtMismatchXML { * * @return Loaded hash map or null on error or null if data does not exist */ - public boolean save(HashMap sigTypeToExtMap) { - boolean success; - - DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); - - try { - DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); - Document doc = docBuilder.newDocument(); - - Element rootEl = doc.createElement(ROOT_EL); - doc.appendChild(rootEl); - - ArrayList mimeTypeList = new ArrayList<>(sigTypeToExtMap.keySet()); - Collections.sort(mimeTypeList); - - for (String mimeType : mimeTypeList) { - Element sigEl = doc.createElement(SIG_EL); - sigEl.setAttribute(SIG_MIMETYPE_ATTR, mimeType.toLowerCase()); - - String[] extArray = sigTypeToExtMap.get(mimeType); - if (extArray != null) { - ArrayList extList = new ArrayList<>(Arrays.asList(extArray)); - Collections.sort(extList); - for (String ext : extList) { - Element extEl = doc.createElement(EXT_EL); - extEl.setTextContent(ext.toLowerCase()); - sigEl.appendChild(extEl); - } - } - rootEl.appendChild(sigEl); - - } - - success = XMLUtil.saveDoc(FileExtMismatchXML.class, filePath, ENCODING, doc); - - } catch (ParserConfigurationException e) { - logger.log(Level.SEVERE, "Error saving keyword list: can't initialize parser.", e); //NON-NLS - success = false; + public boolean save(HashMap sigTypeToExtMap) throws FileExtMismatchException { + try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DEFAULT_SERIALIZED_FILE_PATH))) { + FileExtMismatchSettings settings = new FileExtMismatchSettings(sigTypeToExtMap); + out.writeObject(settings); + return true; + } catch (IOException ex) { + throw new FileExtMismatchException(String.format("Failed to write settings to %s", DEFAULT_SERIALIZED_FILE_PATH), ex); } - return success; } - + /** * Used to translate more implementation-details-specific exceptions (which * are logged by this class) into more generic exceptions for propagation to @@ -217,5 +186,4 @@ class FileExtMismatchXML { } } - } From f1061c6361d662846d55360c01aeb4025ef655d7 Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Fri, 11 Mar 2016 10:13:25 -0500 Subject: [PATCH 04/14] Updates to error messages --- .../modules/fileextmismatch/Bundle.properties | 3 +- ...ExtMismatchContextMenuActionsProvider.java | 37 +++++++++------ .../FileExtMismatchIngestModule.java | 7 ++- .../FileExtMismatchSettingsPanel.java | 47 ++++++++++++++----- 4 files changed, 65 insertions(+), 29 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties index 385bc66925..76895be76a 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties @@ -1,7 +1,8 @@ OpenIDE-Module-Name=FileExtMismatch OptionsCategory_Name_FileExtMismatchOptions=File Extension Mismatch OptionsCategory_FileExtMismatch=File Extension Mismatch -AddFileExtensionAction.msgDlg.msg=Writing XML configuration file failed. +AddFileExtensionAction.msgDlg.msg=Writing configuration file failed. +AddFileExtensionAction.msgDlg.msg2=Loading configuration file failed. AddFileExtensionAction.msgDlg.title=Add Mismatch Extension Error AddFileExtensionAction.extHeaderLbl.text=Allowed Extensions for FileExtMismatchConfigPanel.name.text=Advanced File Extension Mismatch Configuration diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java index 005e7fba50..6684093e1a 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java @@ -25,6 +25,8 @@ import java.util.HashMap; import java.util.List; import java.util.logging.Level; import javax.swing.Action; +import javax.swing.JOptionPane; +import org.openide.util.Exceptions; import org.openide.util.Lookup; import org.openide.util.NbBundle; import org.openide.util.Utilities; @@ -71,14 +73,14 @@ public class FileExtMismatchContextMenuActionsProvider implements ContextMenuAct } if (af != null) { - int i = af.getName().lastIndexOf("."); - if ((i > -1) && ((i + 1) < af.getName().length())) { - extStr = af.getName().substring(i + 1).toLowerCase(); - } - mimeTypeStr = af.getMIMEType(); - if(mimeTypeStr == null) { - mimeTypeStr = ""; - } + int i = af.getName().lastIndexOf("."); + if ((i > -1) && ((i + 1) < af.getName().length())) { + extStr = af.getName().substring(i + 1).toLowerCase(); + } + mimeTypeStr = af.getMIMEType(); + if (mimeTypeStr == null) { + mimeTypeStr = ""; + } if (!extStr.isEmpty() && !mimeTypeStr.isEmpty()) { // Limit max size so the context window doesn't get ridiculously wide @@ -94,13 +96,20 @@ public class FileExtMismatchContextMenuActionsProvider implements ContextMenuAct actions.add(new AddFileExtensionAction(menuItemStr, extStr, mimeTypeStr)); // Check if already added - HashMap editableMap = FileExtMismatchXML.getDefault().load(); - ArrayList editedExtensions = new ArrayList<>(Arrays.asList(editableMap.get(mimeTypeStr))); - if (editedExtensions.contains(extStr)) { - // Informs the user that they have already added this extension to this MIME type - actions.get(0).setEnabled(false); + HashMap editableMap; + try { + editableMap = FileExtMismatchXML.getDefault().load(); + ArrayList editedExtensions = new ArrayList<>(Arrays.asList(editableMap.get(mimeTypeStr))); + if (editedExtensions.contains(extStr)) { + // Informs the user that they have already added this extension to this MIME type + actions.get(0).setEnabled(false); + } + } catch (FileExtMismatchXML.FileExtMismatchException ex) { + JOptionPane.showMessageDialog(null, + NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg2"), + NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), + JOptionPane.ERROR_MESSAGE); } - } } } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java index a3b54e8d41..b2544455bc 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java @@ -23,6 +23,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.logging.Level; +import org.openide.util.Exceptions; import org.openide.util.NbBundle; import org.sleuthkit.autopsy.casemodule.Case; import org.sleuthkit.autopsy.casemodule.services.Blackboard; @@ -92,7 +93,11 @@ public class FileExtMismatchIngestModule implements FileIngestModule { refCounter.incrementAndGet(jobId); FileExtMismatchXML xmlLoader = FileExtMismatchXML.getDefault(); - SigTypeToExtMap = xmlLoader.load(); + try { + SigTypeToExtMap = xmlLoader.load(); + } catch (FileExtMismatchXML.FileExtMismatchException ex) { + throw new IngestModuleException("Could not load file extension mismatch configurations.", ex); + } try { this.detector = new FileTypeDetector(); } catch (FileTypeDetector.FileTypeDetectorInitException ex) { diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java index 0fd686da93..2aebfcc21c 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java @@ -29,6 +29,7 @@ import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.AbstractTableModel; +import org.openide.util.Exceptions; import org.sleuthkit.autopsy.ingest.IngestModuleGlobalSettingsPanel; import org.openide.util.NbBundle; import org.sleuthkit.autopsy.coreutils.Logger; @@ -547,15 +548,25 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel @Override public void saveSettings() { - if (FileExtMismatchXML.getDefault().save(editableMap)) { - mimeErrLabel.setText(" "); - mimeRemoveErrLabel.setText(" "); - extRemoveErrLabel.setText(" "); - extErrorLabel.setText(" "); + try { + if (FileExtMismatchXML.getDefault().save(editableMap)) { + mimeErrLabel.setText(" "); + mimeRemoveErrLabel.setText(" "); + extRemoveErrLabel.setText(" "); + extErrorLabel.setText(" "); - saveMsgLabel.setText(NbBundle.getMessage(this.getClass(), "FileExtMismatchConfigPanel.store.msg")); - saveButton.setEnabled(false); - } else { + saveMsgLabel.setText(NbBundle.getMessage(this.getClass(), "FileExtMismatchConfigPanel.store.msg")); + saveButton.setEnabled(false); + } else { + //error + JOptionPane.showMessageDialog(this, + NbBundle.getMessage(this.getClass(), + "FileExtMismatchConfigPanel.store.msgDlg.msg"), + NbBundle.getMessage(this.getClass(), + "FileExtMismatchConfigPanel.save.msgDlg.title"), + JOptionPane.ERROR_MESSAGE); + } + } catch (FileExtMismatchXML.FileExtMismatchException ex) { //error JOptionPane.showMessageDialog(this, NbBundle.getMessage(this.getClass(), @@ -568,11 +579,21 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel @Override public void load() { - // Load the XML into a buffer that the user can modify. They can choose - // to save it back to the file after making changes. - editableMap = FileExtMismatchXML.getDefault().load(); - updateMimeList(); - updateExtList(); + try { + // Load the configuration into a buffer that the user can modify. They can choose + // to save it back to the file after making changes. + editableMap = FileExtMismatchXML.getDefault().load(); + updateMimeList(); + updateExtList(); + } catch (FileExtMismatchXML.FileExtMismatchException ex) { + //error + JOptionPane.showMessageDialog(this, + NbBundle.getMessage(this.getClass(), + "FileExtMismatchConfigPanel.store.msgDlg.msg2"), + NbBundle.getMessage(this.getClass(), + "FileExtMismatchConfigPanel.save.msgDlg.title"), + JOptionPane.ERROR_MESSAGE); + } } @Override From 75f4d3865c7044634e8dcd5b24861e514c18c873 Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Fri, 11 Mar 2016 11:04:50 -0500 Subject: [PATCH 05/14] Updated error handling behavior --- .../fileextmismatch/FileExtMismatchSettingsPanel.java | 2 +- .../modules/fileextmismatch/FileExtMismatchXML.java | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java index 2aebfcc21c..92788d2308 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java @@ -589,7 +589,7 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel //error JOptionPane.showMessageDialog(this, NbBundle.getMessage(this.getClass(), - "FileExtMismatchConfigPanel.store.msgDlg.msg2"), + "AddFileExtensionAction.msgDlg.msg2"), NbBundle.getMessage(this.getClass(), "FileExtMismatchConfigPanel.save.msgDlg.title"), JOptionPane.ERROR_MESSAGE); diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java index 10f6c03da7..fc3cc01336 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java @@ -91,7 +91,8 @@ class FileExtMismatchXML { * @return Loaded hash map or null on error or null if data does not exist */ public HashMap load() throws FileExtMismatchException { - HashMap sigTypeToExtMap = new HashMap<>(); + throw new FileExtMismatchException(""); + /*HashMap sigTypeToExtMap = new HashMap<>(); File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH); if (serializedFile.exists()) { try { @@ -147,7 +148,7 @@ class FileExtMismatchXML { logger.log(Level.SEVERE, "Error loading config file.", e); //NON-NLS return null; } - return sigTypeToExtMap; + return sigTypeToExtMap;*/ } /** @@ -159,13 +160,14 @@ class FileExtMismatchXML { * @return Loaded hash map or null on error or null if data does not exist */ public boolean save(HashMap sigTypeToExtMap) throws FileExtMismatchException { - try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DEFAULT_SERIALIZED_FILE_PATH))) { + throw new FileExtMismatchException(""); + /*try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DEFAULT_SERIALIZED_FILE_PATH))) { FileExtMismatchSettings settings = new FileExtMismatchSettings(sigTypeToExtMap); out.writeObject(settings); return true; } catch (IOException ex) { throw new FileExtMismatchException(String.format("Failed to write settings to %s", DEFAULT_SERIALIZED_FILE_PATH), ex); - } + }*/ } /** From aaafce3eab1bb3cfb8a6cbfce54dee85540c5868 Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Fri, 11 Mar 2016 11:18:58 -0500 Subject: [PATCH 06/14] Finished error messaging protocol --- .../autopsy/modules/fileextmismatch/Bundle.properties | 2 +- .../fileextmismatch/FileExtMismatchSettingsPanel.java | 5 +++-- .../modules/fileextmismatch/FileExtMismatchXML.java | 11 ++++------- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties index 76895be76a..9681229663 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties @@ -21,7 +21,7 @@ FileExtMismatchConfigPanel.removeExtButton.noneSelected=No extension selected\! FileExtMismatchConfigPanel.removeExtButton.noMimeTypeSelected=No MIME type selected\! FileExtMismatchConfigPanel.removeExtButton.deleted=Extension {0} deleted. FileExtMismatchConfigPanel.store.msg=Saved. -FileExtMismatchConfigPanel.store.msgDlg.msg=Writing XML configuration file failed. +FileExtMismatchConfigPanel.store.msgDlg.msg=Writing configuration file failed. FileExtMismatchConfigPanel.save.msgDlg.title=Save Error FileExtMismatchConfigPanel.ok.confDlg.msg=Would you like to save configuration changes? FileExtMismatchConfigPanel.confDlg.title=Unsaved Changes diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java index 92788d2308..2eea561575 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java @@ -583,8 +583,7 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel // Load the configuration into a buffer that the user can modify. They can choose // to save it back to the file after making changes. editableMap = FileExtMismatchXML.getDefault().load(); - updateMimeList(); - updateExtList(); + } catch (FileExtMismatchXML.FileExtMismatchException ex) { //error JOptionPane.showMessageDialog(this, @@ -594,6 +593,8 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel "FileExtMismatchConfigPanel.save.msgDlg.title"), JOptionPane.ERROR_MESSAGE); } + updateMimeList(); + updateExtList(); } @Override diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java index fc3cc01336..630bc6add5 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java @@ -90,9 +90,7 @@ class FileExtMismatchXML { * * @return Loaded hash map or null on error or null if data does not exist */ - public HashMap load() throws FileExtMismatchException { - throw new FileExtMismatchException(""); - /*HashMap sigTypeToExtMap = new HashMap<>(); + public HashMap load() throws FileExtMismatchException {HashMap sigTypeToExtMap = new HashMap<>(); File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH); if (serializedFile.exists()) { try { @@ -148,7 +146,7 @@ class FileExtMismatchXML { logger.log(Level.SEVERE, "Error loading config file.", e); //NON-NLS return null; } - return sigTypeToExtMap;*/ + return sigTypeToExtMap; } /** @@ -160,14 +158,13 @@ class FileExtMismatchXML { * @return Loaded hash map or null on error or null if data does not exist */ public boolean save(HashMap sigTypeToExtMap) throws FileExtMismatchException { - throw new FileExtMismatchException(""); - /*try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DEFAULT_SERIALIZED_FILE_PATH))) { + try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DEFAULT_SERIALIZED_FILE_PATH))) { FileExtMismatchSettings settings = new FileExtMismatchSettings(sigTypeToExtMap); out.writeObject(settings); return true; } catch (IOException ex) { throw new FileExtMismatchException(String.format("Failed to write settings to %s", DEFAULT_SERIALIZED_FILE_PATH), ex); - }*/ + } } /** From f42a535cbd0794e8934708636010d31c35a4cbdb Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Tue, 5 Apr 2016 14:31:09 -0400 Subject: [PATCH 07/14] Moved writing of settings to FileExtMismatchSettings --- .../AddFileExtensionAction.java | 9 +- ...ExtMismatchContextMenuActionsProvider.java | 4 +- .../FileExtMismatchDetectorModuleFactory.java | 2 +- .../FileExtMismatchIngestModule.java | 5 +- .../FileExtMismatchSettings.java | 160 ++++++++++++++- .../FileExtMismatchSettingsPanel.java | 8 +- .../fileextmismatch/FileExtMismatchXML.java | 188 ------------------ 7 files changed, 169 insertions(+), 207 deletions(-) delete mode 100644 Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java index 2ad7398b8a..e6e75bdd6c 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java @@ -25,7 +25,6 @@ import java.util.Arrays; import java.util.HashMap; import javax.swing.AbstractAction; import javax.swing.JOptionPane; -import org.openide.util.Exceptions; /** * Do the context menu action for adding a new filename extension to the @@ -46,8 +45,8 @@ class AddFileExtensionAction extends AbstractAction { public void actionPerformed(ActionEvent event) { HashMap editableMap; try { - editableMap = FileExtMismatchXML.getDefault().load(); - } catch (FileExtMismatchXML.FileExtMismatchException ex) { + editableMap = FileExtMismatchSettings.readSettings().getSigTypeToExtMap(); + } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { JOptionPane.showMessageDialog(null, NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), @@ -61,14 +60,14 @@ class AddFileExtensionAction extends AbstractAction { editableMap.put(mimeTypeStr, editedExtensions.toArray(new String[0])); try { - if (!FileExtMismatchXML.getDefault().save(editableMap)) { + if (!FileExtMismatchSettings.writeSettings(new FileExtMismatchSettings(editableMap))) { //error JOptionPane.showMessageDialog(null, NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), JOptionPane.ERROR_MESSAGE); } // else //in the future we might want to update the statusbar to give feedback to the user - } catch (FileExtMismatchXML.FileExtMismatchException ex) { + } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { JOptionPane.showMessageDialog(null, NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java index 6684093e1a..afffbbcce2 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java @@ -98,13 +98,13 @@ public class FileExtMismatchContextMenuActionsProvider implements ContextMenuAct // Check if already added HashMap editableMap; try { - editableMap = FileExtMismatchXML.getDefault().load(); + editableMap = FileExtMismatchSettings.readSettings().getSigTypeToExtMap(); ArrayList editedExtensions = new ArrayList<>(Arrays.asList(editableMap.get(mimeTypeStr))); if (editedExtensions.contains(extStr)) { // Informs the user that they have already added this extension to this MIME type actions.get(0).setEnabled(false); } - } catch (FileExtMismatchXML.FileExtMismatchException ex) { + } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { JOptionPane.showMessageDialog(null, NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg2"), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchDetectorModuleFactory.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchDetectorModuleFactory.java index b303fd770f..0840f24296 100755 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchDetectorModuleFactory.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchDetectorModuleFactory.java @@ -99,7 +99,7 @@ public class FileExtMismatchDetectorModuleFactory extends IngestModuleFactoryAda assert settings instanceof FileExtMismatchDetectorModuleSettings; if (!(settings instanceof FileExtMismatchDetectorModuleSettings)) { throw new IllegalArgumentException(NbBundle.getMessage(this.getClass(), - "FileExtMismatchDetectorModuleFactory.createFileIngestModule.exception.msg")); + "FileExtMismatchDetectorModuleFactory.getIngestJobSettingsPanel.exception.msg")); } return new FileExtMismatchIngestModule((FileExtMismatchDetectorModuleSettings) settings); } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java index b2544455bc..664b92e078 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java @@ -92,10 +92,9 @@ public class FileExtMismatchIngestModule implements FileIngestModule { jobId = context.getJobId(); refCounter.incrementAndGet(jobId); - FileExtMismatchXML xmlLoader = FileExtMismatchXML.getDefault(); try { - SigTypeToExtMap = xmlLoader.load(); - } catch (FileExtMismatchXML.FileExtMismatchException ex) { + SigTypeToExtMap = FileExtMismatchSettings.readSettings().getSigTypeToExtMap(); + } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { throw new IngestModuleException("Could not load file extension mismatch configurations.", ex); } try { diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java index b798f860f0..6a0799b7bf 100755 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java @@ -18,13 +18,56 @@ */ package org.sleuthkit.autopsy.modules.fileextmismatch; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; import java.io.Serializable; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; +import java.util.logging.Level; +import org.openide.util.io.NbObjectInputStream; +import org.openide.util.io.NbObjectOutputStream; +import org.sleuthkit.autopsy.coreutils.Logger; +import org.sleuthkit.autopsy.coreutils.PlatformUtil; +import org.sleuthkit.autopsy.coreutils.XMLUtil; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +/** + * Serialization settings for file extension mismatch. Contains static methods + * for reading and writing settings, and instances of this class are what is + * written to the serialized file. + */ class FileExtMismatchSettings implements Serializable { - private static final long serialVersionUID = 1L; - private final HashMap sigTypeToExtMap; + private static final long serialVersionUID = 1L; + private HashMap sigTypeToExtMap; + private static final Logger logger = Logger.getLogger(FileExtMismatchSettings.class.getName()); + private static final String SIG_EL = "signature"; //NON-NLS + private static final String EXT_EL = "ext"; //NON-NLS + private static final String SIG_MIMETYPE_ATTR = "mimetype"; //NON-NLS + + private static final String DEFAULT_CONFIG_FILE_NAME = "mismatch_config.xml"; //NON-NLS + private static final String FILTER_CONFIG_FILE = PlatformUtil.getUserConfigDirectory() + File.separator + DEFAULT_CONFIG_FILE_NAME; + private static final String DEFAULT_SERIALIZED_FILE_NAME = "mismatch_config.settings"; + private static final String DEFAULT_SERIALIZED_FILE_PATH = PlatformUtil.getUserConfigDirectory() + File.separator + DEFAULT_SERIALIZED_FILE_NAME; + + static { + try { + boolean extracted = PlatformUtil.extractResourceToUserConfigDir(FileExtMismatchSettings.class, DEFAULT_CONFIG_FILE_NAME, false); + } catch (IOException ex) { + logger.log(Level.SEVERE, "Error copying default mismatch configuration to user dir ", ex); //NON-NLS + } + } + + /** + * Makes a settings object based on given sig type map + * + * @param sigTypeToExtMap + */ FileExtMismatchSettings(HashMap sigTypeToExtMap) { this.sigTypeToExtMap = sigTypeToExtMap; } @@ -35,6 +78,115 @@ class FileExtMismatchSettings implements Serializable { HashMap getSigTypeToExtMap() { return sigTypeToExtMap; } - - + + /** + * Sets the signature to extension map for this settings. + */ + public void setSigTypeToExtMap(HashMap sigTypeToExtMap) { + this.sigTypeToExtMap = sigTypeToExtMap; + } + + /** + * Load and parse the settings + * + * @return Loaded hash map or null on error or null if data does not exist + */ + public static synchronized FileExtMismatchSettings readSettings() throws FileExtMismatchSettingsException { + HashMap sigTypeToExtMap = new HashMap<>(); + File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH); + //Tries reading the serialized file first, as this is the prioritized settings. + if (serializedFile.exists()) { + try { + try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(serializedFile))) { + FileExtMismatchSettings fileExtMismatchSettings = (FileExtMismatchSettings) in.readObject(); + return fileExtMismatchSettings; + } + } catch (IOException | ClassNotFoundException ex) { + throw new FileExtMismatchSettingsException("Couldn't read serialized settings.", ex); + } + } + + //Next tries to read the xml file if the serialized file did not exist + File xmlFile = new File(FILTER_CONFIG_FILE); + if (xmlFile.exists()) { + try { + final Document doc = XMLUtil.loadDoc(FileExtMismatchSettings.class, FILTER_CONFIG_FILE); + if (doc == null) { + return new FileExtMismatchSettings(sigTypeToExtMap); + } + + Element root = doc.getDocumentElement(); + if (root == null) { + throw new FileExtMismatchSettingsException("Error loading config file: invalid file format (bad root)."); //NON-NLS + } + + NodeList sigNList = root.getElementsByTagName(SIG_EL); + final int numSigs = sigNList.getLength(); + + if (numSigs == 0) { + throw new FileExtMismatchSettingsException("Error loading config file: invalid file format (bad signature)."); //NON-NLS + } + + for (int sigIndex = 0; sigIndex < numSigs; ++sigIndex) { + Element sigEl = (Element) sigNList.item(sigIndex); + final String mimetype = sigEl.getAttribute(SIG_MIMETYPE_ATTR); + + NodeList extNList = sigEl.getElementsByTagName(EXT_EL); + final int numExts = extNList.getLength(); + + if (numExts != 0) { + List extStrings = new ArrayList<>(); + for (int extIndex = 0; extIndex < numExts; ++extIndex) { + Element extEl = (Element) extNList.item(extIndex); + extStrings.add(extEl.getTextContent()); + } + String[] sarray = extStrings.toArray(new String[0]); + sigTypeToExtMap.put(mimetype, sarray); + } else { + sigTypeToExtMap.put(mimetype, null); //ok to have an empty type (the ingest module will not use it) + } + } + + } catch (Exception e) { + logger.log(Level.SEVERE, "Error loading config file.", e); //NON-NLS + return null; + } + } + return new FileExtMismatchSettings(sigTypeToExtMap); + } + + /** + * Save XML to filePath, overwriting it if it already exists + * + * @param sigTypeToExtMap String arrays of extensions mapped to each string + * mimetype. + * + * @return Loaded hash map or null on error or null if data does not exist + */ + public static synchronized boolean writeSettings(FileExtMismatchSettings settings) throws FileExtMismatchSettingsException { + try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DEFAULT_SERIALIZED_FILE_PATH))) { + out.writeObject(settings); + return true; + } catch (IOException ex) { + throw new FileExtMismatchSettingsException(String.format("Failed to write settings to %s", DEFAULT_SERIALIZED_FILE_PATH), ex); + } + } + + /** + * Used to translate more implementation-details-specific exceptions (which + * are logged by this class) into more generic exceptions for propagation to + * clients of the user-defined file types manager. + */ + static class FileExtMismatchSettingsException extends Exception { + + private static final long serialVersionUID = 1L; + + FileExtMismatchSettingsException(String message) { + super(message); + } + + FileExtMismatchSettingsException(String message, Throwable throwable) { + super(message, throwable); + } + } } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java index 2eea561575..405231a196 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java @@ -549,7 +549,7 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel @Override public void saveSettings() { try { - if (FileExtMismatchXML.getDefault().save(editableMap)) { + if (FileExtMismatchSettings.writeSettings(new FileExtMismatchSettings(editableMap))) { mimeErrLabel.setText(" "); mimeRemoveErrLabel.setText(" "); extRemoveErrLabel.setText(" "); @@ -566,7 +566,7 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel "FileExtMismatchConfigPanel.save.msgDlg.title"), JOptionPane.ERROR_MESSAGE); } - } catch (FileExtMismatchXML.FileExtMismatchException ex) { + } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { //error JOptionPane.showMessageDialog(this, NbBundle.getMessage(this.getClass(), @@ -582,9 +582,9 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel try { // Load the configuration into a buffer that the user can modify. They can choose // to save it back to the file after making changes. - editableMap = FileExtMismatchXML.getDefault().load(); + editableMap = FileExtMismatchSettings.readSettings().getSigTypeToExtMap(); - } catch (FileExtMismatchXML.FileExtMismatchException ex) { + } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { //error JOptionPane.showMessageDialog(this, NbBundle.getMessage(this.getClass(), diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java deleted file mode 100644 index 630bc6add5..0000000000 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchXML.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Autopsy Forensic Browser - * - * Copyright 2011-2014 Basis Technology Corp. - * Contact: carrier sleuthkit 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.modules.fileextmismatch; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.logging.Level; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import org.openide.util.io.NbObjectInputStream; -import org.openide.util.io.NbObjectOutputStream; -import org.sleuthkit.autopsy.coreutils.Logger; -import org.sleuthkit.autopsy.coreutils.PlatformUtil; -import org.sleuthkit.autopsy.coreutils.XMLUtil; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; - -/** - * Storage of file extension mismatch configuration, which maps mimetypes to - * allowable filename extensions. - */ -class FileExtMismatchXML { - - private static final Logger logger = Logger.getLogger(FileExtMismatchXML.class.getName()); - private static FileExtMismatchXML defaultInstance = null; - - private static final String ENCODING = "UTF-8"; //NON-NLS - private static final String XSDFILE = "MismatchConfigSchema.xsd"; //NON-NLS - - private static final String ROOT_EL = "mismatch_config"; //NON-NLS - private static final String SIG_EL = "signature"; //NON-NLS - private static final String EXT_EL = "ext"; //NON-NLS - private static final String SIG_MIMETYPE_ATTR = "mimetype"; //NON-NLS - - private static final String DEFAULT_CONFIG_FILE_NAME = "mismatch_config.xml"; //NON-NLS - private static final String DEFAULT_SERIALIZED_FILE_NAME = "mismatch_config.settings"; - private static final String DEFAULT_SERIALIZED_FILE_PATH = PlatformUtil.getUserConfigDirectory() + File.separator + DEFAULT_SERIALIZED_FILE_NAME; - - protected String filePath; - - FileExtMismatchXML(String filePath) { - this.filePath = filePath; - - try { - boolean extracted = PlatformUtil.extractResourceToUserConfigDir(FileExtMismatchXML.class, DEFAULT_CONFIG_FILE_NAME, false); - } catch (IOException ex) { - logger.log(Level.SEVERE, "Error copying default mismatch configuration to user dir ", ex); //NON-NLS - } - } - - /** - * Singleton provides default configuration from user's directory; user CAN - * modify this file. - */ - public static synchronized FileExtMismatchXML getDefault() { - if (defaultInstance == null) { - final String FILTER_CONFIG_FILE = PlatformUtil.getUserConfigDirectory() + File.separator + DEFAULT_CONFIG_FILE_NAME; - defaultInstance = new FileExtMismatchXML(FILTER_CONFIG_FILE); - } - return defaultInstance; - } - - /** - * Load and parse XML - * - * @return Loaded hash map or null on error or null if data does not exist - */ - public HashMap load() throws FileExtMismatchException {HashMap sigTypeToExtMap = new HashMap<>(); - File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH); - if (serializedFile.exists()) { - try { - try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(serializedFile))) { - FileExtMismatchSettings fileExtMismatchSettings = (FileExtMismatchSettings) in.readObject(); - return fileExtMismatchSettings.getSigTypeToExtMap(); - } - } catch (IOException | ClassNotFoundException ex) { - throw new FileExtMismatchException("Couldn't read serialized settings.", ex); - } - } - - try { - final Document doc = XMLUtil.loadDoc(FileExtMismatchXML.class, filePath); - if (doc == null) { - return null; - } - - Element root = doc.getDocumentElement(); - if (root == null) { - logger.log(Level.SEVERE, "Error loading config file: invalid file format (bad root)."); //NON-NLS - return null; - } - - NodeList sigNList = root.getElementsByTagName(SIG_EL); - final int numSigs = sigNList.getLength(); - - if (numSigs == 0) { - return null; - } - - for (int sigIndex = 0; sigIndex < numSigs; ++sigIndex) { - Element sigEl = (Element) sigNList.item(sigIndex); - final String mimetype = sigEl.getAttribute(SIG_MIMETYPE_ATTR); - - NodeList extNList = sigEl.getElementsByTagName(EXT_EL); - final int numExts = extNList.getLength(); - - if (numExts != 0) { - List extStrings = new ArrayList<>(); - for (int extIndex = 0; extIndex < numExts; ++extIndex) { - Element extEl = (Element) extNList.item(extIndex); - extStrings.add(extEl.getTextContent()); - } - String[] sarray = extStrings.toArray(new String[0]); - sigTypeToExtMap.put(mimetype, sarray); - } else { - sigTypeToExtMap.put(mimetype, null); //ok to have an empty type (the ingest module will not use it) - } - } - - } catch (Exception e) { - logger.log(Level.SEVERE, "Error loading config file.", e); //NON-NLS - return null; - } - return sigTypeToExtMap; - } - - /** - * Save XML to filePath, overwriting it if it already exists - * - * @param sigTypeToExtMap String arrays of extensions mapped to each string - * mimetype. - * - * @return Loaded hash map or null on error or null if data does not exist - */ - public boolean save(HashMap sigTypeToExtMap) throws FileExtMismatchException { - try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DEFAULT_SERIALIZED_FILE_PATH))) { - FileExtMismatchSettings settings = new FileExtMismatchSettings(sigTypeToExtMap); - out.writeObject(settings); - return true; - } catch (IOException ex) { - throw new FileExtMismatchException(String.format("Failed to write settings to %s", DEFAULT_SERIALIZED_FILE_PATH), ex); - } - } - - /** - * Used to translate more implementation-details-specific exceptions (which - * are logged by this class) into more generic exceptions for propagation to - * clients of the user-defined file types manager. - */ - static class FileExtMismatchException extends Exception { - - private static final long serialVersionUID = 1L; - - FileExtMismatchException(String message) { - super(message); - } - - FileExtMismatchException(String message, Throwable throwable) { - super(message, throwable); - } - } - -} From 1ec523febc0d023253d933cd60b6da38ac1028d6 Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Fri, 8 Apr 2016 14:43:30 -0400 Subject: [PATCH 08/14] Updated multiple protocols/semantics. --- .../AddFileExtensionAction.java | 35 ++++++++++--------- .../modules/fileextmismatch/Bundle.properties | 2 +- ...ExtMismatchContextMenuActionsProvider.java | 27 ++++++-------- .../FileExtMismatchIngestModule.java | 13 +++---- .../FileExtMismatchSettings.java | 33 +++++++++-------- .../FileExtMismatchSettingsPanel.java | 2 +- 6 files changed, 54 insertions(+), 58 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java index e6e75bdd6c..ac27fd6479 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java @@ -1,7 +1,7 @@ /* * Autopsy Forensic Browser * - * Copyright 2014 Basis Technology Corp. + * Copyright 2011-2016 Basis Technology Corp. * Contact: carrier sleuthkit org * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,41 +18,40 @@ */ package org.sleuthkit.autopsy.modules.fileextmismatch; -import org.openide.util.NbBundle; import java.awt.event.ActionEvent; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.logging.Level; import javax.swing.AbstractAction; import javax.swing.JOptionPane; +import org.openide.util.NbBundle; +import org.openide.util.NbBundle.Messages; +import org.sleuthkit.autopsy.coreutils.Logger; /** * Do the context menu action for adding a new filename extension to the - * mismatch list for the MIME type of the selected node. + * extension list for the MIME type. */ class AddFileExtensionAction extends AbstractAction { + private static final long serialVersionUID = 1L; - private String extStr; - private String mimeTypeStr; + private final String extStr; + private final String mimeTypeStr; + private final FileExtMismatchSettings settings; - public AddFileExtensionAction(String menuItemStr, String extStr, String mimeTypeStr) { + AddFileExtensionAction(String menuItemStr, String extStr, String mimeTypeStr, FileExtMismatchSettings settings) { super(menuItemStr); this.mimeTypeStr = mimeTypeStr; this.extStr = extStr; + this.settings = settings; } @Override + @Messages({"AddFileExtensionAction.writeError.message=Could not write file extension settings."}) public void actionPerformed(ActionEvent event) { HashMap editableMap; - try { - editableMap = FileExtMismatchSettings.readSettings().getSigTypeToExtMap(); - } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { - JOptionPane.showMessageDialog(null, - NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"), - NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), - JOptionPane.ERROR_MESSAGE); - return; - } + editableMap = settings.getMimeTypeToExtsMap(); ArrayList editedExtensions = new ArrayList<>(Arrays.asList(editableMap.get(mimeTypeStr))); editedExtensions.add(extStr); @@ -63,15 +62,17 @@ class AddFileExtensionAction extends AbstractAction { if (!FileExtMismatchSettings.writeSettings(new FileExtMismatchSettings(editableMap))) { //error JOptionPane.showMessageDialog(null, - NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"), + Bundle.AddFileExtensionAction_writeError_message(), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), JOptionPane.ERROR_MESSAGE); + Logger.getLogger(this.getClass().getName()).log(Level.WARNING, Bundle.AddFileExtensionAction_writeError_message()); } // else //in the future we might want to update the statusbar to give feedback to the user } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { JOptionPane.showMessageDialog(null, - NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"), + Bundle.AddFileExtensionAction_writeError_message(), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), JOptionPane.ERROR_MESSAGE); + Logger.getLogger(this.getClass().getName()).log(Level.WARNING, Bundle.AddFileExtensionAction_writeError_message(), ex); } } } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties index 32bf210e5e..8d62766e8c 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties @@ -2,7 +2,7 @@ OpenIDE-Module-Name=FileExtMismatch OptionsCategory_Name_FileExtMismatchOptions=File Extension Mismatch OptionsCategory_FileExtMismatch=File Extension Mismatch AddFileExtensionAction.msgDlg.msg=Writing configuration file failed. -AddFileExtensionAction.msgDlg.msg2=Loading configuration file failed. +AddFileExtensionAction.msgDlg.msg2=File extension mismatch settings could not be read, extensions update not available. AddFileExtensionAction.msgDlg.title=Add Mismatch Extension Error AddFileExtensionAction.extHeaderLbl.text=Allowed Extensions for FileExtMismatchConfigPanel.addExtButton.errLabel.empty=Extension text is empty\! diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java index afffbbcce2..d21ca8ec69 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java @@ -1,7 +1,7 @@ /* * Autopsy Forensic Browser * - * Copyright 2014 Basis Technology Corp. + * Copyright 2011-2016 Basis Technology Corp. * Contact: carrier sleuthkit org * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,22 +26,19 @@ import java.util.List; import java.util.logging.Level; import javax.swing.Action; import javax.swing.JOptionPane; -import org.openide.util.Exceptions; -import org.openide.util.Lookup; import org.openide.util.NbBundle; import org.openide.util.Utilities; import org.openide.util.lookup.ServiceProvider; import org.sleuthkit.autopsy.corecomponentinterfaces.ContextMenuActionsProvider; import org.sleuthkit.autopsy.coreutils.Logger; +import org.sleuthkit.autopsy.datamodel.BlackboardArtifactNode; import org.sleuthkit.autopsy.ingest.IngestManager; import org.sleuthkit.datamodel.AbstractFile; import org.sleuthkit.datamodel.BlackboardArtifact; -import org.sleuthkit.datamodel.BlackboardAttribute; -import org.sleuthkit.datamodel.TskCoreException; /** * This creates a single context menu item for adding a new filename extension - * to the mismatch list for the MIME type of the selected node. + * to the extension list for the MIME type of the selected node. */ @ServiceProvider(service = ContextMenuActionsProvider.class) public class FileExtMismatchContextMenuActionsProvider implements ContextMenuActionsProvider { @@ -53,24 +50,20 @@ public class FileExtMismatchContextMenuActionsProvider implements ContextMenuAct // Ignore if file ingest is in progress. if (!IngestManager.getInstance().isIngestRunning()) { - final Collection selectedArts = Utilities.actionsGlobalContext().lookupAll(BlackboardArtifact.class); + final Collection selectedArts = Utilities.actionsGlobalContext().lookupAll(BlackboardArtifactNode.class); // Prevent multiselect if (selectedArts.size() == 1) { - for (BlackboardArtifact nodeArt : selectedArts) { + for (BlackboardArtifactNode artifactNode : selectedArts) { + BlackboardArtifact nodeArt = artifactNode.getLookup().lookup(BlackboardArtifact.class); // Only for mismatch results if (nodeArt.getArtifactTypeName().equals("TSK_EXT_MISMATCH_DETECTED")) { //NON-NLS String mimeTypeStr = ""; String extStr = ""; - AbstractFile af = null; - try { - af = nodeArt.getSleuthkitCase().getAbstractFileById(nodeArt.getObjectID()); - } catch (TskCoreException ex) { - Logger.getLogger(FileExtMismatchContextMenuActionsProvider.class.getName()).log(Level.SEVERE, "Error getting file by id", ex); //NON-NLS - } + AbstractFile af = artifactNode.getLookup().lookup(AbstractFile.class); if (af != null) { int i = af.getName().lastIndexOf("."); @@ -93,12 +86,13 @@ public class FileExtMismatchContextMenuActionsProvider implements ContextMenuAct String menuItemStr = NbBundle.getMessage(this.getClass(), "FileExtMismatchContextMenuActionsProvider.menuItemStr", extStr, mimeTypeStr); - actions.add(new AddFileExtensionAction(menuItemStr, extStr, mimeTypeStr)); // Check if already added HashMap editableMap; try { - editableMap = FileExtMismatchSettings.readSettings().getSigTypeToExtMap(); + FileExtMismatchSettings settings = FileExtMismatchSettings.readSettings(); + editableMap = settings.getMimeTypeToExtsMap(); + actions.add(new AddFileExtensionAction(menuItemStr, extStr, mimeTypeStr, settings)); ArrayList editedExtensions = new ArrayList<>(Arrays.asList(editableMap.get(mimeTypeStr))); if (editedExtensions.contains(extStr)) { // Informs the user that they have already added this extension to this MIME type @@ -109,6 +103,7 @@ public class FileExtMismatchContextMenuActionsProvider implements ContextMenuAct NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg2"), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), JOptionPane.ERROR_MESSAGE); + Logger.getLogger(this.getClass().getName()).log(Level.WARNING, NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg2"), ex); } } } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java index 664b92e078..ab83419d9c 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java @@ -23,8 +23,8 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.logging.Level; -import org.openide.util.Exceptions; import org.openide.util.NbBundle; +import org.openide.util.NbBundle.Messages; import org.sleuthkit.autopsy.casemodule.Case; import org.sleuthkit.autopsy.casemodule.services.Blackboard; import org.sleuthkit.autopsy.coreutils.Logger; @@ -32,9 +32,9 @@ import org.sleuthkit.autopsy.coreutils.MessageNotifyUtil; import org.sleuthkit.autopsy.ingest.FileIngestModule; import org.sleuthkit.autopsy.ingest.IngestJobContext; import org.sleuthkit.autopsy.ingest.IngestMessage; +import org.sleuthkit.autopsy.ingest.IngestModuleReferenceCounter; import org.sleuthkit.autopsy.ingest.IngestServices; import org.sleuthkit.autopsy.ingest.ModuleDataEvent; -import org.sleuthkit.autopsy.ingest.IngestModuleReferenceCounter; import org.sleuthkit.autopsy.modules.filetypeid.FileTypeDetector; import org.sleuthkit.datamodel.AbstractFile; import org.sleuthkit.datamodel.BlackboardArtifact; @@ -52,7 +52,7 @@ public class FileExtMismatchIngestModule implements FileIngestModule { private static final Logger logger = Logger.getLogger(FileExtMismatchIngestModule.class.getName()); private final IngestServices services = IngestServices.getInstance(); private final FileExtMismatchDetectorModuleSettings settings; - private HashMap SigTypeToExtMap = new HashMap<>(); + private HashMap mimeTypeToExtsMap = new HashMap<>(); private long jobId; private static final HashMap totalsForIngestJobs = new HashMap<>(); private static final IngestModuleReferenceCounter refCounter = new IngestModuleReferenceCounter(); @@ -88,14 +88,15 @@ public class FileExtMismatchIngestModule implements FileIngestModule { } @Override + @Messages({"FileExtMismatchIngestModule.readError.message=Could not read settings."}) public void startUp(IngestJobContext context) throws IngestModuleException { jobId = context.getJobId(); refCounter.incrementAndGet(jobId); try { - SigTypeToExtMap = FileExtMismatchSettings.readSettings().getSigTypeToExtMap(); + mimeTypeToExtsMap = FileExtMismatchSettings.readSettings().getMimeTypeToExtsMap(); } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { - throw new IngestModuleException("Could not load file extension mismatch configurations.", ex); + throw new IngestModuleException(Bundle.FileExtMismatchIngestModule_readError_message(), ex); } try { this.detector = new FileTypeDetector(); @@ -179,7 +180,7 @@ public class FileExtMismatchIngestModule implements FileIngestModule { } //get known allowed values from the map for this type - String[] allowedExtArray = SigTypeToExtMap.get(currActualSigType); + String[] allowedExtArray = mimeTypeToExtsMap.get(currActualSigType); if (allowedExtArray != null) { List allowedExtList = Arrays.asList(allowedExtArray); diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java index 6a0799b7bf..67e7b694a6 100755 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java @@ -44,7 +44,7 @@ import org.w3c.dom.NodeList; class FileExtMismatchSettings implements Serializable { private static final long serialVersionUID = 1L; - private HashMap sigTypeToExtMap; + private HashMap mimeTypeToExtsMap; private static final Logger logger = Logger.getLogger(FileExtMismatchSettings.class.getName()); private static final String SIG_EL = "signature"; //NON-NLS private static final String EXT_EL = "ext"; //NON-NLS @@ -64,32 +64,32 @@ class FileExtMismatchSettings implements Serializable { } /** - * Makes a settings object based on given sig type map + * Makes a settings object based on given mime type map * - * @param sigTypeToExtMap + * @param mimeTypeToExtsMap */ - FileExtMismatchSettings(HashMap sigTypeToExtMap) { - this.sigTypeToExtMap = sigTypeToExtMap; + FileExtMismatchSettings(HashMap mimeTypeToExtsMap) { + this.mimeTypeToExtsMap = mimeTypeToExtsMap; } /** - * @return the sig type to extension map + * @return the mime type to extension map */ - HashMap getSigTypeToExtMap() { - return sigTypeToExtMap; + HashMap getMimeTypeToExtsMap() { + return mimeTypeToExtsMap; } /** * Sets the signature to extension map for this settings. */ - public void setSigTypeToExtMap(HashMap sigTypeToExtMap) { - this.sigTypeToExtMap = sigTypeToExtMap; + public void setMimeTypeToExtsMap(HashMap mimeTypeToExtsMap) { + this.mimeTypeToExtsMap = mimeTypeToExtsMap; } /** - * Load and parse the settings + * Reads the file extension mismatch settings. * - * @return Loaded hash map or null on error or null if data does not exist + * @return Loaded settings (empty if there are no settings to load). */ public static synchronized FileExtMismatchSettings readSettings() throws FileExtMismatchSettingsException { HashMap sigTypeToExtMap = new HashMap<>(); @@ -149,18 +149,17 @@ class FileExtMismatchSettings implements Serializable { } catch (Exception e) { logger.log(Level.SEVERE, "Error loading config file.", e); //NON-NLS - return null; + throw new FileExtMismatchSettingsException("Error loading config file.", e); //NON-NLS } } return new FileExtMismatchSettings(sigTypeToExtMap); } /** - * Save XML to filePath, overwriting it if it already exists - * - * @param sigTypeToExtMap String arrays of extensions mapped to each string - * mimetype. + * Save settings to disk. * + * @param settings The settings to save to disk + * * @return Loaded hash map or null on error or null if data does not exist */ public static synchronized boolean writeSettings(FileExtMismatchSettings settings) throws FileExtMismatchSettingsException { diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java index ff2b86cf7c..2bb681d44b 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java @@ -564,7 +564,7 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel try { // Load the configuration into a buffer that the user can modify. They can choose // to save it back to the file after making changes. - editableMap = FileExtMismatchSettings.readSettings().getSigTypeToExtMap(); + editableMap = FileExtMismatchSettings.readSettings().getMimeTypeToExtsMap(); } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { //error From b5a796ba3950641a11fc75fe4d01bab9f5bacdc7 Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Fri, 8 Apr 2016 16:12:08 -0400 Subject: [PATCH 09/14] Fixed menus options, semantics changes.' --- .../FileExtMismatchContextMenuActionsProvider.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java index d21ca8ec69..63a57921cf 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java @@ -31,7 +31,6 @@ import org.openide.util.Utilities; import org.openide.util.lookup.ServiceProvider; import org.sleuthkit.autopsy.corecomponentinterfaces.ContextMenuActionsProvider; import org.sleuthkit.autopsy.coreutils.Logger; -import org.sleuthkit.autopsy.datamodel.BlackboardArtifactNode; import org.sleuthkit.autopsy.ingest.IngestManager; import org.sleuthkit.datamodel.AbstractFile; import org.sleuthkit.datamodel.BlackboardArtifact; @@ -50,21 +49,20 @@ public class FileExtMismatchContextMenuActionsProvider implements ContextMenuAct // Ignore if file ingest is in progress. if (!IngestManager.getInstance().isIngestRunning()) { - final Collection selectedArts = Utilities.actionsGlobalContext().lookupAll(BlackboardArtifactNode.class); + final Collection selectedArts = Utilities.actionsGlobalContext().lookupAll(BlackboardArtifact.class); // Prevent multiselect if (selectedArts.size() == 1) { - for (BlackboardArtifactNode artifactNode : selectedArts) { + for (BlackboardArtifact nodeArt : selectedArts) { - BlackboardArtifact nodeArt = artifactNode.getLookup().lookup(BlackboardArtifact.class); // Only for mismatch results if (nodeArt.getArtifactTypeName().equals("TSK_EXT_MISMATCH_DETECTED")) { //NON-NLS String mimeTypeStr = ""; String extStr = ""; - AbstractFile af = artifactNode.getLookup().lookup(AbstractFile.class); - + AbstractFile af = Utilities.actionsGlobalContext().lookup(AbstractFile.class); + if (af != null) { int i = af.getName().lastIndexOf("."); if ((i > -1) && ((i + 1) < af.getName().length())) { From 85ff25a68629caba1222ffa8e30d6093dc1954c1 Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Wed, 13 Apr 2016 13:24:31 -0400 Subject: [PATCH 10/14] Logging semantics changes. --- .../modules/fileextmismatch/AddFileExtensionAction.java | 7 ++++--- .../FileExtMismatchContextMenuActionsProvider.java | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java index ac27fd6479..70cf0cff61 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java @@ -34,8 +34,9 @@ import org.sleuthkit.autopsy.coreutils.Logger; * extension list for the MIME type. */ class AddFileExtensionAction extends AbstractAction { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; + private static final Logger logger = Logger.getLogger(AddFileExtensionAction.class.getName()); private final String extStr; private final String mimeTypeStr; private final FileExtMismatchSettings settings; @@ -65,14 +66,14 @@ class AddFileExtensionAction extends AbstractAction { Bundle.AddFileExtensionAction_writeError_message(), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), JOptionPane.ERROR_MESSAGE); - Logger.getLogger(this.getClass().getName()).log(Level.WARNING, Bundle.AddFileExtensionAction_writeError_message()); + logger.log(Level.SEVERE, Bundle.AddFileExtensionAction_writeError_message()); } // else //in the future we might want to update the statusbar to give feedback to the user } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { JOptionPane.showMessageDialog(null, Bundle.AddFileExtensionAction_writeError_message(), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), JOptionPane.ERROR_MESSAGE); - Logger.getLogger(this.getClass().getName()).log(Level.WARNING, Bundle.AddFileExtensionAction_writeError_message(), ex); + logger.log(Level.SEVERE, Bundle.AddFileExtensionAction_writeError_message(), ex); } } } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java index 63a57921cf..76657aea33 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java @@ -41,6 +41,7 @@ import org.sleuthkit.datamodel.BlackboardArtifact; */ @ServiceProvider(service = ContextMenuActionsProvider.class) public class FileExtMismatchContextMenuActionsProvider implements ContextMenuActionsProvider { + private static final Logger logger = Logger.getLogger(FileExtMismatchContextMenuActionsProvider.class.getName()); @Override public List getActions() { @@ -101,7 +102,7 @@ public class FileExtMismatchContextMenuActionsProvider implements ContextMenuAct NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg2"), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), JOptionPane.ERROR_MESSAGE); - Logger.getLogger(this.getClass().getName()).log(Level.WARNING, NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg2"), ex); + logger.log(Level.WARNING, NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg2"), ex); } } } From 541b18e0f093d98cca0be75232980bb340beacd9 Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Wed, 13 Apr 2016 13:48:11 -0400 Subject: [PATCH 11/14] Semantics fixes, changed error behavior. --- .../AddFileExtensionAction.java | 12 ++----- .../FileExtMismatchSettings.java | 36 +++++++++++-------- .../FileExtMismatchSettingsPanel.java | 22 ++++-------- 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java index 70cf0cff61..782d3b68f5 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java @@ -60,20 +60,14 @@ class AddFileExtensionAction extends AbstractAction { editableMap.put(mimeTypeStr, editedExtensions.toArray(new String[0])); try { - if (!FileExtMismatchSettings.writeSettings(new FileExtMismatchSettings(editableMap))) { - //error - JOptionPane.showMessageDialog(null, - Bundle.AddFileExtensionAction_writeError_message(), - NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), - JOptionPane.ERROR_MESSAGE); - logger.log(Level.SEVERE, Bundle.AddFileExtensionAction_writeError_message()); - } // else //in the future we might want to update the statusbar to give feedback to the user + FileExtMismatchSettings.writeSettings(new FileExtMismatchSettings(editableMap)); } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { + //error JOptionPane.showMessageDialog(null, Bundle.AddFileExtensionAction_writeError_message(), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), JOptionPane.ERROR_MESSAGE); - logger.log(Level.SEVERE, Bundle.AddFileExtensionAction_writeError_message(), ex); + logger.log(Level.SEVERE, Bundle.AddFileExtensionAction_writeError_message()); } } } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java index 67e7b694a6..f873c908df 100755 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java @@ -92,27 +92,35 @@ class FileExtMismatchSettings implements Serializable { * @return Loaded settings (empty if there are no settings to load). */ public static synchronized FileExtMismatchSettings readSettings() throws FileExtMismatchSettingsException { - HashMap sigTypeToExtMap = new HashMap<>(); File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH); //Tries reading the serialized file first, as this is the prioritized settings. if (serializedFile.exists()) { - try { - try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(serializedFile))) { - FileExtMismatchSettings fileExtMismatchSettings = (FileExtMismatchSettings) in.readObject(); - return fileExtMismatchSettings; - } - } catch (IOException | ClassNotFoundException ex) { - throw new FileExtMismatchSettingsException("Couldn't read serialized settings.", ex); - } + return readSerializedSettings(); } + return readXmlSettings(); + } + private static FileExtMismatchSettings readSerializedSettings() throws FileExtMismatchSettingsException { + File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH); + try { + try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(serializedFile))) { + FileExtMismatchSettings fileExtMismatchSettings = (FileExtMismatchSettings) in.readObject(); + return fileExtMismatchSettings; + } + } catch (IOException | ClassNotFoundException ex) { + throw new FileExtMismatchSettingsException("Couldn't read serialized settings.", ex); + } + } + + private static FileExtMismatchSettings readXmlSettings() throws FileExtMismatchSettingsException { + HashMap sigTypeToExtMap = new HashMap<>(); //Next tries to read the xml file if the serialized file did not exist File xmlFile = new File(FILTER_CONFIG_FILE); if (xmlFile.exists()) { try { final Document doc = XMLUtil.loadDoc(FileExtMismatchSettings.class, FILTER_CONFIG_FILE); if (doc == null) { - return new FileExtMismatchSettings(sigTypeToExtMap); + throw new FileExtMismatchSettingsException("Error loading config file: invalid file format (could not load doc)."); } Element root = doc.getDocumentElement(); @@ -124,7 +132,7 @@ class FileExtMismatchSettings implements Serializable { final int numSigs = sigNList.getLength(); if (numSigs == 0) { - throw new FileExtMismatchSettingsException("Error loading config file: invalid file format (bad signature)."); //NON-NLS + throw new FileExtMismatchSettingsException("Error loading config file: invalid file format (no signature)."); //NON-NLS } for (int sigIndex = 0; sigIndex < numSigs; ++sigIndex) { @@ -148,7 +156,6 @@ class FileExtMismatchSettings implements Serializable { } } catch (Exception e) { - logger.log(Level.SEVERE, "Error loading config file.", e); //NON-NLS throw new FileExtMismatchSettingsException("Error loading config file.", e); //NON-NLS } } @@ -159,13 +166,12 @@ class FileExtMismatchSettings implements Serializable { * Save settings to disk. * * @param settings The settings to save to disk - * + * * @return Loaded hash map or null on error or null if data does not exist */ - public static synchronized boolean writeSettings(FileExtMismatchSettings settings) throws FileExtMismatchSettingsException { + public static synchronized void writeSettings(FileExtMismatchSettings settings) throws FileExtMismatchSettingsException { try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DEFAULT_SERIALIZED_FILE_PATH))) { out.writeObject(settings); - return true; } catch (IOException ex) { throw new FileExtMismatchSettingsException(String.format("Failed to write settings to %s", DEFAULT_SERIALIZED_FILE_PATH), ex); } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java index 2bb681d44b..4d446a4865 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java @@ -29,7 +29,6 @@ import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.AbstractTableModel; -import org.openide.util.Exceptions; import org.sleuthkit.autopsy.ingest.IngestModuleGlobalSettingsPanel; import org.openide.util.NbBundle; import org.sleuthkit.autopsy.coreutils.Logger; @@ -532,22 +531,13 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel @Override public void saveSettings() { try { - if (FileExtMismatchSettings.writeSettings(new FileExtMismatchSettings(editableMap))) { - mimeErrLabel.setText(" "); - mimeRemoveErrLabel.setText(" "); - extRemoveErrLabel.setText(" "); - extErrorLabel.setText(" "); + FileExtMismatchSettings.writeSettings(new FileExtMismatchSettings(editableMap)); + mimeErrLabel.setText(" "); + mimeRemoveErrLabel.setText(" "); + extRemoveErrLabel.setText(" "); + extErrorLabel.setText(" "); - saveMsgLabel.setText(NbBundle.getMessage(this.getClass(), "FileExtMismatchConfigPanel.store.msg")); - } else { - //error - JOptionPane.showMessageDialog(this, - NbBundle.getMessage(this.getClass(), - "FileExtMismatchConfigPanel.store.msgDlg.msg"), - NbBundle.getMessage(this.getClass(), - "FileExtMismatchConfigPanel.save.msgDlg.title"), - JOptionPane.ERROR_MESSAGE); - } + saveMsgLabel.setText(NbBundle.getMessage(this.getClass(), "FileExtMismatchConfigPanel.store.msg")); } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) { //error JOptionPane.showMessageDialog(this, From 85b9694659e96061d202820fe12abeeb33b6572c Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Mon, 18 Apr 2016 12:47:25 -0400 Subject: [PATCH 12/14] Started updating to standardize using extension set. --- .../AddFileExtensionAction.java | 9 +++---- ...ExtMismatchContextMenuActionsProvider.java | 6 ++--- .../FileExtMismatchIngestModule.java | 21 ++++++---------- .../FileExtMismatchSettings.java | 19 +++++++------- .../FileExtMismatchSettingsPanel.java | 25 ++++++++++--------- 5 files changed, 37 insertions(+), 43 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java index 782d3b68f5..77815c5eea 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java @@ -19,9 +19,8 @@ package org.sleuthkit.autopsy.modules.fileextmismatch; import java.awt.event.ActionEvent; -import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; +import java.util.Set; import java.util.logging.Level; import javax.swing.AbstractAction; import javax.swing.JOptionPane; @@ -51,13 +50,13 @@ class AddFileExtensionAction extends AbstractAction { @Override @Messages({"AddFileExtensionAction.writeError.message=Could not write file extension settings."}) public void actionPerformed(ActionEvent event) { - HashMap editableMap; + HashMap> editableMap; editableMap = settings.getMimeTypeToExtsMap(); - ArrayList editedExtensions = new ArrayList<>(Arrays.asList(editableMap.get(mimeTypeStr))); + Set editedExtensions = editableMap.get(mimeTypeStr); editedExtensions.add(extStr); // Old array will be replaced by new array for this key - editableMap.put(mimeTypeStr, editedExtensions.toArray(new String[0])); + editableMap.put(mimeTypeStr, editedExtensions); try { FileExtMismatchSettings.writeSettings(new FileExtMismatchSettings(editableMap)); diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java index 76657aea33..e7e547e846 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java @@ -19,10 +19,10 @@ package org.sleuthkit.autopsy.modules.fileextmismatch; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.List; +import java.util.Set; import java.util.logging.Level; import javax.swing.Action; import javax.swing.JOptionPane; @@ -87,12 +87,12 @@ public class FileExtMismatchContextMenuActionsProvider implements ContextMenuAct extStr, mimeTypeStr); // Check if already added - HashMap editableMap; + HashMap> editableMap; try { FileExtMismatchSettings settings = FileExtMismatchSettings.readSettings(); editableMap = settings.getMimeTypeToExtsMap(); actions.add(new AddFileExtensionAction(menuItemStr, extStr, mimeTypeStr, settings)); - ArrayList editedExtensions = new ArrayList<>(Arrays.asList(editableMap.get(mimeTypeStr))); + Set editedExtensions = editableMap.get(mimeTypeStr); if (editedExtensions.contains(extStr)) { // Informs the user that they have already added this extension to this MIME type actions.get(0).setEnabled(false); diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java index ab83419d9c..2bcc005749 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchIngestModule.java @@ -18,10 +18,9 @@ */ package org.sleuthkit.autopsy.modules.fileextmismatch; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.List; +import java.util.Set; import java.util.logging.Level; import org.openide.util.NbBundle; import org.openide.util.NbBundle.Messages; @@ -52,7 +51,7 @@ public class FileExtMismatchIngestModule implements FileIngestModule { private static final Logger logger = Logger.getLogger(FileExtMismatchIngestModule.class.getName()); private final IngestServices services = IngestServices.getInstance(); private final FileExtMismatchDetectorModuleSettings settings; - private HashMap mimeTypeToExtsMap = new HashMap<>(); + private HashMap> mimeTypeToExtsMap = new HashMap<>(); private long jobId; private static final HashMap totalsForIngestJobs = new HashMap<>(); private static final IngestModuleReferenceCounter refCounter = new IngestModuleReferenceCounter(); @@ -109,7 +108,7 @@ public class FileExtMismatchIngestModule implements FileIngestModule { @Override public ProcessResult process(AbstractFile abstractFile) { blackboard = Case.getCurrentCase().getServices().getBlackboard(); - if(this.settings.skipKnownFiles() && (abstractFile.getKnown() == FileKnown.KNOWN)) { + if (this.settings.skipKnownFiles() && (abstractFile.getKnown() == FileKnown.KNOWN)) { return ProcessResult.OK; } @@ -180,16 +179,12 @@ public class FileExtMismatchIngestModule implements FileIngestModule { } //get known allowed values from the map for this type - String[] allowedExtArray = mimeTypeToExtsMap.get(currActualSigType); - if (allowedExtArray != null) { - List allowedExtList = Arrays.asList(allowedExtArray); - + Set allowedExtSet = mimeTypeToExtsMap.get(currActualSigType); + if (allowedExtSet != null) { // see if the filename ext is in the allowed list - if (allowedExtList != null) { - for (String e : allowedExtList) { - if (e.equals(currActualExt)) { - return false; - } + for (String e : allowedExtSet) { + if (e.equals(currActualExt)) { + return false; } return true; //potential mismatch } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java index f873c908df..57b9b52893 100755 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java @@ -23,9 +23,9 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.Serializable; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import java.util.logging.Level; import org.openide.util.io.NbObjectInputStream; import org.openide.util.io.NbObjectOutputStream; @@ -44,7 +44,7 @@ import org.w3c.dom.NodeList; class FileExtMismatchSettings implements Serializable { private static final long serialVersionUID = 1L; - private HashMap mimeTypeToExtsMap; + private HashMap> mimeTypeToExtsMap; private static final Logger logger = Logger.getLogger(FileExtMismatchSettings.class.getName()); private static final String SIG_EL = "signature"; //NON-NLS private static final String EXT_EL = "ext"; //NON-NLS @@ -68,21 +68,21 @@ class FileExtMismatchSettings implements Serializable { * * @param mimeTypeToExtsMap */ - FileExtMismatchSettings(HashMap mimeTypeToExtsMap) { + FileExtMismatchSettings(HashMap> mimeTypeToExtsMap) { this.mimeTypeToExtsMap = mimeTypeToExtsMap; } /** * @return the mime type to extension map */ - HashMap getMimeTypeToExtsMap() { + HashMap> getMimeTypeToExtsMap() { return mimeTypeToExtsMap; } /** * Sets the signature to extension map for this settings. */ - public void setMimeTypeToExtsMap(HashMap mimeTypeToExtsMap) { + public void setMimeTypeToExtsMap(HashMap> mimeTypeToExtsMap) { this.mimeTypeToExtsMap = mimeTypeToExtsMap; } @@ -113,7 +113,7 @@ class FileExtMismatchSettings implements Serializable { } private static FileExtMismatchSettings readXmlSettings() throws FileExtMismatchSettingsException { - HashMap sigTypeToExtMap = new HashMap<>(); + HashMap> sigTypeToExtMap = new HashMap<>(); //Next tries to read the xml file if the serialized file did not exist File xmlFile = new File(FILTER_CONFIG_FILE); if (xmlFile.exists()) { @@ -143,13 +143,12 @@ class FileExtMismatchSettings implements Serializable { final int numExts = extNList.getLength(); if (numExts != 0) { - List extStrings = new ArrayList<>(); + Set extStrings = new HashSet<>(); for (int extIndex = 0; extIndex < numExts; ++extIndex) { Element extEl = (Element) extNList.item(extIndex); extStrings.add(extEl.getTextContent()); } - String[] sarray = extStrings.toArray(new String[0]); - sigTypeToExtMap.put(mimetype, sarray); + sigTypeToExtMap.put(mimetype, extStrings); } else { sigTypeToExtMap.put(mimetype, null); //ok to have an empty type (the ingest module will not use it) } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java index 4d446a4865..35bc8d539c 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettingsPanel.java @@ -20,19 +20,20 @@ package org.sleuthkit.autopsy.modules.fileextmismatch; import java.awt.Color; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; import java.util.logging.Level; import javax.swing.JOptionPane; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.AbstractTableModel; -import org.sleuthkit.autopsy.ingest.IngestModuleGlobalSettingsPanel; import org.openide.util.NbBundle; -import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.corecomponents.OptionsPanel; +import org.sleuthkit.autopsy.coreutils.Logger; +import org.sleuthkit.autopsy.ingest.IngestModuleGlobalSettingsPanel; import org.sleuthkit.autopsy.modules.filetypeid.FileTypeDetector; /** @@ -42,7 +43,7 @@ import org.sleuthkit.autopsy.modules.filetypeid.FileTypeDetector; final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel implements OptionsPanel { private static final Logger logger = Logger.getLogger(FileExtMismatchSettingsPanel.class.getName()); - private HashMap editableMap = new HashMap<>(); + private HashMap> editableMap = new HashMap<>(); private ArrayList mimeList = null; private ArrayList currentExtensions = null; private MimeTableModel mimeTableModel; @@ -382,11 +383,11 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel return; } - ArrayList editedExtensions = new ArrayList<>(Arrays.asList(editableMap.get(selectedMime))); + Set editedExtensions = editableMap.get(selectedMime); editedExtensions.add(newExt); // Old array will be replaced by new array for this key - editableMap.put(selectedMime, editedExtensions.toArray(new String[0])); + editableMap.put(selectedMime, editedExtensions); // Refresh table updateExtList(); @@ -430,7 +431,7 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel return; } - editableMap.put(newMime, new String[0]); + editableMap.put(newMime, new HashSet()); // Refresh table updateMimeList(); @@ -491,12 +492,12 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel return; } - ArrayList editedExtensions = new ArrayList<>(Arrays.asList(editableMap.get(selectedMime))); + Set editedExtensions = editableMap.get(selectedMime); editedExtensions.remove(selectedExt); String deadExt = selectedExt; // Old array will be replaced by new array for this key - editableMap.put(selectedMime, editedExtensions.toArray(new String[0])); + editableMap.put(selectedMime, editedExtensions); // Refresh tables updateExtList(); @@ -517,10 +518,10 @@ final class FileExtMismatchSettingsPanel extends IngestModuleGlobalSettingsPanel } private void updateExtList() { - String[] temp = editableMap.get(selectedMime); + Set temp = editableMap.get(selectedMime); if (temp != null) { - currentExtensions = new ArrayList<>(Arrays.asList(temp)); - if (temp.length > 0) { + currentExtensions = new ArrayList<>(temp); + if (temp.size() > 0) { Collections.sort(currentExtensions); } } else { From 730bfc6278d3816f54b7a30c521b0265a43c1335 Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Mon, 18 Apr 2016 13:52:41 -0400 Subject: [PATCH 13/14] Fixed up error messages and use of public. --- .../modules/fileextmismatch/AddFileExtensionAction.java | 2 +- .../autopsy/modules/fileextmismatch/Bundle.properties | 3 +-- .../FileExtMismatchContextMenuActionsProvider.java | 2 +- .../modules/fileextmismatch/FileExtMismatchSettings.java | 8 ++++---- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java index 77815c5eea..967990cdeb 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java @@ -66,7 +66,7 @@ class AddFileExtensionAction extends AbstractAction { Bundle.AddFileExtensionAction_writeError_message(), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), JOptionPane.ERROR_MESSAGE); - logger.log(Level.SEVERE, Bundle.AddFileExtensionAction_writeError_message()); + logger.log(Level.SEVERE, "Could not write file extension settings.", ex); } } } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties index 8d62766e8c..6ac747bd6c 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/Bundle.properties @@ -1,7 +1,6 @@ OpenIDE-Module-Name=FileExtMismatch OptionsCategory_Name_FileExtMismatchOptions=File Extension Mismatch OptionsCategory_FileExtMismatch=File Extension Mismatch -AddFileExtensionAction.msgDlg.msg=Writing configuration file failed. AddFileExtensionAction.msgDlg.msg2=File extension mismatch settings could not be read, extensions update not available. AddFileExtensionAction.msgDlg.title=Add Mismatch Extension Error AddFileExtensionAction.extHeaderLbl.text=Allowed Extensions for @@ -20,7 +19,7 @@ FileExtMismatchConfigPanel.removeExtButton.noneSelected=No extension selected\! FileExtMismatchConfigPanel.removeExtButton.noMimeTypeSelected=No MIME type selected\! FileExtMismatchConfigPanel.removeExtButton.deleted=Extension {0} deleted. FileExtMismatchConfigPanel.store.msg=Saved. -FileExtMismatchConfigPanel.store.msgDlg.msg=Writing configuration file failed. +FileExtMismatchConfigPanel.store.msgDlg.msg=File extension mismatch settings could not be saved. FileExtMismatchConfigPanel.save.msgDlg.title=Save Error FileExtMismatchConfigPanel.ok.confDlg.msg=Would you like to save configuration changes? FileExtMismatchConfigPanel.confDlg.title=Unsaved Changes diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java index e7e547e846..877a92e6e6 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchContextMenuActionsProvider.java @@ -102,7 +102,7 @@ public class FileExtMismatchContextMenuActionsProvider implements ContextMenuAct NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg2"), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), JOptionPane.ERROR_MESSAGE); - logger.log(Level.WARNING, NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg2"), ex); + logger.log(Level.WARNING, "File extension mismatch settings could not be read, extensions update not available.", ex); } } } diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java index 57b9b52893..e489e55975 100755 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/FileExtMismatchSettings.java @@ -57,7 +57,7 @@ class FileExtMismatchSettings implements Serializable { static { try { - boolean extracted = PlatformUtil.extractResourceToUserConfigDir(FileExtMismatchSettings.class, DEFAULT_CONFIG_FILE_NAME, false); + PlatformUtil.extractResourceToUserConfigDir(FileExtMismatchSettings.class, DEFAULT_CONFIG_FILE_NAME, false); } catch (IOException ex) { logger.log(Level.SEVERE, "Error copying default mismatch configuration to user dir ", ex); //NON-NLS } @@ -82,7 +82,7 @@ class FileExtMismatchSettings implements Serializable { /** * Sets the signature to extension map for this settings. */ - public void setMimeTypeToExtsMap(HashMap> mimeTypeToExtsMap) { + void setMimeTypeToExtsMap(HashMap> mimeTypeToExtsMap) { this.mimeTypeToExtsMap = mimeTypeToExtsMap; } @@ -91,7 +91,7 @@ class FileExtMismatchSettings implements Serializable { * * @return Loaded settings (empty if there are no settings to load). */ - public static synchronized FileExtMismatchSettings readSettings() throws FileExtMismatchSettingsException { + static synchronized FileExtMismatchSettings readSettings() throws FileExtMismatchSettingsException { File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH); //Tries reading the serialized file first, as this is the prioritized settings. if (serializedFile.exists()) { @@ -168,7 +168,7 @@ class FileExtMismatchSettings implements Serializable { * * @return Loaded hash map or null on error or null if data does not exist */ - public static synchronized void writeSettings(FileExtMismatchSettings settings) throws FileExtMismatchSettingsException { + static synchronized void writeSettings(FileExtMismatchSettings settings) throws FileExtMismatchSettingsException { try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DEFAULT_SERIALIZED_FILE_PATH))) { out.writeObject(settings); } catch (IOException ex) { From 3289326de6a890836982aeea9fc7687323f2ae9f Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Tue, 19 Apr 2016 10:29:42 -0400 Subject: [PATCH 14/14] Got rid of unnecessary re-put in map. --- .../modules/fileextmismatch/AddFileExtensionAction.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java index 967990cdeb..7c43f14182 100644 --- a/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java +++ b/Core/src/org/sleuthkit/autopsy/modules/fileextmismatch/AddFileExtensionAction.java @@ -55,9 +55,6 @@ class AddFileExtensionAction extends AbstractAction { Set editedExtensions = editableMap.get(mimeTypeStr); editedExtensions.add(extStr); - // Old array will be replaced by new array for this key - editableMap.put(mimeTypeStr, editedExtensions); - try { FileExtMismatchSettings.writeSettings(new FileExtMismatchSettings(editableMap)); } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) {