From 3d84bf1dfb70087b7efb0a9ba7a2be1a7f21ef8c Mon Sep 17 00:00:00 2001 From: Oliver Spohngellert Date: Fri, 11 Mar 2016 09:02:50 -0500 Subject: [PATCH] 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 { } } - }