Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
QueryResults.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2014-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.keywordsearch;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.logging.Level;
28 import javax.swing.SwingWorker;
29 import org.netbeans.api.progress.ProgressHandle;
30 import org.netbeans.api.progress.aggregate.ProgressContributor;
31 import org.openide.util.NbBundle;
41 
47 class QueryResults {
48 
49  private static final Logger logger = Logger.getLogger(QueryResults.class.getName());
50 
54  private final KeywordSearchQuery keywordSearchQuery;
55 
59  private final Map<Keyword, List<KeywordHit>> results = new HashMap<>();
60 
64  // TODO: This is redundant. The keyword list is in the query.
65  private final KeywordList keywordList;
66 
67  QueryResults(KeywordSearchQuery query, KeywordList keywordList) {
68  this.keywordSearchQuery = query;
69  this.keywordList = keywordList;
70  }
71 
72  void addResult(Keyword keyword, List<KeywordHit> hits) {
73  results.put(keyword, hits);
74  }
75 
76  // TODO: This is redundant. The keyword list is in the query.
77  KeywordList getKeywordList() {
78  return keywordList;
79  }
80 
81  KeywordSearchQuery getQuery() {
82  return keywordSearchQuery;
83  }
84 
85  List<KeywordHit> getResults(Keyword keyword) {
86  return results.get(keyword);
87  }
88 
89  Set<Keyword> getKeywords() {
90  return results.keySet();
91  }
92 
107  Collection<BlackboardArtifact> writeAllHitsToBlackBoard(ProgressHandle progress, ProgressContributor subProgress, SwingWorker<Object, Void> worker, boolean notifyInbox) {
108  final Collection<BlackboardArtifact> newArtifacts = new ArrayList<>();
109  if (progress != null) {
110  progress.start(getKeywords().size());
111  }
112  int unitProgress = 0;
113 
114  for (final Keyword keyword : getKeywords()) {
115  if (worker.isCancelled()) {
116  logger.log(Level.INFO, "Cancel detected, bailing before new keyword processed: {0}", keyword.getQuery()); //NON-NLS
117  break;
118  }
119 
120  // Update progress object(s), if any
121  if (progress != null) {
122  progress.progress(keyword.toString(), unitProgress);
123  }
124  if (subProgress != null) {
125  String hitDisplayStr = keyword.getQuery();
126  if (hitDisplayStr.length() > 50) {
127  hitDisplayStr = hitDisplayStr.substring(0, 49) + "...";
128  }
129  subProgress.progress(keywordList.getName() + ": " + hitDisplayStr, unitProgress);
130  }
131 
132  for (KeywordHit hit : getOneHitPerObject(keyword)) {
133  String termString = keyword.getQuery();
134  final String snippetQuery = KeywordSearchUtil.escapeLuceneQuery(termString);
135  String snippet;
136  try {
137  snippet = LuceneQuery.querySnippet(snippetQuery, hit.getSolrObjectId(), hit.getChunkId(), !keywordSearchQuery.isLiteral(), true);
138  } catch (NoOpenCoreException e) {
139  logger.log(Level.WARNING, "Error querying snippet: " + snippetQuery, e); //NON-NLS
140  //no reason to continue
141  break;
142  } catch (Exception e) {
143  logger.log(Level.WARNING, "Error querying snippet: " + snippetQuery, e); //NON-NLS
144  continue;
145  }
146  if (snippet != null) {
147  KeywordCachedArtifact writeResult = keywordSearchQuery.writeSingleFileHitsToBlackBoard(termString, hit, snippet, keywordList.getName());
148  if (writeResult != null) {
149  newArtifacts.add(writeResult.getArtifact());
150  if (notifyInbox) {
151  writeSingleFileInboxMessage(writeResult, hit.getContent());
152  }
153  } else {
154  logger.log(Level.WARNING, "BB artifact for keyword hit not written, file: {0}, hit: {1}", new Object[]{hit.getContent(), keyword.toString()}); //NON-NLS
155  }
156  }
157  }
158  ++unitProgress;
159  }
160 
161  // Update artifact browser
162  if (!newArtifacts.isEmpty()) {
163  IngestServices.getInstance().fireModuleDataEvent(new ModuleDataEvent(KeywordSearchModuleFactory.getModuleName(), BlackboardArtifact.ARTIFACT_TYPE.TSK_KEYWORD_HIT, newArtifacts));
164  }
165 
166  return newArtifacts;
167  }
168 
174  private Collection<KeywordHit> getOneHitPerObject(Keyword keyword) {
175 
176  HashMap<Long, KeywordHit> hits = new HashMap<Long, KeywordHit>();
177 
178  // create a list of KeywordHits. KeywordHits with lowest chunkID is added the the list.
179  for(KeywordHit hit: getResults(keyword)) {
180  if(!hits.containsKey(hit.getSolrObjectId())) {
181  hits.put(hit.getSolrObjectId(), hit);
182  } else {
183  if(hit.getChunkId() < hits.get(hit.getSolrObjectId()).getChunkId()) {
184  hits.put(hit.getSolrObjectId(), hit);
185  }
186  }
187  }
188  return hits.values();
189  }
190 
197  private void writeSingleFileInboxMessage(KeywordCachedArtifact written, Content hitContent) {
198  StringBuilder subjectSb = new StringBuilder();
199  StringBuilder detailsSb = new StringBuilder();
200 
201  if (!keywordSearchQuery.isLiteral()) {
202  subjectSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.regExpHitLbl"));
203  } else {
204  subjectSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.kwHitLbl"));
205  }
206  String uniqueKey = null;
207  BlackboardAttribute attr = written.getAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD.getTypeID());
208  if (attr != null) {
209  final String keyword = attr.getValueString();
210  subjectSb.append(keyword);
211  uniqueKey = keyword.toLowerCase();
212  }
213 
214  //details
215  detailsSb.append("<table border='0' cellpadding='4' width='280'>"); //NON-NLS
216  //hit
217  detailsSb.append("<tr>"); //NON-NLS
218  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.kwHitThLbl"));
219  detailsSb.append("<td>").append(EscapeUtil.escapeHtml(attr.getValueString())).append("</td>"); //NON-NLS
220  detailsSb.append("</tr>"); //NON-NLS
221 
222  //preview
223  attr = written.getAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_PREVIEW.getTypeID());
224  if (attr != null) {
225  detailsSb.append("<tr>"); //NON-NLS
226  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.previewThLbl"));
227  detailsSb.append("<td>").append(EscapeUtil.escapeHtml(attr.getValueString())).append("</td>"); //NON-NLS
228  detailsSb.append("</tr>"); //NON-NLS
229  }
230 
231  //file
232  detailsSb.append("<tr>"); //NON-NLS
233  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.fileThLbl"));
234  if (hitContent instanceof AbstractFile) {
235  AbstractFile hitFile = (AbstractFile)hitContent;
236  detailsSb.append("<td>").append(hitFile.getParentPath()).append(hitFile.getName()).append("</td>"); //NON-NLS
237  }
238  else {
239  detailsSb.append("<td>").append(hitContent.getName()).append("</td>"); //NON-NLS
240  }
241  detailsSb.append("</tr>"); //NON-NLS
242 
243  //list
244  attr = written.getAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID());
245  detailsSb.append("<tr>"); //NON-NLS
246  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.listThLbl"));
247  detailsSb.append("<td>").append(attr.getValueString()).append("</td>"); //NON-NLS
248  detailsSb.append("</tr>"); //NON-NLS
249 
250  //regex
251  if (!keywordSearchQuery.isLiteral()) {
252  attr = written.getAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_REGEXP.getTypeID());
253  if (attr != null) {
254  detailsSb.append("<tr>"); //NON-NLS
255  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.regExThLbl"));
256  detailsSb.append("<td>").append(attr.getValueString()).append("</td>"); //NON-NLS
257  detailsSb.append("</tr>"); //NON-NLS
258  }
259  }
260  detailsSb.append("</table>"); //NON-NLS
261 
262  IngestServices.getInstance().postMessage(IngestMessage.createDataMessage(KeywordSearchModuleFactory.getModuleName(), subjectSb.toString(), detailsSb.toString(), uniqueKey, written.getArtifact()));
263  }
264 
265 }

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