Autopsy  4.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-2015 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.HashSet;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Observable;
32 import java.util.Observer;
33 import java.util.Set;
34 import java.util.logging.Level;
35 import org.openide.nodes.ChildFactory;
36 import org.openide.nodes.Children;
37 import org.openide.nodes.Node;
38 import org.openide.nodes.Sheet;
39 import org.openide.util.NbBundle;
40 import org.openide.util.lookup.Lookups;
45 import org.sleuthkit.datamodel.BlackboardArtifact;
46 import org.sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;
47 import org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE;
48 import org.sleuthkit.datamodel.SleuthkitCase;
49 import org.sleuthkit.datamodel.SleuthkitCase.CaseDbQuery;
50 import org.sleuthkit.datamodel.TskCoreException;
51 import org.sleuthkit.datamodel.TskException;
52 
56 public class HashsetHits implements AutopsyVisitableItem {
57 
58  private static final String HASHSET_HITS = BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT.getLabel();
59  private static final String DISPLAY_NAME = BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT.getDisplayName();
60  private static final Logger logger = Logger.getLogger(HashsetHits.class.getName());
61  private SleuthkitCase skCase;
63 
64  public HashsetHits(SleuthkitCase skCase) {
65  this.skCase = skCase;
66  hashsetResults = new HashsetResults();
67  }
68 
69  @Override
70  public <T> T accept(AutopsyItemVisitor<T> v) {
71  return v.visit(this);
72  }
73 
78  private class HashsetResults extends Observable {
79 
80  // maps hashset name to list of artifacts for that set
81 
82  private final Map<String, Set<Long>> hashSetHitsMap = new LinkedHashMap<>();
83 
84  HashsetResults() {
85  update();
86  }
87 
88  List<String> getSetNames() {
89  List<String> names = new ArrayList<>(hashSetHitsMap.keySet());
90  Collections.sort(names);
91  return names;
92  }
93 
94  Set<Long> getArtifactIds(String hashSetName) {
95  return hashSetHitsMap.get(hashSetName);
96  }
97 
98  @SuppressWarnings("deprecation")
99  final void update() {
100  hashSetHitsMap.clear();
101 
102  if (skCase == null) {
103  return;
104  }
105 
106  int setNameId = ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID();
107  int artId = ARTIFACT_TYPE.TSK_HASHSET_HIT.getTypeID();
108  String query = "SELECT value_text,blackboard_attributes.artifact_id,attribute_type_id " //NON-NLS
109  + "FROM blackboard_attributes,blackboard_artifacts WHERE " //NON-NLS
110  + "attribute_type_id=" + setNameId //NON-NLS
111  + " AND blackboard_attributes.artifact_id=blackboard_artifacts.artifact_id" //NON-NLS
112  + " AND blackboard_artifacts.artifact_type_id=" + artId; //NON-NLS
113 
114  try (CaseDbQuery dbQuery = skCase.executeQuery(query)) {
115  ResultSet resultSet = dbQuery.getResultSet();
116  while (resultSet.next()) {
117  String setName = resultSet.getString("value_text"); //NON-NLS
118  long artifactId = resultSet.getLong("artifact_id"); //NON-NLS
119  if (!hashSetHitsMap.containsKey(setName)) {
120  hashSetHitsMap.put(setName, new HashSet<Long>());
121  }
122  hashSetHitsMap.get(setName).add(artifactId);
123  }
124  } catch (TskCoreException | SQLException ex) {
125  logger.log(Level.WARNING, "SQL Exception occurred: ", ex); //NON-NLS
126  }
127 
128  setChanged();
129  notifyObservers();
130  }
131  }
132 
136  public class RootNode extends DisplayableItemNode {
137 
138  public RootNode() {
139  super(Children.create(new HashsetNameFactory(), true), Lookups.singleton(DISPLAY_NAME));
140  super.setName(HASHSET_HITS);
141  super.setDisplayName(DISPLAY_NAME);
142  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/hashset_hits.png"); //NON-NLS
143  }
144 
145  @Override
146  public boolean isLeafTypeNode() {
147  return false;
148  }
149 
150  @Override
151  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
152  return v.visit(this);
153  }
154 
155  @Override
156  protected Sheet createSheet() {
157  Sheet s = super.createSheet();
158  Sheet.Set ss = s.get(Sheet.PROPERTIES);
159  if (ss == null) {
160  ss = Sheet.createPropertiesSet();
161  s.put(ss);
162  }
163 
164  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.name"),
165  NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.displayName"),
166  NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.desc"),
167  getName()));
168 
169  return s;
170  }
171 
172  /*
173  * TODO (AUT-1849): Correct or remove peristent column reordering code
174  *
175  * Added to support this feature.
176  */
177 // @Override
178 // public String getItemType() {
179 // return "HashsetRoot"; //NON-NLS
180 // }
181  }
182 
186  private class HashsetNameFactory extends ChildFactory.Detachable<String> implements Observer {
187 
188  /*
189  * This should probably be in the HashsetHits class, but the factory has
190  * nice methods for its startup and shutdown, so it seemed like a
191  * cleaner place to register the property change listener.
192  */
193  private final PropertyChangeListener pcl = new PropertyChangeListener() {
194  @Override
195  public void propertyChange(PropertyChangeEvent evt) {
196  String eventType = evt.getPropertyName();
197  if (eventType.equals(IngestManager.IngestModuleEvent.DATA_ADDED.toString())) {
204  try {
211  ModuleDataEvent eventData = (ModuleDataEvent) evt.getOldValue();
212  if (null != eventData && eventData.getBlackboardArtifactType().getTypeID() == ARTIFACT_TYPE.TSK_HASHSET_HIT.getTypeID()) {
213  hashsetResults.update();
214  }
215  } catch (IllegalStateException notUsed) {
219  }
220  } else if (eventType.equals(IngestManager.IngestJobEvent.COMPLETED.toString())
221  || eventType.equals(IngestManager.IngestJobEvent.CANCELLED.toString())) {
228  try {
230  hashsetResults.update();
231  } catch (IllegalStateException notUsed) {
235  }
236  } else if (eventType.equals(Case.Events.CURRENT_CASE.toString())) {
237  // case was closed. Remove listeners so that we don't get called with a stale case handle
238  if (evt.getNewValue() == null) {
239  removeNotify();
240  skCase = null;
241  }
242  }
243  }
244  };
245 
246  @Override
247  protected void addNotify() {
251  hashsetResults.update();
252  hashsetResults.addObserver(this);
253  }
254 
255  @Override
256  protected void removeNotify() {
260  hashsetResults.deleteObserver(this);
261  }
262 
263  @Override
264  protected boolean createKeys(List<String> list) {
265  list.addAll(hashsetResults.getSetNames());
266  return true;
267  }
268 
269  @Override
270  protected Node createNodeForKey(String key) {
271  return new HashsetNameNode(key);
272  }
273 
274  @Override
275  public void update(Observable o, Object arg) {
276  refresh(true);
277  }
278  }
279 
283  public class HashsetNameNode extends DisplayableItemNode implements Observer {
284 
285  private final String hashSetName;
286 
287  public HashsetNameNode(String hashSetName) {
288  super(Children.create(new HitFactory(hashSetName), true), Lookups.singleton(hashSetName));
289  super.setName(hashSetName);
290  this.hashSetName = hashSetName;
292  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/hashset_hits.png"); //NON-NLS
293  hashsetResults.addObserver(this);
294  }
295 
299  private void updateDisplayName() {
300  super.setDisplayName(hashSetName + " (" + hashsetResults.getArtifactIds(hashSetName).size() + ")");
301  }
302 
303  @Override
304  public boolean isLeafTypeNode() {
305  return true;
306  }
307 
308  @Override
309  protected Sheet createSheet() {
310  Sheet s = super.createSheet();
311  Sheet.Set ss = s.get(Sheet.PROPERTIES);
312  if (ss == null) {
313  ss = Sheet.createPropertiesSet();
314  s.put(ss);
315  }
316 
317  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.name"),
318  NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.displayName"),
319  NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.desc"),
320  getName()));
321 
322  return s;
323  }
324 
325  @Override
326  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
327  return v.visit(this);
328  }
329 
330  @Override
331  public void update(Observable o, Object arg) {
333  }
334 
335  /*
336  * TODO (AUT-1849): Correct or remove peristent column reordering code
337  *
338  * Added to support this feature.
339  */
340 // @Override
341 // public String getItemType() {
342 // return "HashsetName"; //NON-NLS
343 // }
344  }
345 
349  private class HitFactory extends ChildFactory.Detachable<Long> implements Observer {
350 
351  private String hashsetName;
352 
353  private HitFactory(String hashsetName) {
354  super();
355  this.hashsetName = hashsetName;
356  }
357 
358  @Override
359  protected void addNotify() {
360  hashsetResults.addObserver(this);
361  }
362 
363  @Override
364  protected void removeNotify() {
365  hashsetResults.deleteObserver(this);
366  }
367 
368  @Override
369  protected boolean createKeys(List<Long> list) {
370  list.addAll(hashsetResults.getArtifactIds(hashsetName));
371  return true;
372  }
373 
374  @Override
375  protected Node createNodeForKey(Long id) {
376  if (skCase == null) {
377  return null;
378  }
379 
380  try {
381  BlackboardArtifact art = skCase.getBlackboardArtifact(id);
382  return new BlackboardArtifactNode(art);
383  } catch (TskException ex) {
384  logger.log(Level.WARNING, "TSK Exception occurred", ex); //NON-NLS
385  }
386  return null;
387  }
388 
389  @Override
390  public void update(Observable o, Object arg) {
391  refresh(true);
392  }
393  }
394 }
BlackboardArtifact.Type getBlackboardArtifactType()
void removeIngestModuleEventListener(final PropertyChangeListener listener)
static synchronized IngestManager getInstance()
void removeIngestJobEventListener(final PropertyChangeListener listener)
void addIngestJobEventListener(final PropertyChangeListener listener)
static synchronized void removePropertyChangeListener(PropertyChangeListener listener)
Definition: Case.java:1305
void addIngestModuleEventListener(final PropertyChangeListener listener)
static synchronized void addPropertyChangeListener(PropertyChangeListener listener)
Definition: Case.java:1292
synchronized static Logger getLogger(String name)
Definition: Logger.java:166

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.