From d9071440b7e0485d5834d05e46f4db2408d81f89 Mon Sep 17 00:00:00 2001 From: William Schaefer Date: Fri, 9 Jul 2021 11:00:24 -0400 Subject: [PATCH 1/2] 7801 - changes to notify user when errors occur with manage hosts --- .../datamodel/hosts/Bundle.properties-MERGED | 8 ++ .../datamodel/hosts/ManageHostsDialog.java | 90 +++++++++++-------- 2 files changed, 61 insertions(+), 37 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/datamodel/hosts/Bundle.properties-MERGED b/Core/src/org/sleuthkit/autopsy/datamodel/hosts/Bundle.properties-MERGED index cec3355913..f29e2b67a0 100644 --- a/Core/src/org/sleuthkit/autopsy/datamodel/hosts/Bundle.properties-MERGED +++ b/Core/src/org/sleuthkit/autopsy/datamodel/hosts/Bundle.properties-MERGED @@ -15,6 +15,14 @@ CTL_OpenHosts=Manage Hosts HostNameValidator_getValidationMessage_onDuplicate=Another host already has the same name. Please choose a different name. HostNameValidator_getValidationMessage_onEmpty=Please provide some text for the host name. HostNameValidator_getValidationMessage_sameAsOriginal=Please provide a new name for this host. +# {0} - hostname +ManageHostsDialog.failureToAdd.txt=Unable to add new host {0} at this time. +# {0} - hostname +ManageHostsDialog.failureToDelete.txt=Unable to delete host {0} at this time. +# {0} - hostname +# {1} - hostId +ManageHostsDialog.failureToEdit.txt=Unable to update host {0} with id: {1} at this time. +ManageHostsDialog.failureToGetHosts.txt=There was an error while fetching hosts for current case. # To change this license header, choose License Headers in Project Properties. # To change this template file, choose Tools | Templates # and open the template in the editor. diff --git a/Core/src/org/sleuthkit/autopsy/datamodel/hosts/ManageHostsDialog.java b/Core/src/org/sleuthkit/autopsy/datamodel/hosts/ManageHostsDialog.java index 6cb4220a7a..3c98dd2760 100644 --- a/Core/src/org/sleuthkit/autopsy/datamodel/hosts/ManageHostsDialog.java +++ b/Core/src/org/sleuthkit/autopsy/datamodel/hosts/ManageHostsDialog.java @@ -31,10 +31,12 @@ import java.util.stream.Collectors; import javax.swing.JFrame; import javax.swing.ListModel; import org.apache.commons.collections4.CollectionUtils; +import org.openide.util.NbBundle; import org.openide.util.NbBundle.Messages; import org.sleuthkit.autopsy.casemodule.Case; import org.sleuthkit.autopsy.casemodule.NoCurrentCaseException; import org.sleuthkit.autopsy.coreutils.Logger; +import org.sleuthkit.autopsy.coreutils.MessageNotifyUtil; import org.sleuthkit.datamodel.DataSource; import org.sleuthkit.datamodel.Host; import org.sleuthkit.datamodel.SleuthkitCase; @@ -52,14 +54,14 @@ public class ManageHostsDialog extends javax.swing.JDialog { * List item to be used with jlist. */ private static class HostListItem { - + private final Host host; private final List dataSources; /** * Main constructor. * - * @param host The host. + * @param host The host. * @param dataSources The data sources that are children of this host. */ HostListItem(Host host, List dataSources) { @@ -80,19 +82,19 @@ public class ManageHostsDialog extends javax.swing.JDialog { List getDataSources() { return dataSources; } - + @Override public String toString() { return host == null ? "" : host.getName(); } - + @Override public int hashCode() { int hash = 5; hash = 89 * hash + Objects.hashCode(this.host == null ? 0 : this.host.getHostId()); return hash; } - + @Override public boolean equals(Object obj) { if (this == obj) { @@ -108,15 +110,15 @@ public class ManageHostsDialog extends javax.swing.JDialog { if (this.host == null || other.getHost() == null) { return this.host == null && other.getHost() == null; } - + return this.host.getHostId() == other.getHost().getHostId(); } - + } - + private static final Logger logger = Logger.getLogger(ManageHostsDialog.class.getName()); private static final long serialVersionUID = 1L; - + private Map> hostChildrenMap = Collections.emptyMap(); /** @@ -152,7 +154,7 @@ public class ManageHostsDialog extends javax.swing.JDialog { /** * @return The currently selected host in the list or null if no host is - * selected. + * selected. */ Host getSelectedHost() { return (hostList.getSelectedValue() == null) ? null : hostList.getSelectedValue().getHost(); @@ -161,6 +163,8 @@ public class ManageHostsDialog extends javax.swing.JDialog { /** * Shows add/edit dialog, and if a value is returned, creates a new Host. */ + @NbBundle.Messages({"# {0} - hostname", + "ManageHostsDialog.failureToAdd.txt=Unable to add new host {0} at this time."}) private void addHost() { String newHostName = getAddEditDialogName(null); if (newHostName != null) { @@ -169,7 +173,8 @@ public class ManageHostsDialog extends javax.swing.JDialog { Host newHost = Case.getCurrentCaseThrows().getSleuthkitCase().getHostManager().newHost(newHostName); selectedId = newHost == null ? null : newHost.getHostId(); } catch (NoCurrentCaseException | TskCoreException e) { - logger.log(Level.WARNING, String.format("Unable to add new host '%s' at this time.", newHostName), e); + logger.log(Level.WARNING, Bundle.ManageHostsDialog_failureToAdd_txt(newHostName), e); + MessageNotifyUtil.Message.warn(Bundle.ManageHostsDialog_failureToAdd_txt(newHostName)); } refresh(); setSelectedHostById(selectedId); @@ -181,12 +186,15 @@ public class ManageHostsDialog extends javax.swing.JDialog { * * @param selectedHost */ + @NbBundle.Messages({"# {0} - hostname", + "ManageHostsDialog.failureToDelete.txt=Unable to delete host {0} at this time."}) private void deleteHost(Host selectedHost) { if (selectedHost != null && selectedHost.getName() != null) { try { Case.getCurrentCaseThrows().getSleuthkitCase().getHostManager().deleteHost(selectedHost.getName()); } catch (NoCurrentCaseException | TskCoreException e) { - logger.log(Level.WARNING, String.format("Unable to delete host '%s' at this time.", selectedHost.getName()), e); + logger.log(Level.WARNING, Bundle.ManageHostsDialog_failureToDelete_txt(selectedHost.getName()), e); + MessageNotifyUtil.Message.error(Bundle.ManageHostsDialog_failureToDelete_txt(selectedHost.getName())); } refresh(); } @@ -199,28 +207,28 @@ public class ManageHostsDialog extends javax.swing.JDialog { */ private void setSelectedHostById(Long selectedId) { ListModel model = hostList.getModel(); - + if (selectedId == null) { hostList.clearSelection(); } - + for (int i = 0; i < model.getSize(); i++) { Object o = model.getElementAt(i); if (!(o instanceof HostListItem)) { continue; } - + Host host = ((HostListItem) o).getHost(); if (host == null) { continue; } - + if (host.getHostId() == selectedId) { hostList.setSelectedIndex(i); return; } } - + hostList.clearSelection(); } @@ -229,22 +237,26 @@ public class ManageHostsDialog extends javax.swing.JDialog { * * @param selectedHost The selected host. */ + @NbBundle.Messages({"# {0} - hostname", + "# {1} - hostId", + "ManageHostsDialog.failureToEdit.txt=Unable to update host {0} with id: {1} at this time."}) private void editHost(Host selectedHost) { - + if (selectedHost != null) { String newHostName = getAddEditDialogName(selectedHost); if (newHostName != null) { try { Case.getCurrentCaseThrows().getSleuthkitCase().getHostManager().updateHostName(selectedHost, newHostName); } catch (NoCurrentCaseException | TskCoreException e) { - logger.log(Level.WARNING, String.format("Unable to update host '%s' with id: %d at this time.", selectedHost.getName(), selectedHost.getHostId()), e); + logger.log(Level.WARNING, Bundle.ManageHostsDialog_failureToEdit_txt(selectedHost.getName(), selectedHost.getHostId()), e); + MessageNotifyUtil.Message.warn(Bundle.ManageHostsDialog_failureToEdit_txt(selectedHost.getName(), selectedHost.getHostId())); } - + HostListItem selectedItem = hostList.getSelectedValue(); Long selectedId = selectedItem == null || selectedItem.getHost() == null ? null : selectedItem.getHost().getHostId(); - + refresh(); - + setSelectedHostById(selectedId); } } @@ -254,25 +266,26 @@ public class ManageHostsDialog extends javax.swing.JDialog { * Shows the dialog to add or edit the name of a host. * * @param origValue The original values for the host or null if adding a - * host. + * host. + * * @return The new name for the host or null if operation was cancelled. */ private String getAddEditDialogName(Host origValue) { JFrame parent = (this.getRootPane() != null && this.getRootPane().getParent() instanceof JFrame) ? (JFrame) this.getRootPane().getParent() : null; - + AddEditHostDialog addEditDialog = new AddEditHostDialog(parent, hostChildrenMap.keySet(), origValue); addEditDialog.setResizable(false); addEditDialog.setLocationRelativeTo(parent); addEditDialog.setVisible(true); addEditDialog.toFront(); - + if (addEditDialog.isChanged()) { String newHostName = addEditDialog.getValue(); return newHostName; } - + return null; } @@ -289,14 +302,14 @@ public class ManageHostsDialog extends javax.swing.JDialog { * hosts. */ private void refreshData() { - + hostChildrenMap = getHostListData(); - + Vector jlistData = hostChildrenMap.entrySet().stream() .sorted((a, b) -> getNameOrEmpty(a.getKey()).compareTo(getNameOrEmpty(b.getKey()))) .map(entry -> new HostListItem(entry.getKey(), entry.getValue())) .collect(Collectors.toCollection(Vector::new)); - + hostList.setListData(jlistData); } @@ -305,6 +318,7 @@ public class ManageHostsDialog extends javax.swing.JDialog { * host is null. * * @param h The host. + * * @return The name of the host or empty string. */ private String getNameOrEmpty(Host h) { @@ -315,33 +329,35 @@ public class ManageHostsDialog extends javax.swing.JDialog { * Retrieves the current list of hosts for the case. * * @return The list of hosts to be displayed in the list (sorted - * alphabetically). + * alphabetically). */ + @NbBundle.Messages({"ManageHostsDialog.failureToGetHosts.txt=There was an error while fetching hosts for current case."}) private Map> getHostListData() { Map> hostMapping = new HashMap<>(); try { SleuthkitCase curCase = Case.getCurrentCaseThrows().getSleuthkitCase(); List hosts = curCase.getHostManager().getAllHosts(); List dataSources = curCase.getDataSources(); - + if (dataSources != null) { for (DataSource ds : dataSources) { List hostDataSources = hostMapping.computeIfAbsent(ds.getHost(), (d) -> new ArrayList<>()); hostDataSources.add(ds); } - + } - + if (hosts != null) { for (Host host : hosts) { hostMapping.putIfAbsent(host, Collections.emptyList()); } } - + } catch (TskCoreException | NoCurrentCaseException ex) { - logger.log(Level.WARNING, "There was an error while fetching hosts for current case.", ex); + logger.log(Level.WARNING, Bundle.ManageHostsDialog_failureToGetHosts_txt(), ex); + MessageNotifyUtil.Message.warn(Bundle.ManageHostsDialog_failureToGetHosts_txt()); } - + return hostMapping; } @@ -356,7 +372,7 @@ public class ManageHostsDialog extends javax.swing.JDialog { this.deleteButton.setEnabled(selectedHost != null && CollectionUtils.isEmpty(dataSources)); String nameTextFieldStr = selectedHost != null && selectedHost.getName() != null ? selectedHost.getName() : ""; this.hostNameTextField.setText(nameTextFieldStr); - + } /** From 232637e27f7d6fcc30de8486f7846dc7e9e8b3df Mon Sep 17 00:00:00 2001 From: William Schaefer Date: Fri, 9 Jul 2021 13:09:39 -0400 Subject: [PATCH 2/2] 7801 add message instructions to see log --- .../datamodel/hosts/Bundle.properties-MERGED | 1 + .../datamodel/hosts/ManageHostsDialog.java | 69 ++++++++++--------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/datamodel/hosts/Bundle.properties-MERGED b/Core/src/org/sleuthkit/autopsy/datamodel/hosts/Bundle.properties-MERGED index f29e2b67a0..53aeebac4e 100644 --- a/Core/src/org/sleuthkit/autopsy/datamodel/hosts/Bundle.properties-MERGED +++ b/Core/src/org/sleuthkit/autopsy/datamodel/hosts/Bundle.properties-MERGED @@ -38,6 +38,7 @@ AddEditHostDialog.nameLabel.text=Name: AddEditHostDialog.okButton.text=OK AddEditHostDialog.cancelButton.text=Cancel AddEditHostDialog.inputTextField.text=jTextField1 +ManageHostsDialog.seeLog.txt=\ See log for more information. ManageHostsDialog_title_text=Manage Hosts # {0} - sourceHost # {1} - destHost diff --git a/Core/src/org/sleuthkit/autopsy/datamodel/hosts/ManageHostsDialog.java b/Core/src/org/sleuthkit/autopsy/datamodel/hosts/ManageHostsDialog.java index 3c98dd2760..bbb1f40251 100644 --- a/Core/src/org/sleuthkit/autopsy/datamodel/hosts/ManageHostsDialog.java +++ b/Core/src/org/sleuthkit/autopsy/datamodel/hosts/ManageHostsDialog.java @@ -54,7 +54,7 @@ public class ManageHostsDialog extends javax.swing.JDialog { * List item to be used with jlist. */ private static class HostListItem { - + private final Host host; private final List dataSources; @@ -82,19 +82,19 @@ public class ManageHostsDialog extends javax.swing.JDialog { List getDataSources() { return dataSources; } - + @Override public String toString() { return host == null ? "" : host.getName(); } - + @Override public int hashCode() { int hash = 5; hash = 89 * hash + Objects.hashCode(this.host == null ? 0 : this.host.getHostId()); return hash; } - + @Override public boolean equals(Object obj) { if (this == obj) { @@ -110,15 +110,15 @@ public class ManageHostsDialog extends javax.swing.JDialog { if (this.host == null || other.getHost() == null) { return this.host == null && other.getHost() == null; } - + return this.host.getHostId() == other.getHost().getHostId(); } - + } - + private static final Logger logger = Logger.getLogger(ManageHostsDialog.class.getName()); private static final long serialVersionUID = 1L; - + private Map> hostChildrenMap = Collections.emptyMap(); /** @@ -164,7 +164,8 @@ public class ManageHostsDialog extends javax.swing.JDialog { * Shows add/edit dialog, and if a value is returned, creates a new Host. */ @NbBundle.Messages({"# {0} - hostname", - "ManageHostsDialog.failureToAdd.txt=Unable to add new host {0} at this time."}) + "ManageHostsDialog.failureToAdd.txt=Unable to add new host {0} at this time.", + "ManageHostsDialog.seeLog.txt= See log for more information."}) private void addHost() { String newHostName = getAddEditDialogName(null); if (newHostName != null) { @@ -174,7 +175,7 @@ public class ManageHostsDialog extends javax.swing.JDialog { selectedId = newHost == null ? null : newHost.getHostId(); } catch (NoCurrentCaseException | TskCoreException e) { logger.log(Level.WARNING, Bundle.ManageHostsDialog_failureToAdd_txt(newHostName), e); - MessageNotifyUtil.Message.warn(Bundle.ManageHostsDialog_failureToAdd_txt(newHostName)); + MessageNotifyUtil.Message.warn(Bundle.ManageHostsDialog_failureToAdd_txt(newHostName) + Bundle.ManageHostsDialog_seeLog_txt()); } refresh(); setSelectedHostById(selectedId); @@ -194,7 +195,7 @@ public class ManageHostsDialog extends javax.swing.JDialog { Case.getCurrentCaseThrows().getSleuthkitCase().getHostManager().deleteHost(selectedHost.getName()); } catch (NoCurrentCaseException | TskCoreException e) { logger.log(Level.WARNING, Bundle.ManageHostsDialog_failureToDelete_txt(selectedHost.getName()), e); - MessageNotifyUtil.Message.error(Bundle.ManageHostsDialog_failureToDelete_txt(selectedHost.getName())); + MessageNotifyUtil.Message.error(Bundle.ManageHostsDialog_failureToDelete_txt(selectedHost.getName()) + Bundle.ManageHostsDialog_seeLog_txt()); } refresh(); } @@ -207,28 +208,28 @@ public class ManageHostsDialog extends javax.swing.JDialog { */ private void setSelectedHostById(Long selectedId) { ListModel model = hostList.getModel(); - + if (selectedId == null) { hostList.clearSelection(); } - + for (int i = 0; i < model.getSize(); i++) { Object o = model.getElementAt(i); if (!(o instanceof HostListItem)) { continue; } - + Host host = ((HostListItem) o).getHost(); if (host == null) { continue; } - + if (host.getHostId() == selectedId) { hostList.setSelectedIndex(i); return; } } - + hostList.clearSelection(); } @@ -241,7 +242,7 @@ public class ManageHostsDialog extends javax.swing.JDialog { "# {1} - hostId", "ManageHostsDialog.failureToEdit.txt=Unable to update host {0} with id: {1} at this time."}) private void editHost(Host selectedHost) { - + if (selectedHost != null) { String newHostName = getAddEditDialogName(selectedHost); if (newHostName != null) { @@ -249,14 +250,14 @@ public class ManageHostsDialog extends javax.swing.JDialog { Case.getCurrentCaseThrows().getSleuthkitCase().getHostManager().updateHostName(selectedHost, newHostName); } catch (NoCurrentCaseException | TskCoreException e) { logger.log(Level.WARNING, Bundle.ManageHostsDialog_failureToEdit_txt(selectedHost.getName(), selectedHost.getHostId()), e); - MessageNotifyUtil.Message.warn(Bundle.ManageHostsDialog_failureToEdit_txt(selectedHost.getName(), selectedHost.getHostId())); + MessageNotifyUtil.Message.warn(Bundle.ManageHostsDialog_failureToEdit_txt(selectedHost.getName(), selectedHost.getHostId()) + Bundle.ManageHostsDialog_seeLog_txt()); } - + HostListItem selectedItem = hostList.getSelectedValue(); Long selectedId = selectedItem == null || selectedItem.getHost() == null ? null : selectedItem.getHost().getHostId(); - + refresh(); - + setSelectedHostById(selectedId); } } @@ -274,18 +275,18 @@ public class ManageHostsDialog extends javax.swing.JDialog { JFrame parent = (this.getRootPane() != null && this.getRootPane().getParent() instanceof JFrame) ? (JFrame) this.getRootPane().getParent() : null; - + AddEditHostDialog addEditDialog = new AddEditHostDialog(parent, hostChildrenMap.keySet(), origValue); addEditDialog.setResizable(false); addEditDialog.setLocationRelativeTo(parent); addEditDialog.setVisible(true); addEditDialog.toFront(); - + if (addEditDialog.isChanged()) { String newHostName = addEditDialog.getValue(); return newHostName; } - + return null; } @@ -302,14 +303,14 @@ public class ManageHostsDialog extends javax.swing.JDialog { * hosts. */ private void refreshData() { - + hostChildrenMap = getHostListData(); - + Vector jlistData = hostChildrenMap.entrySet().stream() .sorted((a, b) -> getNameOrEmpty(a.getKey()).compareTo(getNameOrEmpty(b.getKey()))) .map(entry -> new HostListItem(entry.getKey(), entry.getValue())) .collect(Collectors.toCollection(Vector::new)); - + hostList.setListData(jlistData); } @@ -338,26 +339,26 @@ public class ManageHostsDialog extends javax.swing.JDialog { SleuthkitCase curCase = Case.getCurrentCaseThrows().getSleuthkitCase(); List hosts = curCase.getHostManager().getAllHosts(); List dataSources = curCase.getDataSources(); - + if (dataSources != null) { for (DataSource ds : dataSources) { List hostDataSources = hostMapping.computeIfAbsent(ds.getHost(), (d) -> new ArrayList<>()); hostDataSources.add(ds); } - + } - + if (hosts != null) { for (Host host : hosts) { hostMapping.putIfAbsent(host, Collections.emptyList()); } } - + } catch (TskCoreException | NoCurrentCaseException ex) { logger.log(Level.WARNING, Bundle.ManageHostsDialog_failureToGetHosts_txt(), ex); - MessageNotifyUtil.Message.warn(Bundle.ManageHostsDialog_failureToGetHosts_txt()); + MessageNotifyUtil.Message.warn(Bundle.ManageHostsDialog_failureToGetHosts_txt() + Bundle.ManageHostsDialog_seeLog_txt()); } - + return hostMapping; } @@ -372,7 +373,7 @@ public class ManageHostsDialog extends javax.swing.JDialog { this.deleteButton.setEnabled(selectedHost != null && CollectionUtils.isEmpty(dataSources)); String nameTextFieldStr = selectedHost != null && selectedHost.getName() != null ? selectedHost.getName() : ""; this.hostNameTextField.setText(nameTextFieldStr); - + } /**