Autopsy  4.4
Graphical digital forensics platform for The Sleuth Kit and other tools.
DataResultFilterNode.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2017 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.directorytree;
20 
21 import java.awt.event.ActionEvent;
22 import java.beans.PropertyVetoException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.prefs.PreferenceChangeEvent;
29 import java.util.prefs.PreferenceChangeListener;
30 import javax.swing.AbstractAction;
31 import javax.swing.Action;
32 import org.openide.explorer.ExplorerManager;
33 import org.openide.nodes.AbstractNode;
34 import org.openide.nodes.FilterNode;
35 import org.openide.nodes.Node;
36 import org.openide.nodes.Sheet;
37 import org.openide.util.NbBundle;
38 import org.openide.util.Utilities;
60 import org.sleuthkit.datamodel.AbstractFile;
61 import org.sleuthkit.datamodel.BlackboardArtifact;
62 import org.sleuthkit.datamodel.BlackboardAttribute;
63 import org.sleuthkit.datamodel.Content;
64 import org.sleuthkit.datamodel.DerivedFile;
65 import org.sleuthkit.datamodel.Directory;
66 import org.sleuthkit.datamodel.File;
67 import org.sleuthkit.datamodel.LayoutFile;
68 import org.sleuthkit.datamodel.LocalFile;
69 import org.sleuthkit.datamodel.SlackFile;
70 import org.sleuthkit.datamodel.TskData;
71 import org.sleuthkit.datamodel.TskException;
72 import org.sleuthkit.datamodel.VirtualDirectory;
73 import static org.sleuthkit.autopsy.directorytree.Bundle.*;
74 
80 public class DataResultFilterNode extends FilterNode {
81 
82  private static final Logger LOGGER = Logger.getLogger(DataResultFilterNode.class.getName());
83 
88 
89  static {
90  UserPreferences.addChangeListener(new PreferenceChangeListener() {
91  @Override
92  public void preferenceChange(PreferenceChangeEvent evt) {
93  switch (evt.getKey()) {
95  filterKnownFromDataSources = UserPreferences.hideKnownFilesInDataSourcesTree();
96  break;
98  filterKnownFromViews = UserPreferences.hideKnownFilesInViewsTree();
99  break;
101  filterSlackFromDataSources = UserPreferences.hideSlackFilesInDataSourcesTree();
102  break;
104  filterSlackFromViews = UserPreferences.hideSlackFilesInViewsTree();
105  break;
106  }
107  }
108  });
109  }
110 
113 
114  private final ExplorerManager sourceEm;
115 
125  public DataResultFilterNode(Node node, ExplorerManager em) {
126  super(node, new DataResultFilterChildren(node, em));
127  this.sourceEm = em;
128  }
129 
143  private DataResultFilterNode(Node node, ExplorerManager em, boolean filterKnown, boolean filterSlack) {
144  super(node, new DataResultFilterChildren(node, em, filterKnown, filterSlack));
145  this.sourceEm = em;
146  }
147 
156  @Override
157  public Action[] getActions(boolean popup) {
158 
159  List<Action> actions = new ArrayList<>();
160 
161  final DisplayableItemNode originalNode = (DisplayableItemNode) this.getOriginal();
162  List<Action> accept = originalNode.accept(getActionsDIV);
163  if (accept != null) {
164  actions.addAll(accept);
165  }
166 
167  //actions.add(new IndexContentFilesAction(nodeContent, "Index"));
168  return actions.toArray(new Action[actions.size()]);
169  }
170 
177  @Override
178  public Action getPreferredAction() {
179  final Node original = this.getOriginal();
180  // Once had a org.openide.nodes.ChildFactory$WaitFilterNode passed in
181  if ((original instanceof DisplayableItemNode) == false) {
182  return null;
183  }
184 
185  final DisplayableItemNode originalNode = (DisplayableItemNode) this.getOriginal();
186  return originalNode.accept(getPreferredActionsDIV);
187  }
188 
189  @Override
190  public Node.PropertySet[] getPropertySets() {
191  Node.PropertySet[] propertySets = super.getPropertySets();
192 
193  for (int i = 0; i < propertySets.length; i++) {
194  Node.PropertySet ps = propertySets[i];
195 
196  if (ps.getName().equals(Sheet.PROPERTIES)) {
197  Sheet.Set newPs = new Sheet.Set();
198  newPs.setName(ps.getName());
199  newPs.setDisplayName(ps.getDisplayName());
200  newPs.setShortDescription(ps.getShortDescription());
201 
202  newPs.put(ps.getProperties());
203  if (newPs.remove(AbstractFsContentNode.HIDE_PARENT) != null) {
204  newPs.remove(AbstractFilePropertyType.LOCATION.toString());
205  }
206  propertySets[i] = newPs;
207  }
208  }
209 
210  return propertySets;
211  }
212 
219  public void setChildNodeSelectionInfo(NodeSelectionInfo selectedChildNodeInfo) {
220  if (getOriginal() instanceof DisplayableItemNode) {
221  ((DisplayableItemNode) getOriginal()).setChildNodeSelectionInfo(selectedChildNodeInfo);
222  }
223  }
224 
233  if (getOriginal() instanceof DisplayableItemNode) {
234  return ((DisplayableItemNode) getOriginal()).getChildNodeSelectionInfo();
235  } else {
236  return null;
237  }
238  }
239 
245  private static class DataResultFilterChildren extends FilterNode.Children {
246 
247  private final ExplorerManager sourceEm;
248 
249  private boolean filterKnown;
250  private boolean filterSlack;
251 
255  private DataResultFilterChildren(Node arg, ExplorerManager sourceEm) {
256  super(arg);
257  switch (SelectionContext.getSelectionContext(arg)) {
258  case DATA_SOURCES:
259  filterSlack = filterSlackFromDataSources;
260  filterKnown = filterKnownFromDataSources;
261  break;
262  case VIEWS:
263  filterSlack = filterSlackFromViews;
264  filterKnown = filterKnownFromViews;
265  break;
266  default:
267  filterSlack = false;
268  filterKnown = false;
269  break;
270  }
271  this.sourceEm = sourceEm;
272  }
273 
274  private DataResultFilterChildren(Node arg, ExplorerManager sourceEm, boolean filterKnown, boolean filterSlack) {
275  super(arg);
276  this.filterKnown = filterKnown;
277  this.filterSlack = filterSlack;
278  this.sourceEm = sourceEm;
279  }
280 
281  @Override
282  protected Node[] createNodes(Node key) {
283  AbstractFile file = key.getLookup().lookup(AbstractFile.class);
284  if (file != null) {
285  if (filterKnown && (file.getKnown() == TskData.FileKnown.KNOWN)) {
286  // Filter out child nodes that represent known files
287  return new Node[]{};
288  }
289  if (filterSlack && file.getType().equals(TskData.TSK_DB_FILES_TYPE_ENUM.SLACK)) {
290  // Filter out child nodes that represent slack files
291  return new Node[]{};
292  }
293  }
294  return new Node[]{new DataResultFilterNode(key, sourceEm, filterKnown, filterSlack)};
295  }
296  }
297 
298  @NbBundle.Messages("DataResultFilterNode.viewSourceArtifact.text=View Source Result")
303  private static class GetPopupActionsDisplayableItemNodeVisitor extends DisplayableItemNodeVisitor.Default<List<Action>> {
304 
305  @Override
306  public List<Action> visit(BlackboardArtifactNode ban) {
307  //set up actions for artifact node based on its Content object
308  //TODO all actions need to be consolidated in single place!
309  //they should be set in individual Node subclass and using a utility to get Actions per Content sub-type
310  // TODO UPDATE: There is now a DataModelActionsFactory utility;
311 
312  List<Action> actionsList = new ArrayList<>();
313 
314  //merge predefined specific node actions if bban subclasses have their own
315  for (Action a : ban.getActions(true)) {
316  actionsList.add(a);
317  }
318  BlackboardArtifact ba = ban.getLookup().lookup(BlackboardArtifact.class);
319  final int artifactTypeID = ba.getArtifactTypeID();
320 
321  if (artifactTypeID == BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT.getTypeID()
322  || artifactTypeID == BlackboardArtifact.ARTIFACT_TYPE.TSK_KEYWORD_HIT.getTypeID()) {
323  actionsList.add(new ViewContextAction(
324  NbBundle.getMessage(this.getClass(), "DataResultFilterNode.action.viewFileInDir.text"), ban));
325  } else if (artifactTypeID == BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT.getTypeID()) {
326  //action to go to the source artifact
327  actionsList.add(new ViewSourceArtifactAction(DataResultFilterNode_viewSourceArtifact_text(), ba));
328  // action to go to the source file of the artifact
329  actionsList.add(new ViewContextAction(
330  NbBundle.getMessage(this.getClass(), "DataResultFilterNode.action.viewSrcFileInDir.text"), ban));
331  } else {
332  // if the artifact links to another file, add an action to go to
333  // that file
334  Content c = findLinked(ban);
335  if (c != null) {
336  actionsList.add(new ViewContextAction(
337  NbBundle.getMessage(this.getClass(), "DataResultFilterNode.action.viewFileInDir.text"), c));
338  }
339  // action to go to the source file of the artifact
340  actionsList.add(new ViewContextAction(
341  NbBundle.getMessage(this.getClass(), "DataResultFilterNode.action.viewSrcFileInDir.text"), ban));
342  }
343  Content c = ban.getLookup().lookup(File.class);
344  Node n = null;
345  boolean md5Action = false;
346  if (c != null) {
347  n = new FileNode((AbstractFile) c);
348  md5Action = true;
349  } else if ((c = ban.getLookup().lookup(Directory.class)) != null) {
350  n = new DirectoryNode((Directory) c);
351  } else if ((c = ban.getLookup().lookup(VirtualDirectory.class)) != null) {
352  n = new VirtualDirectoryNode((VirtualDirectory) c);
353  } else if ((c = ban.getLookup().lookup(LayoutFile.class)) != null) {
354  n = new LayoutFileNode((LayoutFile) c);
355  } else if ((c = ban.getLookup().lookup(LocalFile.class)) != null
356  || (c = ban.getLookup().lookup(DerivedFile.class)) != null) {
357  n = new LocalFileNode((AbstractFile) c);
358  } else if ((c = ban.getLookup().lookup(SlackFile.class)) != null) {
359  n = new SlackFileNode((SlackFile) c);
360  }
361  if (n != null) {
362  actionsList.add(null); // creates a menu separator
363  actionsList.add(new NewWindowViewAction(
364  NbBundle.getMessage(this.getClass(), "DataResultFilterNode.action.viewInNewWin.text"), n));
365  actionsList.add(new ExternalViewerAction(
366  NbBundle.getMessage(this.getClass(), "DataResultFilterNode.action.openInExtViewer.text"), n));
367  actionsList.add(null); // creates a menu separator
368  actionsList.add(ExtractAction.getInstance());
369  if (md5Action) {
370  actionsList.add(new HashSearchAction(
371  NbBundle.getMessage(this.getClass(), "DataResultFilterNode.action.searchFilesSameMd5.text"), n));
372  }
373  actionsList.add(null); // creates a menu separator
374  actionsList.add(AddContentTagAction.getInstance());
375  actionsList.add(AddBlackboardArtifactTagAction.getInstance());
376 
377  final Collection<AbstractFile> selectedFilesList
378  = new HashSet<>(Utilities.actionsGlobalContext().lookupAll(AbstractFile.class));
379  if (selectedFilesList.size() == 1) {
380  actionsList.add(DeleteFileContentTagAction.getInstance());
381  }
382  } else {
383  // There's no specific file associated with the artifact, but
384  // we can still tag the artifact itself
385  actionsList.add(null);
386  actionsList.add(AddBlackboardArtifactTagAction.getInstance());
387  }
388 
389  final Collection<BlackboardArtifact> selectedArtifactsList
390  = new HashSet<>(Utilities.actionsGlobalContext().lookupAll(BlackboardArtifact.class));
391  if (selectedArtifactsList.size() == 1) {
393  }
394 
395  if (n != null) {
396  actionsList.addAll(ContextMenuExtensionPoint.getActions());
397  }
398 
399  return actionsList;
400  }
401 
402  @Override
403  public List<Action> visit(Reports.ReportsListNode ditem) {
404  // The base class Action is "Collapse All", inappropriate.
405  return null;
406  }
407 
408  @Override
409  public List<Action> visit(FileTypesNode fileTypes) {
410  return defaultVisit(fileTypes);
411  }
412 
413  @Override
414  protected List<Action> defaultVisit(DisplayableItemNode ditem) {
415  //preserve the default node's actions
416  List<Action> actions = new ArrayList<>();
417 
418  for (Action action : ditem.getActions(true)) {
419  actions.add(action);
420  }
421 
422  return actions;
423  }
424 
425  private Content findLinked(BlackboardArtifactNode ba) {
426  BlackboardArtifact art = ba.getLookup().lookup(BlackboardArtifact.class);
427  Content c = null;
428  try {
429  for (BlackboardAttribute attr : art.getAttributes()) {
430  if (attr.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH_ID.getTypeID()) {
431  switch (attr.getAttributeType().getValueType()) {
432  case INTEGER:
433  int i = attr.getValueInt();
434  if (i != -1) {
435  c = art.getSleuthkitCase().getContentById(i);
436  }
437  break;
438  case LONG:
439  long l = attr.getValueLong();
440  if (l != -1) {
441  c = art.getSleuthkitCase().getContentById(l);
442  }
443  break;
444  }
445  }
446  }
447  } catch (TskException ex) {
448  Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Error getting linked file", ex); //NON-NLS
449  }
450  return c;
451  }
452 
453  }
454 
455  /*
456  * Action for double-click / preferred action on nodes.
457  */
458  private class GetPreferredActionsDisplayableItemNodeVisitor extends DisplayableItemNodeVisitor.Default<AbstractAction> {
459 
460  @Override
461  public AbstractAction visit(BlackboardArtifactNode ban) {
462  return new ViewContextAction(
463  NbBundle.getMessage(this.getClass(), "DataResultFilterNode.action.viewInDir.text"), ban);
464  }
465 
466  @Override
467  public AbstractAction visit(DirectoryNode dn) {
468  if (dn.getDisplayName().equals(DirectoryNode.DOTDOTDIR)) {
469  return openParent(dn);
470  } else if (dn.getDisplayName().equals(DirectoryNode.DOTDIR) == false) {
471  return openChild(dn);
472  } else {
473  return null;
474  }
475  }
476 
477  @Override
478  public AbstractAction visit(FileNode fn) {
479  if (fn.hasContentChildren()) {
480  return openChild(fn);
481  } else {
482  return null;
483  }
484  }
485 
486  @Override
487  public AbstractAction visit(LocalFileNode dfn) {
488  if (dfn.hasContentChildren()) {
489  return openChild(dfn);
490  } else {
491  return null;
492  }
493  }
494 
495  @Override
496  public AbstractAction visit(Reports.ReportNode reportNode) {
497  return reportNode.getPreferredAction();
498  }
499 
500  @Override
501  protected AbstractAction defaultVisit(DisplayableItemNode c) {
502  return openChild(c);
503  }
504 
505  @Override
506  public AbstractAction visit(FileTypesNode fileTypes) {
507  return openChild(fileTypes);
508  }
509 
518  private AbstractAction openChild(final AbstractNode dataModelNode) {
519  // get the current selection from the directory tree explorer manager,
520  // which is a DirectoryTreeFilterNode. One of that node's children
521  // is a DirectoryTreeFilterNode that wraps the dataModelNode. We need
522  // to set that wrapped node as the selection and root context of the
523  // directory tree explorer manager (sourceEm)
524  final Node currentSelectionInDirectoryTree = sourceEm.getSelectedNodes()[0];
525 
526  return new AbstractAction() {
527  @Override
528  public void actionPerformed(ActionEvent e) {
529  if (currentSelectionInDirectoryTree != null) {
530  // Find the filter version of the passed in dataModelNode.
531  final org.openide.nodes.Children children = currentSelectionInDirectoryTree.getChildren();
532  // This call could break if the DirectoryTree is re-implemented with lazy ChildFactory objects.
533  Node newSelection = children.findChild(dataModelNode.getName());
534 
535  /*
536  * We got null here when we were viewing a ZIP file in
537  * the Views -> Archives area and double clicking on it
538  * got to this code. It tried to find the child in the
539  * tree and didn't find it. An exception was then thrown
540  * from setting the selected node to be null.
541  */
542  if (newSelection != null) {
543  try {
544  sourceEm.setExploredContextAndSelection(newSelection, new Node[]{newSelection});
545  } catch (PropertyVetoException ex) {
546  Logger logger = Logger.getLogger(DataResultFilterNode.class.getName());
547  logger.log(Level.WARNING, "Error: can't open the selected directory.", ex); //NON-NLS
548  }
549  }
550  }
551  }
552  };
553  }
554 
563  private AbstractAction openParent(AbstractNode node) {
564  // @@@ Why do we ignore node?
565  Node[] selectedFilterNodes = sourceEm.getSelectedNodes();
566  Node selectedFilterNode = selectedFilterNodes[0];
567  final Node parentNode = selectedFilterNode.getParentNode();
568 
569  return new AbstractAction() {
570  @Override
571  public void actionPerformed(ActionEvent e) {
572  try {
573  sourceEm.setSelectedNodes(new Node[]{parentNode});
574  } catch (PropertyVetoException ex) {
575  Logger logger = Logger.getLogger(DataResultFilterNode.class.getName());
576  logger.log(Level.WARNING, "Error: can't open the parent directory.", ex); //NON-NLS
577  }
578  }
579  };
580  }
581  }
582 }
abstract< T > T accept(DisplayableItemNodeVisitor< T > visitor)
static final DisplayableItemNodeVisitor< List< Action > > getActionsDIV
static synchronized AddBlackboardArtifactTagAction getInstance()
static synchronized DeleteFileBlackboardArtifactTagAction getInstance()
DataResultFilterChildren(Node arg, ExplorerManager sourceEm, boolean filterKnown, boolean filterSlack)
static synchronized ExtractAction getInstance()
void setChildNodeSelectionInfo(NodeSelectionInfo selectedChildNodeInfo)
static synchronized DeleteFileContentTagAction getInstance()
final DisplayableItemNodeVisitor< AbstractAction > getPreferredActionsDIV
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
static void addChangeListener(PreferenceChangeListener listener)
static synchronized AddContentTagAction getInstance()
DataResultFilterNode(Node node, ExplorerManager em, boolean filterKnown, boolean filterSlack)

Copyright © 2012-2016 Basis Technology. Generated on: Tue Jun 13 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.