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 javax.swing.SwingWorker;
29 import org.netbeans.api.progress.ProgressHandle;
30 import org.netbeans.api.progress.aggregate.ProgressContributor;
31 import org.openide.util.NbBundle;
49 private static final Logger logger = Logger.getLogger(QueryResults.class.getName());
54 private final KeywordSearchQuery keywordSearchQuery;
59 private final Map<Keyword, List<KeywordHit>> results =
new HashMap<>();
65 private final KeywordList keywordList;
67 QueryResults(KeywordSearchQuery query, KeywordList keywordList) {
68 this.keywordSearchQuery = query;
69 this.keywordList = keywordList;
72 void addResult(Keyword keyword, List<KeywordHit> hits) {
73 results.put(keyword, hits);
77 KeywordList getKeywordList() {
81 KeywordSearchQuery getQuery() {
82 return keywordSearchQuery;
85 List<KeywordHit> getResults(Keyword keyword) {
86 return results.get(keyword);
89 Set<Keyword> getKeywords() {
90 return results.keySet();
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());
112 int unitProgress = 0;
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());
121 if (progress != null) {
122 progress.progress(keyword.toString(), unitProgress);
124 if (subProgress != null) {
125 String hitDisplayStr = keyword.getQuery();
126 if (hitDisplayStr.length() > 50) {
127 hitDisplayStr = hitDisplayStr.substring(0, 49) +
"...";
129 subProgress.progress(keywordList.getName() +
": " + hitDisplayStr, unitProgress);
132 for (KeywordHit hit : getOneHitPerObject(keyword)) {
133 String termString = keyword.getQuery();
134 final String snippetQuery = KeywordSearchUtil.escapeLuceneQuery(termString);
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);
142 }
catch (Exception e) {
143 logger.log(Level.WARNING,
"Error querying snippet: " + snippetQuery, e);
146 if (snippet != null) {
147 KeywordCachedArtifact writeResult = keywordSearchQuery.writeSingleFileHitsToBlackBoard(termString, hit, snippet, keywordList.getName());
148 if (writeResult != null) {
149 newArtifacts.add(writeResult.getArtifact());
151 writeSingleFileInboxMessage(writeResult, hit.getContent());
154 logger.log(Level.WARNING,
"BB artifact for keyword hit not written, file: {0}, hit: {1}",
new Object[]{hit.getContent(), keyword.toString()});
162 if (!newArtifacts.isEmpty()) {
163 IngestServices.getInstance().fireModuleDataEvent(
new ModuleDataEvent(KeywordSearchModuleFactory.getModuleName(), BlackboardArtifact.ARTIFACT_TYPE.TSK_KEYWORD_HIT, newArtifacts));
174 private Collection<KeywordHit> getOneHitPerObject(Keyword keyword) {
176 HashMap<Long, KeywordHit> hits =
new HashMap<Long, KeywordHit>();
179 for(KeywordHit hit: getResults(keyword)) {
180 if(!hits.containsKey(hit.getSolrObjectId())) {
181 hits.put(hit.getSolrObjectId(), hit);
183 if(hit.getChunkId() < hits.get(hit.getSolrObjectId()).getChunkId()) {
184 hits.put(hit.getSolrObjectId(), hit);
188 return hits.values();
197 private void writeSingleFileInboxMessage(KeywordCachedArtifact written, Content hitContent) {
198 StringBuilder subjectSb =
new StringBuilder();
199 StringBuilder detailsSb =
new StringBuilder();
201 if (!keywordSearchQuery.isLiteral()) {
202 subjectSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.regExpHitLbl"));
204 subjectSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.kwHitLbl"));
206 String uniqueKey = null;
207 BlackboardAttribute attr = written.getAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD.getTypeID());
209 final String keyword = attr.getValueString();
210 subjectSb.append(keyword);
211 uniqueKey = keyword.toLowerCase();
215 detailsSb.append(
"<table border='0' cellpadding='4' width='280'>");
217 detailsSb.append(
"<tr>");
218 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.kwHitThLbl"));
219 detailsSb.append(
"<td>").append(EscapeUtil.escapeHtml(attr.getValueString())).append(
"</td>");
220 detailsSb.append(
"</tr>");
223 attr = written.getAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_PREVIEW.getTypeID());
225 detailsSb.append(
"<tr>");
226 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.previewThLbl"));
227 detailsSb.append(
"<td>").append(EscapeUtil.escapeHtml(attr.getValueString())).append(
"</td>");
228 detailsSb.append(
"</tr>");
232 detailsSb.append(
"<tr>");
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>");
239 detailsSb.append(
"<td>").append(hitContent.getName()).append(
"</td>");
241 detailsSb.append(
"</tr>");
244 attr = written.getAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID());
245 detailsSb.append(
"<tr>");
246 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.listThLbl"));
247 detailsSb.append(
"<td>").append(attr.getValueString()).append(
"</td>");
248 detailsSb.append(
"</tr>");
251 if (!keywordSearchQuery.isLiteral()) {
252 attr = written.getAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_REGEXP.getTypeID());
254 detailsSb.append(
"<tr>");
255 detailsSb.append(NbBundle.getMessage(
this.getClass(),
"KeywordSearchIngestModule.regExThLbl"));
256 detailsSb.append(
"<td>").append(attr.getValueString()).append(
"</td>");
257 detailsSb.append(
"</tr>");
260 detailsSb.append(
"</table>");
262 IngestServices.getInstance().postMessage(IngestMessage.createDataMessage(KeywordSearchModuleFactory.getModuleName(), subjectSb.toString(), detailsSb.toString(), uniqueKey, written.getArtifact()));