Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
InterestingHits.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.BlackboardAttribute;
47 import org.sleuthkit.datamodel.SleuthkitCase;
48 import org.sleuthkit.datamodel.SleuthkitCase.CaseDbQuery;
49 import org.sleuthkit.datamodel.TskCoreException;
50 
51 public class InterestingHits implements AutopsyVisitableItem {
52 
53  private static final String INTERESTING_ITEMS = NbBundle
54  .getMessage(InterestingHits.class, "InterestingHits.interestingItems.text");
55  private static final String DISPLAY_NAME = NbBundle.getMessage(InterestingHits.class, "InterestingHits.displayName.text");
56  private static final Logger logger = Logger.getLogger(InterestingHits.class.getName());
57  private SleuthkitCase skCase;
59 
60  public InterestingHits(SleuthkitCase skCase) {
61  this.skCase = skCase;
62  interestingResults.update();
63  }
64 
65  private class InterestingResults extends Observable {
66 
67  // NOTE: the map can be accessed by multiple worker threads and needs to be synchronized
68  private final Map<String, Set<Long>> interestingItemsMap = new LinkedHashMap<>();
69 
70  public List<String> getSetNames() {
71  List<String> setNames;
72  synchronized (interestingItemsMap) {
73  setNames = new ArrayList<>(interestingItemsMap.keySet());
74  }
75  Collections.sort(setNames);
76  return setNames;
77  }
78 
79  public Set<Long> getArtifactIds(String setName) {
80  synchronized (interestingItemsMap) {
81  return interestingItemsMap.get(setName);
82  }
83  }
84 
85  public void update() {
86  synchronized (interestingItemsMap) {
87  interestingItemsMap.clear();
88  }
89  loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT);
90  loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT);
91  setChanged();
92  notifyObservers();
93  }
94 
95  /*
96  * Reads the artifacts of specified type, grouped by Set, and loads into
97  * the interestingItemsMap
98  */
99  @SuppressWarnings("deprecation")
100  private void loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE artType) {
101  if (skCase == null) {
102  return;
103  }
104 
105  int setNameId = BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID();
106  int artId = artType.getTypeID();
107  String query = "SELECT value_text,blackboard_attributes.artifact_id,attribute_type_id " //NON-NLS
108  + "FROM blackboard_attributes,blackboard_artifacts WHERE " //NON-NLS
109  + "attribute_type_id=" + setNameId //NON-NLS
110  + " AND blackboard_attributes.artifact_id=blackboard_artifacts.artifact_id" //NON-NLS
111  + " AND blackboard_artifacts.artifact_type_id=" + artId; //NON-NLS
112 
113  try (CaseDbQuery dbQuery = skCase.executeQuery(query)) {
114  synchronized (interestingItemsMap) {
115  ResultSet resultSet = dbQuery.getResultSet();
116  while (resultSet.next()) {
117  String value = resultSet.getString("value_text"); //NON-NLS
118  long artifactId = resultSet.getLong("artifact_id"); //NON-NLS
119  if (!interestingItemsMap.containsKey(value)) {
120  interestingItemsMap.put(value, new HashSet<>());
121  }
122  interestingItemsMap.get(value).add(artifactId);
123  }
124  }
125  } catch (TskCoreException | SQLException ex) {
126  logger.log(Level.WARNING, "SQL Exception occurred: ", ex); //NON-NLS
127  }
128  }
129  }
130 
131  @Override
132  public <T> T accept(AutopsyItemVisitor<T> v) {
133  return v.visit(this);
134  }
135 
139  public class RootNode extends DisplayableItemNode {
140 
141  public RootNode() {
142  super(Children.create(new SetNameFactory(), true), Lookups.singleton(DISPLAY_NAME));
143  super.setName(INTERESTING_ITEMS);
144  super.setDisplayName(DISPLAY_NAME);
145  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/interesting_item.png"); //NON-NLS
146  }
147 
148  @Override
149  public boolean isLeafTypeNode() {
150  return false;
151  }
152 
153  @Override
154  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
155  return v.visit(this);
156  }
157 
158  @Override
159  protected Sheet createSheet() {
160  Sheet s = super.createSheet();
161  Sheet.Set ss = s.get(Sheet.PROPERTIES);
162  if (ss == null) {
163  ss = Sheet.createPropertiesSet();
164  s.put(ss);
165  }
166 
167  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
168  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.displayName"),
169  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.desc"),
170  getName()));
171 
172  return s;
173  }
174 
175  /*
176  * TODO (AUT-1849): Correct or remove peristent column reordering code
177  *
178  * Added to support this feature.
179  */
180 // @Override
181 // public String getItemType() {
182 // return "InterestingHitsRoot"; //NON-NLS
183 // }
184  }
185 
186  private class SetNameFactory extends ChildFactory.Detachable<String> implements Observer {
187 
188  /*
189  * This should probably be in the top-level 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 {
212  ModuleDataEvent eventData = (ModuleDataEvent) evt.getOldValue();
213  if (null != eventData && (eventData.getBlackboardArtifactType().getTypeID() == BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT.getTypeID()
214  || eventData.getBlackboardArtifactType().getTypeID() == BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT.getTypeID())) {
215  interestingResults.update();
216  }
217  } catch (IllegalStateException notUsed) {
221  }
222  } else if (eventType.equals(IngestManager.IngestJobEvent.COMPLETED.toString())
223  || eventType.equals(IngestManager.IngestJobEvent.CANCELLED.toString())) {
230  try {
232  interestingResults.update();
233  } catch (IllegalStateException notUsed) {
237  }
238  } else if (eventType.equals(Case.Events.CURRENT_CASE.toString())) {
239  // case was closed. Remove listeners so that we don't get called with a stale case handle
240  if (evt.getNewValue() == null) {
241  removeNotify();
242  skCase = null;
243  }
244  }
245  }
246  };
247 
248  @Override
249  protected void addNotify() {
253  interestingResults.update();
254  interestingResults.addObserver(this);
255  }
256 
257  @Override
258  protected void removeNotify() {
262  interestingResults.deleteObserver(this);
263  }
264 
265  @Override
266  protected boolean createKeys(List<String> list) {
267  list.addAll(interestingResults.getSetNames());
268  return true;
269  }
270 
271  @Override
272  protected Node createNodeForKey(String key) {
273  return new SetNameNode(key);
274  }
275 
276  @Override
277  public void update(Observable o, Object arg) {
278  refresh(true);
279  }
280  }
281 
282  public class SetNameNode extends DisplayableItemNode implements Observer {
283 
284  private final String setName;
285 
286  public SetNameNode(String setName) {//, Set<Long> children) {
287  super(Children.create(new HitFactory(setName), true), Lookups.singleton(setName));
288  this.setName = setName;
289  super.setName(setName);
291  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/interesting_item.png"); //NON-NLS
292  interestingResults.addObserver(this);
293  }
294 
295  private void updateDisplayName() {
296  super.setDisplayName(setName + " (" + interestingResults.getArtifactIds(setName).size() + ")");
297  }
298 
299  @Override
300  public boolean isLeafTypeNode() {
301  return true;
302  }
303 
304  @Override
305  protected Sheet createSheet() {
306  Sheet s = super.createSheet();
307  Sheet.Set ss = s.get(Sheet.PROPERTIES);
308  if (ss == null) {
309  ss = Sheet.createPropertiesSet();
310  s.put(ss);
311  }
312 
313  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
314  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
315  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.desc"),
316  getName()));
317 
318  return s;
319  }
320 
321  @Override
322  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
323  return v.visit(this);
324  }
325 
326  @Override
327  public void update(Observable o, Object arg) {
329  }
330 
331  /*
332  * TODO (AUT-1849): Correct or remove peristent column reordering code
333  *
334  * Added to support this feature.
335  */
336 // @Override
337 // public String getItemType() {
338 // return "InterestingHitsSetName"; //NON-NLS
339 // }
340  }
341 
342  private class HitFactory extends ChildFactory<Long> implements Observer {
343 
344  private final String setName;
345 
346  private HitFactory(String setName) {
347  super();
348  this.setName = setName;
349  interestingResults.addObserver(this);
350  }
351 
352  @Override
353  protected boolean createKeys(List<Long> list) {
354  for (long l : interestingResults.getArtifactIds(setName)) {
355  list.add(l);
356  }
357  return true;
358  }
359 
360  @Override
361  protected Node createNodeForKey(Long l) {
362  if (skCase == null) {
363  return null;
364  }
365  try {
366  return new BlackboardArtifactNode(skCase.getBlackboardArtifact(l));
367  } catch (TskCoreException ex) {
368  logger.log(Level.SEVERE, "Error creating new Blackboard Artifact node", ex); //NON-NLS
369  return null;
370  }
371  }
372 
373  @Override
374  public void update(Observable o, Object arg) {
375  refresh(true);
376  }
377  }
378 }
BlackboardArtifact.Type getBlackboardArtifactType()
void removeIngestModuleEventListener(final PropertyChangeListener listener)
static synchronized IngestManager getInstance()
static void removePropertyChangeListener(PropertyChangeListener listener)
Definition: Case.java:318
void loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE artType)
void removeIngestJobEventListener(final PropertyChangeListener listener)
void addIngestJobEventListener(final PropertyChangeListener listener)
static void addPropertyChangeListener(PropertyChangeListener listener)
Definition: Case.java:306
void addIngestModuleEventListener(final PropertyChangeListener listener)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161

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