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();
146 void process(ProgressHandle progress, ProgressContributor subProgress, SwingWorker<?, ?> worker,
boolean notifyInbox) {
151 if (null != progress) {
152 progress.start(getKeywords().size());
158 int keywordsProcessed = 0;
159 final Collection<BlackboardArtifact> hitArtifacts =
new ArrayList<>();
160 for (
final Keyword keyword : getKeywords()) {
164 if (worker.isCancelled()) {
165 logger.log(Level.INFO,
"Processing cancelled, exiting before processing search term {0}", keyword.getSearchTerm());
173 if (progress != null) {
174 progress.progress(keyword.toString(), keywordsProcessed);
176 if (subProgress != null) {
177 String hitDisplayStr = keyword.getSearchTerm();
178 if (hitDisplayStr.length() > 50) {
179 hitDisplayStr = hitDisplayStr.substring(0, 49) +
"...";
181 subProgress.progress(query.getKeywordList().getName() +
": " + hitDisplayStr, keywordsProcessed);
189 for (KeywordHit hit : getOneHitPerTextSourceObject(keyword)) {
196 String snippet = hit.getSnippet();
197 if (StringUtils.isBlank(snippet)) {
198 final String snippetQuery = KeywordSearchUtil.escapeLuceneQuery(keyword.getSearchTerm());
200 snippet = LuceneQuery.querySnippet(snippetQuery, hit.getSolrObjectId(), hit.getChunkId(), !query.isLiteral(),
true);
201 }
catch (NoOpenCoreException e) {
202 logger.log(Level.SEVERE,
"Solr core closed while executing snippet query " + snippetQuery, e);
204 }
catch (Exception e) {
205 logger.log(Level.SEVERE,
"Error executing snippet query " + snippetQuery, e);
214 Content content = null;
216 SleuthkitCase tskCase = Case.getOpenCase().getSleuthkitCase();
217 content = tskCase.getContentById(hit.getContentID());
218 }
catch (TskCoreException | NoCurrentCaseException tskCoreException) {
219 logger.log(Level.SEVERE,
"Failed to get text source object for ", tskCoreException);
225 BlackboardArtifact artifact = query.postKeywordHitToBlackboard(content, keyword, hit, snippet, query.getKeywordList().getName());
230 if (null != artifact) {
231 hitArtifacts.add(artifact);
234 writeSingleFileInboxMessage(artifact, content);
235 }
catch (TskCoreException ex) {
236 logger.log(Level.SEVERE,
"Error sending message to ingest messages inbox", ex);
251 if (!hitArtifacts.isEmpty()) {
252 hitArtifacts.stream()
254 .collect(Collectors.groupingBy(BlackboardArtifact::getArtifactTypeID))
256 .forEach((typeID, artifacts)
257 -> IngestServices.getInstance().fireModuleDataEvent(
new ModuleDataEvent(MODULE_NAME, BlackboardArtifact.ARTIFACT_TYPE.fromID(typeID), artifacts)));
271 private Collection<KeywordHit> getOneHitPerTextSourceObject(Keyword keyword) {
277 HashMap< Long, KeywordHit> hits =
new HashMap<>();
278 getResults(keyword).forEach((hit) -> {
279 if (!hits.containsKey(hit.getSolrObjectId())) {
280 hits.put(hit.getSolrObjectId(), hit);
281 }
else if (hit.getChunkId() < hits.get(hit.getSolrObjectId()).getChunkId()) {
282 hits.put(hit.getSolrObjectId(), hit);
285 return hits.values();
298 private void writeSingleFileInboxMessage(BlackboardArtifact artifact, Content hitContent)
throws TskCoreException {
299 StringBuilder subjectSb =
new StringBuilder(1024);
300 if (!query.isLiteral()) {
301 subjectSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.regExpHitLbl"));
303 subjectSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.kwHitLbl"));
306 StringBuilder detailsSb =
new StringBuilder(1024);
307 String uniqueKey = null;
308 BlackboardAttribute attr = artifact.getAttribute(
new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD));
310 final String keyword = attr.getValueString();
311 subjectSb.append(keyword);
312 uniqueKey = keyword.toLowerCase();
313 detailsSb.append(
"<table border='0' cellpadding='4' width='280'>");
314 detailsSb.append(
"<tr>");
315 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.kwHitThLbl"));
316 detailsSb.append(
"<td>").append(EscapeUtil.escapeHtml(keyword)).append(
"</td>");
317 detailsSb.append(
"</tr>");
321 attr = artifact.getAttribute(
new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_PREVIEW));
323 detailsSb.append(
"<tr>");
324 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.previewThLbl"));
325 detailsSb.append(
"<td>").append(EscapeUtil.escapeHtml(attr.getValueString())).append(
"</td>");
326 detailsSb.append(
"</tr>");
330 detailsSb.append(
"<tr>");
331 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.fileThLbl"));
332 if (hitContent instanceof AbstractFile) {
333 AbstractFile hitFile = (AbstractFile) hitContent;
334 detailsSb.append(
"<td>").append(hitFile.getParentPath()).append(hitFile.getName()).append(
"</td>");
336 detailsSb.append(
"<td>").append(hitContent.getName()).append(
"</td>");
338 detailsSb.append(
"</tr>");
341 attr = artifact.getAttribute(
new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME));
343 detailsSb.append(
"<tr>");
344 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.listThLbl"));
345 detailsSb.append(
"<td>").append(attr.getValueString()).append(
"</td>");
346 detailsSb.append(
"</tr>");
350 if (!query.isLiteral()) {
351 attr = artifact.getAttribute(
new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_REGEXP));
353 detailsSb.append(
"<tr>");
354 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.regExThLbl"));
355 detailsSb.append(
"<td>").append(attr.getValueString()).append(
"</td>");
356 detailsSb.append(
"</tr>");
359 detailsSb.append(
"</table>");
361 IngestServices.getInstance().postMessage(IngestMessage.createDataMessage(MODULE_NAME, subjectSb.toString(), detailsSb.toString(), uniqueKey, artifact));