Autopsy  4.8.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
AdHocSearchChildFactory.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 content 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.keywordsearch;
20 
21 import java.awt.EventQueue;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.concurrent.CancellationException;
30 import java.util.concurrent.ExecutionException;
31 import java.util.logging.Level;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34 import javax.swing.SwingWorker;
35 import org.netbeans.api.progress.ProgressHandle;
36 import org.openide.nodes.ChildFactory;
37 import org.openide.nodes.Children;
38 import org.openide.nodes.Node;
39 import org.openide.util.NbBundle;
40 import org.openide.util.lookup.Lookups;
52 import org.sleuthkit.datamodel.AbstractFile;
53 import org.sleuthkit.datamodel.BlackboardArtifact;
54 import org.sleuthkit.datamodel.BlackboardAttribute;
55 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD;
56 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_PREVIEW;
57 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_REGEXP;
58 import org.sleuthkit.datamodel.Content;
59 import org.sleuthkit.datamodel.SleuthkitCase;
60 import org.sleuthkit.datamodel.TskCoreException;
61 
69 class AdHocSearchChildFactory extends ChildFactory<KeyValue> {
70 
71  private static final Logger logger = Logger.getLogger(AdHocSearchChildFactory.class.getName());
72 
73  //common properties (superset of all Node properties) to be displayed as columns
74  static final List<String> COMMON_PROPERTIES
75  = Stream.concat(
76  Stream.of(
77  TSK_KEYWORD,
78  TSK_KEYWORD_REGEXP,
79  TSK_KEYWORD_PREVIEW)
80  .map(BlackboardAttribute.ATTRIBUTE_TYPE::getDisplayName),
81  Arrays.stream(AbstractAbstractFileNode.AbstractFilePropertyType.values())
82  .map(Object::toString))
83  .collect(Collectors.toList());
84 
85  private final Collection<AdHocQueryRequest> queryRequests;
86  private final boolean saveResults;
87 
94  AdHocSearchChildFactory(Collection<AdHocQueryRequest> queryRequests, boolean saveResults) {
95  this.queryRequests = queryRequests;
96  this.saveResults = saveResults;
97  }
98 
106  @Override
107  protected boolean createKeys(List<KeyValue> toPopulate) {
108 
109  for (AdHocQueryRequest queryRequest : queryRequests) {
113  if (!queryRequest.getQuery().validate()) {
114  //TODO mark the particular query node RED
115  break;
116  }
117 
118  //JMTODO: It looks like this map is not actually used for anything...
119  Map<String, Object> map = queryRequest.getProperties();
120  /*
121  * make sure all common properties are displayed as columns (since
122  * we are doing lazy child Node load we need to preinitialize
123  * properties when sending parent Node)
124  */
125  COMMON_PROPERTIES.stream()
126  .forEach((propertyType) -> map.put(propertyType, ""));
127  map.put(TSK_KEYWORD.getDisplayName(), queryRequest.getQueryString());
128  map.put(TSK_KEYWORD_REGEXP.getDisplayName(), !queryRequest.getQuery().isLiteral());
129 
130  createFlatKeys(queryRequest.getQuery(), toPopulate);
131  }
132 
133  // If there were no hits, make a single Node that will display that
134  // no results were found.
135  if (toPopulate.isEmpty()) {
136  toPopulate.add(new KeyValue("This KeyValue Is Empty", 0));
137  }
138 
139  return true;
140  }
141 
149  @NbBundle.Messages({"KeywordSearchResultFactory.query.exception.msg=Could not perform the query "})
150  private boolean createFlatKeys(KeywordSearchQuery queryRequest, List<KeyValue> toPopulate) {
151 
155  QueryResults queryResults;
156  try {
157  queryResults = queryRequest.performQuery();
158  } catch (KeywordSearchModuleException | NoOpenCoreException ex) {
159  logger.log(Level.SEVERE, "Could not perform the query " + queryRequest.getQueryString(), ex); //NON-NLS
160  MessageNotifyUtil.Notify.error(Bundle.KeywordSearchResultFactory_query_exception_msg() + queryRequest.getQueryString(), ex.getCause().getMessage());
161  return false;
162  }
163  SleuthkitCase tskCase;
164  try {
165  tskCase = Case.getCurrentCaseThrows().getSleuthkitCase();
166  } catch (NoCurrentCaseException ex) {
167  logger.log(Level.SEVERE, "There was no case open.", ex); //NON-NLS
168  return false;
169  }
170 
171  int hitNumber = 0;
172  List<KeywordHitKey> tempList = new ArrayList<>();
173  for (KeywordHit hit : getOneHitPerObject(queryResults)) {
174 
178  Map<String, Object> properties = new LinkedHashMap<>();
179  Content content;
180  String contentName;
181  try {
182  content = tskCase.getContentById(hit.getContentID());
183  if (content == null) {
184  logger.log(Level.SEVERE, "There was a error getting content by id."); //NON-NLS
185  return false;
186  }
187  } catch (TskCoreException ex) {
188  logger.log(Level.SEVERE, "There was a error getting content by id.", ex); //NON-NLS
189  return false;
190  }
191 
192  contentName = content.getName();
193  if (content instanceof AbstractFile) {
194  AbstractFsContentNode.fillPropertyMap(properties, (AbstractFile) content);
195  } else {
196  properties.put(LOCATION.toString(), contentName);
197  }
198 
202  if (hit.hasSnippet()) {
203  properties.put(TSK_KEYWORD_PREVIEW.getDisplayName(), hit.getSnippet());
204  }
205 
206  String hitName;
207  BlackboardArtifact artifact = null;
208  if (hit.isArtifactHit()) {
209  try {
210  artifact = tskCase.getBlackboardArtifact(hit.getArtifactID().get());
211  hitName = artifact.getDisplayName() + " Artifact"; //NON-NLS
212  } catch (TskCoreException ex) {
213  logger.log(Level.SEVERE, "Error getting blckboard artifact by id", ex);
214  return false;
215  }
216  } else {
217  hitName = contentName;
218  }
219  hitNumber++;
220  tempList.add(new KeywordHitKey(hitName, properties, hitNumber, hit.getSolrObjectId(), content, artifact, queryRequest, queryResults));
221 
222  }
223 
224  if (hitNumber != 0) {
225  // Add all the nodes to toPopulate at once. Minimizes node creation
226  // EDT threads, which can slow and/or hang the UI on large queries.
227  toPopulate.addAll(tempList);
228  }
229 
230  //write to bb
231  //cannot reuse snippet in BlackboardResultWriter
232  //because for regex searches in UI we compress results by showing a content per regex once (even if multiple term hits)
233  //whereas in bb we write every hit per content separately
234  new BlackboardResultWriter(queryResults, queryRequest.getKeywordList().getName(), saveResults).execute();
235 
236  return true;
237  }
238 
248  Collection<KeywordHit> getOneHitPerObject(QueryResults queryResults) {
249  HashMap<Long, KeywordHit> hits = new HashMap<>();
250  for (Keyword keyWord : queryResults.getKeywords()) {
251  for (KeywordHit hit : queryResults.getResults(keyWord)) {
252  // add hit with lowest SolrObjectID-Chunk-ID combination.
253  if (!hits.containsKey(hit.getSolrObjectId())) {
254  hits.put(hit.getSolrObjectId(), hit);
255  } else if (hit.getChunkId() < hits.get(hit.getSolrObjectId()).getChunkId()) {
256  hits.put(hit.getSolrObjectId(), hit);
257  }
258  }
259  }
260  return hits.values();
261  }
262 
263  @NbBundle.Messages({"KeywordSearchResultFactory.createNodeForKey.noResultsFound.text=No results found."})
264  @Override
265  protected Node createNodeForKey(KeyValue key) {
266  Node resultNode;
267 
268  if (key instanceof KeywordHitKey) {
269  AdHocQueryResult adHocQueryResult = new AdHocQueryResult((KeywordHitKey) key);
270 
275  ArrayList<Object> lookups = new ArrayList<>();
276  lookups.add(adHocQueryResult);
277  if (((KeywordHitKey) key).getContent() != null) {
278  lookups.add(((KeywordHitKey) key).getContent());
279  }
280  if (((KeywordHitKey) key).getArtifact() != null) {
281  lookups.add(((KeywordHitKey) key).getArtifact());
282  }
283 
284  Node kvNode = new KeyValueNode(key, Children.LEAF, Lookups.fixed(lookups.toArray()));
285 
286  //wrap in KeywordSearchFilterNode for the markup content, might need to override FilterNode for more customization
287  resultNode = new AdHocSearchFilterNode(kvNode);
288  } else {
289  resultNode = new EmptyNode("This Node Is Empty");
290  resultNode.setDisplayName(NbBundle.getMessage(this.getClass(), "KeywordSearchResultFactory.createNodeForKey.noResultsFound.text"));
291  }
292 
293  return resultNode;
294 
295  }
296 
301  final class AdHocQueryResult {
302 
303  private final long solrObjectId;
304  private final QueryResults results;
305 
313  AdHocQueryResult(KeywordHitKey key) {
314  this.solrObjectId = key.getSolrObjectId();
315  this.results = key.getHits();
316  }
317 
324  long getSolrObjectId() {
325  return solrObjectId;
326  }
327 
333  QueryResults getResults() {
334  return results;
335  }
336  }
337 
342  class KeywordHitKey extends KeyValue {
343 
344  private final long solrObjectId;
345 
346  private final Content content;
347  private final BlackboardArtifact artifact;
348  private final QueryResults hits;
349  private final KeywordSearchQuery query;
350 
365  KeywordHitKey(String name, Map<String, Object> map, int id, long solrObjectId, Content content, BlackboardArtifact artifact, KeywordSearchQuery query, QueryResults hits) {
366  super(name, map, id);
367  this.solrObjectId = solrObjectId;
368  this.content = content;
369  this.artifact = artifact;
370 
371  this.hits = hits;
372  this.query = query;
373  }
374 
375  Content getContent() {
376  return content;
377  }
378 
379  BlackboardArtifact getArtifact() {
380  return artifact;
381  }
382 
383  long getSolrObjectId() {
384  return solrObjectId;
385  }
386 
387  QueryResults getHits() {
388  return hits;
389  }
390 
391  KeywordSearchQuery getQuery() {
392  return query;
393  }
394  }
395 
400  static class BlackboardResultWriter extends SwingWorker<Void, Void> {
401 
402  private static final List<BlackboardResultWriter> WRITERS = new ArrayList<>();
403  private ProgressHandle progress;
404  private final KeywordSearchQuery query;
405  private final QueryResults hits;
406  private static final int QUERY_DISPLAY_LEN = 40;
407  private final boolean saveResults;
408 
409  BlackboardResultWriter(QueryResults hits, String listName, boolean saveResults) {
410  this.hits = hits;
411  this.query = hits.getQuery();
412  this.saveResults = saveResults;
413  }
414 
415  protected void finalizeWorker() {
416  deregisterWriter(this);
417  EventQueue.invokeLater(progress::finish);
418  }
419 
420  @Override
421  protected Void doInBackground() throws Exception {
422  registerWriter(this); //register (synchronized on class) outside of writerLock to prevent deadlock
423  final String queryStr = query.getQueryString();
424  final String queryDisp = queryStr.length() > QUERY_DISPLAY_LEN ? queryStr.substring(0, QUERY_DISPLAY_LEN - 1) + " ..." : queryStr;
425  try {
426  progress = ProgressHandle.createHandle(NbBundle.getMessage(this.getClass(), "KeywordSearchResultFactory.progress.saving", queryDisp), () -> BlackboardResultWriter.this.cancel(true));
427  hits.process(progress, null, this, false, saveResults);
428  } finally {
429  finalizeWorker();
430  }
431  return null;
432  }
433 
434  @Override
435  protected void done() {
436  try {
437  get();
438  } catch (InterruptedException | CancellationException ex) {
439  logger.log(Level.WARNING, "User cancelled writing of ad hoc search query results for '{0}' to the blackboard", query.getQueryString()); //NON-NLS
440  } catch (ExecutionException ex) {
441  logger.log(Level.SEVERE, "Error writing of ad hoc search query results for " + query.getQueryString() + " to the blackboard", ex); //NON-NLS
442  }
443  }
444 
445  private static synchronized void registerWriter(BlackboardResultWriter writer) {
446  WRITERS.add(writer);
447  }
448 
449  private static synchronized void deregisterWriter(BlackboardResultWriter writer) {
450  WRITERS.remove(writer);
451  }
452 
453  static synchronized void stopAllWriters() {
454  for (BlackboardResultWriter w : WRITERS) {
455  w.cancel(true);
456  WRITERS.remove(w);
457  }
458  }
459  }
460 }

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