Autopsy  4.10.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
HashsetHits.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 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.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.EnumSet;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Objects;
34 import java.util.Observable;
35 import java.util.Observer;
36 import java.util.Set;
37 import java.util.logging.Level;
38 import org.openide.nodes.ChildFactory;
39 import org.openide.nodes.Children;
40 import org.openide.nodes.Node;
41 import org.openide.nodes.Sheet;
42 import org.openide.util.NbBundle;
43 import org.openide.util.lookup.Lookups;
51 import org.sleuthkit.datamodel.BlackboardArtifact;
52 import org.sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;
53 import org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE;
54 import org.sleuthkit.datamodel.SleuthkitCase;
55 import org.sleuthkit.datamodel.SleuthkitCase.CaseDbQuery;
56 import org.sleuthkit.datamodel.TskCoreException;
57 import org.sleuthkit.datamodel.TskException;
58 
62 public class HashsetHits implements AutopsyVisitableItem {
63 
64  private static final String HASHSET_HITS = BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT.getLabel();
65  private static final String DISPLAY_NAME = BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT.getDisplayName();
66  private static final Logger logger = Logger.getLogger(HashsetHits.class.getName());
67  private SleuthkitCase skCase;
69  private final long datasourceObjId;
70 
71 
78  public HashsetHits(SleuthkitCase skCase) {
79  this(skCase, 0);
80  }
81 
89  public HashsetHits(SleuthkitCase skCase, long objId) {
90  this.skCase = skCase;
91  this.datasourceObjId = objId;
92  hashsetResults = new HashsetResults();
93  }
94 
95  @Override
96  public <T> T accept(AutopsyItemVisitor<T> visitor) {
97  return visitor.visit(this);
98  }
99 
104  private class HashsetResults extends Observable {
105 
106  // maps hashset name to list of artifacts for that set
107  // NOTE: the map can be accessed by multiple worker threads and needs to be synchronized
108  private final Map<String, Set<Long>> hashSetHitsMap = new LinkedHashMap<>();
109 
110  HashsetResults() {
111  update();
112  }
113 
114  List<String> getSetNames() {
115  List<String> names;
116  synchronized (hashSetHitsMap) {
117  names = new ArrayList<>(hashSetHitsMap.keySet());
118  }
119  Collections.sort(names);
120  return names;
121  }
122 
123  Set<Long> getArtifactIds(String hashSetName) {
124  synchronized (hashSetHitsMap) {
125  return hashSetHitsMap.get(hashSetName);
126  }
127  }
128 
129  @SuppressWarnings("deprecation")
130  final void update() {
131  synchronized (hashSetHitsMap) {
132  hashSetHitsMap.clear();
133  }
134 
135  if (skCase == null) {
136  return;
137  }
138 
139  int setNameId = ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID();
140  int artId = ARTIFACT_TYPE.TSK_HASHSET_HIT.getTypeID();
141  String query = "SELECT value_text,blackboard_attributes.artifact_id,attribute_type_id " //NON-NLS
142  + "FROM blackboard_attributes,blackboard_artifacts WHERE " //NON-NLS
143  + "attribute_type_id=" + setNameId //NON-NLS
144  + " AND blackboard_attributes.artifact_id=blackboard_artifacts.artifact_id" //NON-NLS
145  + " AND blackboard_artifacts.artifact_type_id=" + artId; //NON-NLS
146  if (Objects.equals(CasePreferences.getGroupItemsInTreeByDataSource(), true)) {
147  query += " AND blackboard_artifacts.data_source_obj_id = " + datasourceObjId;
148  }
149 
150  try (CaseDbQuery dbQuery = skCase.executeQuery(query)) {
151  ResultSet resultSet = dbQuery.getResultSet();
152  synchronized (hashSetHitsMap) {
153  while (resultSet.next()) {
154  String setName = resultSet.getString("value_text"); //NON-NLS
155  long artifactId = resultSet.getLong("artifact_id"); //NON-NLS
156  if (!hashSetHitsMap.containsKey(setName)) {
157  hashSetHitsMap.put(setName, new HashSet<Long>());
158  }
159  hashSetHitsMap.get(setName).add(artifactId);
160  }
161  }
162  } catch (TskCoreException | SQLException ex) {
163  logger.log(Level.WARNING, "SQL Exception occurred: ", ex); //NON-NLS
164  }
165 
166  setChanged();
167  notifyObservers();
168  }
169  }
170 
174  public class RootNode extends DisplayableItemNode {
175 
176  public RootNode() {
177  super(Children.create(new HashsetNameFactory(), true), Lookups.singleton(DISPLAY_NAME));
178  super.setName(HASHSET_HITS);
179  super.setDisplayName(DISPLAY_NAME);
180  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/hashset_hits.png"); //NON-NLS
181  }
182 
183  @Override
184  public boolean isLeafTypeNode() {
185  return false;
186  }
187 
188  @Override
189  public <T> T accept(DisplayableItemNodeVisitor<T> visitor) {
190  return visitor.visit(this);
191  }
192 
193  @Override
194  protected Sheet createSheet() {
195  Sheet sheet = super.createSheet();
196  Sheet.Set sheetSet = sheet.get(Sheet.PROPERTIES);
197  if (sheetSet == null) {
198  sheetSet = Sheet.createPropertiesSet();
199  sheet.put(sheetSet);
200  }
201 
202  sheetSet.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.name"),
203  NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.displayName"),
204  NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.desc"),
205  getName()));
206 
207  return sheet;
208  }
209 
210  @Override
211  public String getItemType() {
212  return getClass().getName();
213  }
214  }
215 
219  private class HashsetNameFactory extends ChildFactory.Detachable<String> implements Observer {
220 
221  /*
222  * This should probably be in the HashsetHits class, but the factory has
223  * nice methods for its startup and shutdown, so it seemed like a
224  * cleaner place to register the property change listener.
225  */
226  private final PropertyChangeListener pcl = new PropertyChangeListener() {
227  @Override
228  public void propertyChange(PropertyChangeEvent evt) {
229  String eventType = evt.getPropertyName();
230  if (eventType.equals(IngestManager.IngestModuleEvent.DATA_ADDED.toString())) {
237  try {
244  ModuleDataEvent eventData = (ModuleDataEvent) evt.getOldValue();
245  if (null != eventData && eventData.getBlackboardArtifactType().getTypeID() == ARTIFACT_TYPE.TSK_HASHSET_HIT.getTypeID()) {
246  hashsetResults.update();
247  }
248  } catch (NoCurrentCaseException notUsed) {
252  }
253  } else if (eventType.equals(IngestManager.IngestJobEvent.COMPLETED.toString())
254  || eventType.equals(IngestManager.IngestJobEvent.CANCELLED.toString())) {
261  try {
263  hashsetResults.update();
264  } catch (NoCurrentCaseException notUsed) {
268  }
269  } else if (eventType.equals(Case.Events.CURRENT_CASE.toString())) {
270  // case was closed. Remove listeners so that we don't get called with a stale case handle
271  if (evt.getNewValue() == null) {
272  removeNotify();
273  skCase = null;
274  }
275  }
276  }
277  };
278 
279  @Override
280  protected void addNotify() {
284  hashsetResults.update();
285  hashsetResults.addObserver(this);
286  }
287 
288  @Override
289  protected void removeNotify() {
293  hashsetResults.deleteObserver(this);
294  }
295 
296  @Override
297  protected boolean createKeys(List<String> list) {
298  list.addAll(hashsetResults.getSetNames());
299  return true;
300  }
301 
302  @Override
303  protected Node createNodeForKey(String key) {
304  return new HashsetNameNode(key);
305  }
306 
307  @Override
308  public void update(Observable o, Object arg) {
309  refresh(true);
310  }
311  }
312 
316  public class HashsetNameNode extends DisplayableItemNode implements Observer {
317 
318  private final String hashSetName;
319 
320  public HashsetNameNode(String hashSetName) {
321  super(Children.create(new HitFactory(hashSetName), true), Lookups.singleton(hashSetName));
322  super.setName(hashSetName);
323  this.hashSetName = hashSetName;
325  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/hashset_hits.png"); //NON-NLS
326  hashsetResults.addObserver(this);
327  }
328 
332  private void updateDisplayName() {
333  super.setDisplayName(hashSetName + " (" + hashsetResults.getArtifactIds(hashSetName).size() + ")");
334  }
335 
336  @Override
337  public boolean isLeafTypeNode() {
338  return true;
339  }
340 
341  @Override
342  protected Sheet createSheet() {
343  Sheet sheet = super.createSheet();
344  Sheet.Set sheetSet = sheet.get(Sheet.PROPERTIES);
345  if (sheetSet == null) {
346  sheetSet = Sheet.createPropertiesSet();
347  sheet.put(sheetSet);
348  }
349 
350  sheetSet.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.name"),
351  NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.displayName"),
352  NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.desc"),
353  getName()));
354 
355  return sheet;
356  }
357 
358  @Override
359  public <T> T accept(DisplayableItemNodeVisitor<T> visitor) {
360  return visitor.visit(this);
361  }
362 
363  @Override
364  public void update(Observable o, Object arg) {
366  }
367 
368  @Override
369  public String getItemType() {
374  return getClass().getName();
375  }
376  }
377 
381  private class HitFactory extends ChildFactory.Detachable<Long> implements Observer {
382 
383  private String hashsetName;
384  private Map<Long, BlackboardArtifact> artifactHits = new HashMap<>();
385 
386  private HitFactory(String hashsetName) {
387  super();
388  this.hashsetName = hashsetName;
389  }
390 
391  @Override
392  protected void addNotify() {
393  hashsetResults.addObserver(this);
394  }
395 
396  @Override
397  protected void removeNotify() {
398  hashsetResults.deleteObserver(this);
399  }
400 
401  @Override
402  protected boolean createKeys(List<Long> list) {
403 
404  if (skCase == null) {
405  return true;
406  }
407 
408  hashsetResults.getArtifactIds(hashsetName).forEach((id) -> {
409  try {
410  if (!artifactHits.containsKey(id)) {
411  BlackboardArtifact art = skCase.getBlackboardArtifact(id);
412  artifactHits.put(id, art);
413  }
414  } catch (TskException ex) {
415  logger.log(Level.SEVERE, "TSK Exception occurred", ex); //NON-NLS
416  }
417  });
418 
419  // Adding all keys at once is more efficient than adding one at a
420  // time because Netbeans triggers internal processing each time an
421  // element is added to the list.
422  list.addAll(artifactHits.keySet());
423  return true;
424  }
425 
426  @Override
427  protected Node createNodeForKey(Long id) {
428  BlackboardArtifact art = artifactHits.get(id);
429  return (null == art) ? null : new BlackboardArtifactNode(art);
430  }
431 
432  @Override
433  public void update(Observable o, Object arg) {
434  refresh(true);
435  }
436  }
437 }
BlackboardArtifact.Type getBlackboardArtifactType()
void removeIngestModuleEventListener(final PropertyChangeListener listener)
static synchronized IngestManager getInstance()
void removeIngestJobEventListener(final PropertyChangeListener listener)
void addIngestJobEventListener(final PropertyChangeListener listener)
void addIngestModuleEventListener(final PropertyChangeListener listener)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static void addEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:437
static void removeEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:482
HashsetHits(SleuthkitCase skCase, long objId)

Copyright © 2012-2018 Basis Technology. Generated on: Fri Mar 22 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.