Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
Tags.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2021 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.datamodel;
20 
21 import java.beans.PropertyChangeEvent;
22 import java.beans.PropertyChangeListener;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.EnumSet;
26 import java.util.List;
27 import java.util.Observable;
28 import java.util.Observer;
29 import java.util.Set;
30 import java.util.logging.Level;
31 import org.openide.nodes.ChildFactory;
32 import org.openide.nodes.Children;
33 import org.openide.nodes.Node;
34 import org.openide.nodes.Sheet;
35 import org.openide.util.NbBundle;
36 import org.openide.util.WeakListeners;
37 import org.openide.util.lookup.Lookups;
45 import org.sleuthkit.datamodel.BlackboardArtifactTag;
46 import org.sleuthkit.datamodel.ContentTag;
47 import org.sleuthkit.datamodel.TagName;
48 import org.sleuthkit.datamodel.TskCoreException;
49 
55 public class Tags implements AutopsyVisitableItem {
56  // Creation of a RootNode object corresponding to a Tags object is done
57  // by a CreateAutopsyNodeVisitor dispatched from the AbstractContentChildren
58  // override of Children.Keys<T>.createNodes().
59 
60  private final static String DISPLAY_NAME = NbBundle.getMessage(RootNode.class, "TagsNode.displayName.text");
62  private static final String USER_NAME_PROPERTY = "user.name"; //NON-NLS
63  private final TagResults tagResults = new TagResults();
64  private final String ICON_PATH = "org/sleuthkit/autopsy/images/tag-folder-blue-icon-16.png"; //NON-NLS
65 
66  private final long filteringDSObjId; // 0 if not filtering/grouping by data source
67 
68  Tags() {
69  this(0);
70  }
71 
72  Tags(long dsObjId) {
73  this.filteringDSObjId = dsObjId;
74  }
75 
81  public static String getTagsDisplayName() {
82  return DISPLAY_NAME;
83  }
84 
85  long filteringDataSourceObjId() {
86  return this.filteringDSObjId;
87  }
88 
89  @Override
90  public <T> T accept(AutopsyItemVisitor<T> visitor) {
91  return visitor.visit(this);
92  }
93 
99  private class TagResults extends Observable {
100 
101  public void update() {
102  setChanged();
103  notifyObservers();
104  }
105  }
106 
113  public class RootNode extends DisplayableItemNode {
114 
115  public RootNode(long objId) {
116  super(Children.create(new TagNameNodeFactory(objId), true), Lookups.singleton(DISPLAY_NAME));
117  super.setName(DISPLAY_NAME);
118  super.setDisplayName(DISPLAY_NAME);
119  this.setIconBaseWithExtension(ICON_PATH);
120  }
121 
122  @Override
123  public boolean isLeafTypeNode() {
124  return false;
125  }
126 
127  @Override
128  public <T> T accept(DisplayableItemNodeVisitor<T> visitor) {
129  return visitor.visit(this);
130  }
131 
132  @Override
133  protected Sheet createSheet() {
134  Sheet propertySheet = super.createSheet();
135  Sheet.Set properties = propertySheet.get(Sheet.PROPERTIES);
136  if (properties == null) {
137  properties = Sheet.createPropertiesSet();
138  propertySheet.put(properties);
139  }
140  properties.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "TagsNode.createSheet.name.name"),
141  NbBundle.getMessage(this.getClass(), "TagsNode.createSheet.name.displayName"), "", getName()));
142  return propertySheet;
143  }
144 
145  @Override
146  public String getItemType() {
147  return getClass().getName();
148  }
149 
153  public void refresh() {
154  tagResults.update();
155  }
156 
157  }
158 
159  private class TagNameNodeFactory extends ChildFactory.Detachable<TagName> implements Observer {
160 
161  private final long filteringDSObjId; // 0 if not filtering/grouping by data source
162 
168 
169  private final PropertyChangeListener pcl = new PropertyChangeListener() {
170  @Override
171  public void propertyChange(PropertyChangeEvent evt) {
172  String eventType = evt.getPropertyName();
173  if (eventType.equals(Case.Events.BLACKBOARD_ARTIFACT_TAG_ADDED.toString())
174  || eventType.equals(Case.Events.BLACKBOARD_ARTIFACT_TAG_DELETED.toString())
175  || eventType.equals(Case.Events.CONTENT_TAG_ADDED.toString())
176  || eventType.equals(Case.Events.CONTENT_TAG_DELETED.toString())) {
183  try {
185  refresh(true);
186  tagResults.update();
187  } catch (NoCurrentCaseException notUsed) {
191  }
192  } else if (eventType.equals(IngestManager.IngestJobEvent.COMPLETED.toString())
193  || eventType.equals(IngestManager.IngestJobEvent.CANCELLED.toString())) {
200  try {
202  refresh(true);
203  tagResults.update();
204  } catch (NoCurrentCaseException notUsed) {
208  }
209  } else if (eventType.equals(Case.Events.CURRENT_CASE.toString())) {
210  // case was closed. Remove listeners so that this can be garbage collected
211  if (evt.getNewValue() == null) {
212  removeNotify();
213  }
214  }
215  }
216  };
217 
218  private final PropertyChangeListener weakPcl = WeakListeners.propertyChange(pcl, null);
219 
225  TagNameNodeFactory(long objId) {
226  this.filteringDSObjId = objId;
227  }
228 
229  @Override
230  protected void addNotify() {
233  tagResults.update();
234  tagResults.addObserver(this);
235  }
236 
237  @Override
238  protected void finalize() throws Throwable {
239  super.finalize();
242  tagResults.deleteObserver(this);
243  }
244 
245  @Override
246  protected boolean createKeys(List<TagName> keys) {
247  try {
248  List<TagName> tagNamesInUse;
250  String userName = System.getProperty(USER_NAME_PROPERTY);
251  tagNamesInUse = (filteringDSObjId > 0)
254  } else {
255  tagNamesInUse = (filteringDSObjId > 0)
258  }
259  Collections.sort(tagNamesInUse, new Comparator<TagName>() {
260  @Override
261  public int compare(TagName o1, TagName o2) {
263  }
264  });
265  keys.addAll(tagNamesInUse);
266  } catch (TskCoreException | NoCurrentCaseException ex) {
267  Logger.getLogger(TagNameNodeFactory.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
268  }
269  return true;
270  }
271 
272  @Override
273  protected Node createNodeForKey(TagName key) {
274  return new TagNameNode(key);
275  }
276 
277  @Override
278  public void update(Observable o, Object arg) {
279  refresh(true);
280  }
281  }
282 
288  public class TagNameNode extends DisplayableItemNode implements Observer {
289 
290  private final String ICON_PATH = "org/sleuthkit/autopsy/images/tag-folder-blue-icon-16.png"; //NON-NLS
291  private final String BOOKMARK_TAG_ICON_PATH = "org/sleuthkit/autopsy/images/star-bookmark-icon-16.png"; //NON-NLS
292  private final TagName tagName;
293 
294  public TagNameNode(TagName tagName) {
295  super(Children.create(new TagTypeNodeFactory(tagName), true), Lookups.singleton(NbBundle.getMessage(TagNameNode.class, "TagNameNode.namePlusTags.text", tagName.getDisplayName())));
296  this.tagName = tagName;
297  setName(TagUtils.getDecoratedTagDisplayName(tagName));
299  if (tagName.getDisplayName().equals(TagsManager.getBookmarkTagDisplayName())) {
300  setIconBaseWithExtension(BOOKMARK_TAG_ICON_PATH);
301  } else {
302  setIconBaseWithExtension(ICON_PATH);
303  }
304  tagResults.addObserver(this);
305  }
306 
307  private void updateDisplayName() {
308  long tagsCount = 0;
309  try {
312  String userName = System.getProperty(USER_NAME_PROPERTY);
313  if (filteringDSObjId > 0) {
314  tagsCount = tm.getContentTagsCountByTagNameForUser(tagName, filteringDSObjId, userName);
315  tagsCount += tm.getBlackboardArtifactTagsCountByTagNameForUser(tagName, filteringDSObjId, userName);
316  } else {
317  tagsCount = tm.getContentTagsCountByTagNameForUser(tagName, userName);
318  tagsCount += tm.getBlackboardArtifactTagsCountByTagNameForUser(tagName, userName);
319  }
320  } else {
321  if (filteringDSObjId > 0) {
322  tagsCount = tm.getContentTagsCountByTagName(tagName, filteringDSObjId);
323  tagsCount += tm.getBlackboardArtifactTagsCountByTagName(tagName, filteringDSObjId);
324  } else {
325  tagsCount = tm.getContentTagsCountByTagName(tagName);
326  tagsCount += tm.getBlackboardArtifactTagsCountByTagName(tagName);
327  }
328  }
329  } catch (TskCoreException | NoCurrentCaseException ex) {
330  Logger.getLogger(TagNameNode.class.getName()).log(Level.SEVERE, "Failed to get tags count for " + tagName.getDisplayName() + " tag name", ex); //NON-NLS
331  }
332  setDisplayName(TagUtils.getDecoratedTagDisplayName(tagName) + " \u200E(\u200E" + tagsCount + ")\u200E");
333  }
334 
335  @Override
336  protected Sheet createSheet() {
337  Sheet propertySheet = super.createSheet();
338  Sheet.Set properties = propertySheet.get(Sheet.PROPERTIES);
339  if (properties == null) {
340  properties = Sheet.createPropertiesSet();
341  propertySheet.put(properties);
342  }
343  properties.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "TagNameNode.createSheet.name.name"),
344  NbBundle.getMessage(this.getClass(), "TagNameNode.createSheet.name.displayName"), tagName.getDescription(), getName()));
345  return propertySheet;
346  }
347 
348  @Override
349  public <T> T accept(DisplayableItemNodeVisitor<T> visitor) {
350  // See classes derived from DisplayableItemNodeVisitor<AbstractNode>
351  // for behavior added using the Visitor pattern.
352  return visitor.visit(this);
353  }
354 
355  @Override
356  public boolean isLeafTypeNode() {
357  return false;
358  }
359 
360  @Override
361  public void update(Observable o, Object arg) {
363  }
364 
365  @Override
366  public String getItemType() {
367  return getClass().getName();
368  }
369  }
370 
375  private class TagTypeNodeFactory extends ChildFactory<String> {
376 
377  private final TagName tagName;
378  private final String CONTENT_TAG_TYPE_NODE_KEY = NbBundle.getMessage(TagNameNode.class, "TagNameNode.contentTagTypeNodeKey.text");
379  private final String BLACKBOARD_ARTIFACT_TAG_TYPE_NODE_KEY = NbBundle.getMessage(TagNameNode.class, "TagNameNode.bbArtTagTypeNodeKey.text");
380 
381  TagTypeNodeFactory(TagName tagName) {
382  super();
383  this.tagName = tagName;
384  }
385 
386  @Override
387  protected boolean createKeys(List<String> keys) {
388  keys.add(CONTENT_TAG_TYPE_NODE_KEY);
389  keys.add(BLACKBOARD_ARTIFACT_TAG_TYPE_NODE_KEY);
390  return true;
391  }
392 
393  @Override
394  protected Node createNodeForKey(String key) {
395  if (CONTENT_TAG_TYPE_NODE_KEY.equals(key)) {
396  return new ContentTagTypeNode(tagName);
397  } else if (BLACKBOARD_ARTIFACT_TAG_TYPE_NODE_KEY.equals(key)) {
398  return new BlackboardArtifactTagTypeNode(tagName);
399  } else {
400  Logger.getLogger(TagNameNode.class.getName()).log(Level.SEVERE, "{0} not a recognized key", key); //NON-NLS
401  return null;
402  }
403  }
404  }
405 
406  private final String CONTENT_DISPLAY_NAME = NbBundle.getMessage(ContentTagTypeNode.class, "ContentTagTypeNode.displayName.text");
407 
414  public class ContentTagTypeNode extends DisplayableItemNode implements Observer {
415 
416  private final String ICON_PATH = "org/sleuthkit/autopsy/images/tag-folder-blue-icon-16.png"; //NON-NLS
417  private final TagName tagName;
418 
419  public ContentTagTypeNode(TagName tagName) {
420  super(Children.create(new ContentTagNodeFactory(tagName), true), Lookups.singleton(tagName.getDisplayName() + " " + CONTENT_DISPLAY_NAME));
421  this.tagName = tagName;
422  super.setName(CONTENT_DISPLAY_NAME);
424  this.setIconBaseWithExtension(ICON_PATH);
425  tagResults.addObserver(this);
426  }
427 
428  private void updateDisplayName() {
429  long tagsCount = 0;
430  try {
431 
433  String userName = System.getProperty(USER_NAME_PROPERTY);
434  tagsCount = (filteringDSObjId > 0)
435  ? Case.getCurrentCaseThrows().getServices().getTagsManager().getContentTagsCountByTagNameForUser(tagName, filteringDSObjId, userName)
437  } else {
438  tagsCount = (filteringDSObjId > 0)
441  }
442  } catch (TskCoreException | NoCurrentCaseException ex) {
443  Logger.getLogger(ContentTagTypeNode.class.getName()).log(Level.SEVERE, "Failed to get content tags count for " + tagName.getDisplayName() + " tag name", ex); //NON-NLS
444  }
445  super.setDisplayName(CONTENT_DISPLAY_NAME + " (" + tagsCount + ")");
446  }
447 
448  @Override
449  protected Sheet createSheet() {
450  Sheet propertySheet = super.createSheet();
451  Sheet.Set properties = propertySheet.get(Sheet.PROPERTIES);
452  if (properties == null) {
453  properties = Sheet.createPropertiesSet();
454  propertySheet.put(properties);
455  }
456  properties.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "ContentTagTypeNode.createSheet.name.name"),
457  NbBundle.getMessage(this.getClass(), "ContentTagTypeNode.createSheet.name.displayName"), "", getName()));
458  return propertySheet;
459  }
460 
461  @Override
462  public <T> T accept(DisplayableItemNodeVisitor<T> visitor) {
463  return visitor.visit(this);
464  }
465 
466  @Override
467  public boolean isLeafTypeNode() {
468  return true;
469  }
470 
471  @Override
472  public void update(Observable o, Object arg) {
474  }
475 
476  @Override
477  public String getItemType() {
478  return getClass().getName();
479  }
480  }
481 
482  private class ContentTagNodeFactory extends ChildFactory<ContentTag> implements Observer {
483 
484  private final TagName tagName;
485 
486  ContentTagNodeFactory(TagName tagName) {
487  super();
488  this.tagName = tagName;
489  tagResults.addObserver(this);
490  }
491 
492  @Override
493  protected boolean createKeys(List<ContentTag> keys) {
494  // Use the content tags bearing the specified tag name as the keys.
495  try {
496  List<ContentTag> contentTags = (filteringDSObjId > 0)
500  String userName = System.getProperty(USER_NAME_PROPERTY);
501  for (ContentTag tag : contentTags) {
502  if (userName.equals(tag.getUserName())) {
503  keys.add(tag);
504  }
505  }
506  } else {
507  keys.addAll(contentTags);
508  }
509  } catch (TskCoreException | NoCurrentCaseException ex) {
510  Logger.getLogger(ContentTagNodeFactory.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
511  }
512  return true;
513  }
514 
515  @Override
516  protected Node createNodeForKey(ContentTag key) {
517  // The content tags to be wrapped are used as the keys.
518  return new ContentTagNode(key);
519  }
520 
521  @Override
522  public void update(Observable o, Object arg) {
523  refresh(true);
524  }
525  }
526 
527  private final String ARTIFACT_DISPLAY_NAME = NbBundle.getMessage(BlackboardArtifactTagTypeNode.class, "BlackboardArtifactTagTypeNode.displayName.text");
528 
535  public class BlackboardArtifactTagTypeNode extends DisplayableItemNode implements Observer {
536 
537  private final TagName tagName;
538  private final String ICON_PATH = "org/sleuthkit/autopsy/images/tag-folder-blue-icon-16.png"; //NON-NLS
539 
540  public BlackboardArtifactTagTypeNode(TagName tagName) {
541  super(Children.create(new BlackboardArtifactTagNodeFactory(tagName), true), Lookups.singleton(tagName.getDisplayName() + " " + ARTIFACT_DISPLAY_NAME));
542  this.tagName = tagName;
543  super.setName(ARTIFACT_DISPLAY_NAME);
544  this.setIconBaseWithExtension(ICON_PATH);
546  tagResults.addObserver(this);
547  }
548 
549  private void updateDisplayName() {
550  long tagsCount = 0;
551  try {
553  String userName = System.getProperty(USER_NAME_PROPERTY);
554  tagsCount = (filteringDSObjId > 0)
557  } else {
558  tagsCount = (filteringDSObjId > 0)
561  }
562  } catch (TskCoreException | NoCurrentCaseException ex) {
563  Logger.getLogger(BlackboardArtifactTagTypeNode.class.getName()).log(Level.SEVERE, "Failed to get blackboard artifact tags count for " + tagName.getDisplayName() + " tag name", ex); //NON-NLS
564  }
565  super.setDisplayName(ARTIFACT_DISPLAY_NAME + " (" + tagsCount + ")");
566  }
567 
568  @Override
569  protected Sheet createSheet() {
570  Sheet propertySheet = super.createSheet();
571  Sheet.Set properties = propertySheet.get(Sheet.PROPERTIES);
572  if (properties == null) {
573  properties = Sheet.createPropertiesSet();
574  propertySheet.put(properties);
575  }
576  properties.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "BlackboardArtifactTagTypeNode.createSheet.name.name"),
577  NbBundle.getMessage(this.getClass(), "BlackboardArtifactTagTypeNode.createSheet.name.displayName"), "", getName()));
578  return propertySheet;
579  }
580 
581  @Override
582  public <T> T accept(DisplayableItemNodeVisitor<T> visitor) {
583  return visitor.visit(this);
584  }
585 
586  @Override
587  public boolean isLeafTypeNode() {
588  return true;
589  }
590 
591  @Override
592  public void update(Observable o, Object arg) {
594  }
595 
596  @Override
597  public String getItemType() {
598  return getClass().getName();
599  }
600  }
601 
602  private class BlackboardArtifactTagNodeFactory extends ChildFactory<BlackboardArtifactTag> implements Observer {
603 
604  private final TagName tagName;
605 
606  BlackboardArtifactTagNodeFactory(TagName tagName) {
607  super();
608  this.tagName = tagName;
609  tagResults.addObserver(this);
610  }
611 
612  @Override
613  protected boolean createKeys(List<BlackboardArtifactTag> keys) {
614  try {
615  // Use the blackboard artifact tags bearing the specified tag name as the keys.
616  List<BlackboardArtifactTag> artifactTags = (filteringDSObjId > 0)
620  String userName = System.getProperty(USER_NAME_PROPERTY);
621  for (BlackboardArtifactTag tag : artifactTags) {
622  if (userName.equals(tag.getUserName())) {
623  keys.add(tag);
624  }
625  }
626  } else {
627  keys.addAll(artifactTags);
628  }
629  } catch (TskCoreException | NoCurrentCaseException ex) {
630  Logger.getLogger(BlackboardArtifactTagNodeFactory.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
631  }
632  return true;
633  }
634 
635  @Override
636  protected Node createNodeForKey(BlackboardArtifactTag key) {
637  // The blackboard artifact tags to be wrapped are used as the keys.
638  return new BlackboardArtifactTagNode(key);
639  }
640 
641  @Override
642  public void update(Observable o, Object arg) {
643  refresh(true);
644  }
645  }
646 }
static synchronized IngestManager getInstance()
void update(Observable o, Object arg)
Definition: Tags.java:361
List< TagName > getTagNamesInUseForUser(String userName)
static String getTagsDisplayName()
Definition: Tags.java:81
void removeIngestJobEventListener(final PropertyChangeListener listener)
boolean createKeys(List< ContentTag > keys)
Definition: Tags.java:493
void addIngestJobEventListener(final PropertyChangeListener listener)
static final String DISPLAY_NAME
Definition: Tags.java:60
boolean createKeys(List< BlackboardArtifactTag > keys)
Definition: Tags.java:613
static final Set< IngestManager.IngestJobEvent > INGEST_JOB_EVENTS_OF_INTEREST
Definition: Tags.java:61
List< BlackboardArtifactTag > getBlackboardArtifactTagsByTagName(TagName tagName)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static String getDecoratedTagDisplayName(TagName tagName)
Definition: TagUtils.java:55
static void addEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:711
List< ContentTag > getContentTagsByTagName(TagName tagName)
static void removeEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:756
long getContentTagsCountByTagNameForUser(TagName tagName, String userName)
long getBlackboardArtifactTagsCountByTagNameForUser(TagName tagName, String userName)
static final String USER_NAME_PROPERTY
Definition: Tags.java:62

Copyright © 2012-2022 Basis Technology. Generated on: Fri Dec 2 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.