19 package org.sleuthkit.autopsy.keywordsearch;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.stream.Collectors;
29 import javax.swing.SwingWorker;
30 import org.apache.commons.lang.StringUtils;
31 import org.netbeans.api.progress.ProgressHandle;
32 import org.netbeans.api.progress.aggregate.ProgressContributor;
33 import org.openide.util.NbBundle;
56 private static final Logger logger = Logger.getLogger(QueryResults.class.getName());
57 private static final String MODULE_NAME = KeywordSearchModuleFactory.getModuleName();
58 private final KeywordSearchQuery query;
59 private final Map<Keyword, List<KeywordHit>> results =
new HashMap<>();
71 QueryResults(KeywordSearchQuery query) {
81 KeywordSearchQuery getQuery() {
93 void addResult(Keyword keyword, List<KeywordHit> hits) {
94 results.put(keyword, hits);
104 List<KeywordHit> getResults(Keyword keyword) {
105 return results.get(keyword);
114 Set<Keyword> getKeywords() {
115 return results.keySet();
147 void process(ProgressHandle progress, ProgressContributor subProgress, SwingWorker<?, ?> worker,
boolean notifyInbox,
boolean saveResults) {
152 if (null != progress) {
153 progress.start(getKeywords().size());
159 int keywordsProcessed = 0;
160 final Collection<BlackboardArtifact> hitArtifacts =
new ArrayList<>();
161 for (
final Keyword keyword : getKeywords()) {
165 if (worker.isCancelled()) {
166 logger.log(Level.INFO,
"Processing cancelled, exiting before processing search term {0}", keyword.getSearchTerm());
174 if (progress != null) {
175 progress.progress(keyword.toString(), keywordsProcessed);
177 if (subProgress != null) {
178 String hitDisplayStr = keyword.getSearchTerm();
179 if (hitDisplayStr.length() > 50) {
180 hitDisplayStr = hitDisplayStr.substring(0, 49) +
"...";
182 subProgress.progress(query.getKeywordList().getName() +
": " + hitDisplayStr, keywordsProcessed);
190 for (KeywordHit hit : getOneHitPerTextSourceObject(keyword)) {
197 String snippet = hit.getSnippet();
198 if (StringUtils.isBlank(snippet)) {
199 final String snippetQuery = KeywordSearchUtil.escapeLuceneQuery(keyword.getSearchTerm());
201 snippet = LuceneQuery.querySnippet(snippetQuery, hit.getSolrObjectId(), hit.getChunkId(), !query.isLiteral(),
true);
202 }
catch (NoOpenCoreException e) {
203 logger.log(Level.SEVERE,
"Solr core closed while executing snippet query " + snippetQuery, e);
205 }
catch (Exception e) {
206 logger.log(Level.SEVERE,
"Error executing snippet query " + snippetQuery, e);
215 Content content = null;
217 SleuthkitCase tskCase = Case.getCurrentCaseThrows().getSleuthkitCase();
218 content = tskCase.getContentById(hit.getContentID());
219 }
catch (TskCoreException | NoCurrentCaseException tskCoreException) {
220 logger.log(Level.SEVERE,
"Failed to get text source object for ", tskCoreException);
227 BlackboardArtifact artifact = query.postKeywordHitToBlackboard(content, keyword, hit, snippet, query.getKeywordList().getName());
232 if (null != artifact) {
233 hitArtifacts.add(artifact);
236 writeSingleFileInboxMessage(artifact, content);
237 }
catch (TskCoreException ex) {
238 logger.log(Level.SEVERE,
"Error sending message to ingest messages inbox", ex);
254 if (!hitArtifacts.isEmpty()) {
255 hitArtifacts.stream()
257 .collect(Collectors.groupingBy(BlackboardArtifact::getArtifactTypeID))
259 .forEach((typeID, artifacts)
260 -> IngestServices.getInstance().fireModuleDataEvent(
new ModuleDataEvent(MODULE_NAME, BlackboardArtifact.ARTIFACT_TYPE.fromID(typeID), artifacts)));
274 private Collection<KeywordHit> getOneHitPerTextSourceObject(Keyword keyword) {
280 HashMap< Long, KeywordHit> hits =
new HashMap<>();
281 getResults(keyword).forEach((hit) -> {
282 if (!hits.containsKey(hit.getSolrObjectId())) {
283 hits.put(hit.getSolrObjectId(), hit);
284 }
else if (hit.getChunkId() < hits.get(hit.getSolrObjectId()).getChunkId()) {
285 hits.put(hit.getSolrObjectId(), hit);
288 return hits.values();
301 private void writeSingleFileInboxMessage(BlackboardArtifact artifact, Content hitContent)
throws TskCoreException {
302 StringBuilder subjectSb =
new StringBuilder(1024);
303 if (!query.isLiteral()) {
304 subjectSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.regExpHitLbl"));
306 subjectSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.kwHitLbl"));
309 StringBuilder detailsSb =
new StringBuilder(1024);
310 String uniqueKey = null;
311 BlackboardAttribute attr = artifact.getAttribute(
new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD));
313 final String keyword = attr.getValueString();
314 subjectSb.append(keyword);
315 uniqueKey = keyword.toLowerCase();
316 detailsSb.append(
"<table border='0' cellpadding='4' width='280'>");
317 detailsSb.append(
"<tr>");
318 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.kwHitThLbl"));
319 detailsSb.append(
"<td>").append(EscapeUtil.escapeHtml(keyword)).append(
"</td>");
320 detailsSb.append(
"</tr>");
324 attr = artifact.getAttribute(
new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_PREVIEW));
326 detailsSb.append(
"<tr>");
327 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.previewThLbl"));
328 detailsSb.append(
"<td>").append(EscapeUtil.escapeHtml(attr.getValueString())).append(
"</td>");
329 detailsSb.append(
"</tr>");
333 detailsSb.append(
"<tr>");
334 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.fileThLbl"));
335 if (hitContent instanceof AbstractFile) {
336 AbstractFile hitFile = (AbstractFile) hitContent;
337 detailsSb.append(
"<td>").append(hitFile.getParentPath()).append(hitFile.getName()).append(
"</td>");
339 detailsSb.append(
"<td>").append(hitContent.getName()).append(
"</td>");
341 detailsSb.append(
"</tr>");
344 attr = artifact.getAttribute(
new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME));
346 detailsSb.append(
"<tr>");
347 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.listThLbl"));
348 detailsSb.append(
"<td>").append(attr.getValueString()).append(
"</td>");
349 detailsSb.append(
"</tr>");
353 if (!query.isLiteral()) {
354 attr = artifact.getAttribute(
new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_REGEXP));
356 detailsSb.append(
"<tr>");
357 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.regExThLbl"));
358 detailsSb.append(
"<td>").append(attr.getValueString()).append(
"</td>");
359 detailsSb.append(
"</tr>");
362 detailsSb.append(
"</table>");
364 IngestServices.getInstance().postMessage(IngestMessage.createDataMessage(MODULE_NAME, subjectSb.toString(), detailsSb.toString(), uniqueKey, artifact));