19 package org.sleuthkit.autopsy.keywordsearch;
21 import java.awt.EventQueue;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.List;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.locks.ReentrantReadWriteLock;
30 import java.util.logging.Level;
31 import javax.swing.SwingWorker;
32 import org.netbeans.api.progress.ProgressHandle;
33 import org.openide.nodes.ChildFactory;
34 import org.openide.nodes.Children;
35 import org.openide.nodes.Node;
36 import org.openide.util.Cancellable;
37 import org.openide.util.NbBundle;
38 import org.openide.util.lookup.Lookups;
58 class KeywordSearchResultFactory
extends ChildFactory<KeyValueQueryContent> {
66 public String toString() {
67 return BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD.getDisplayName();
72 public String toString() {
73 return BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_REGEXP.getDisplayName();
78 public String toString() {
79 return BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_PREVIEW.getDisplayName();
83 private Collection<QueryRequest> queryRequests;
88 this.queryRequests = queryRequests;
99 public static void initCommonProperties(Map<String, Object> toSet) {
100 CommonPropertyTypes[] commonTypes = CommonPropertyTypes.values();
101 final int COMMON_PROPS_LEN = commonTypes.length;
102 for (
int i = 0; i < COMMON_PROPS_LEN; ++i) {
103 toSet.put(commonTypes[i].toString(),
"");
106 AbstractAbstractFileNode.AbstractFilePropertyType[] fsTypes = AbstractAbstractFileNode.AbstractFilePropertyType.values();
107 final int FS_PROPS_LEN = fsTypes.length;
108 for (
int i = 0; i < FS_PROPS_LEN; ++i) {
109 toSet.put(fsTypes[i].toString(),
"");
113 public static void setCommonProperty(Map<String, Object> toSet, CommonPropertyTypes type, String value) {
114 final String typeStr = type.toString();
115 toSet.put(typeStr, value);
118 public static void setCommonProperty(Map<String, Object> toSet, CommonPropertyTypes type, Boolean value) {
119 final String typeStr = type.toString();
120 toSet.put(typeStr, value);
124 protected boolean createKeys(List<KeyValueQueryContent> toPopulate) {
126 for (QueryRequest queryRequest : queryRequests) {
127 Map<String, Object> map = queryRequest.getProperties();
128 initCommonProperties(map);
129 final String query = queryRequest.getQueryString();
130 setCommonProperty(map, CommonPropertyTypes.KEYWORD, query);
131 setCommonProperty(map, CommonPropertyTypes.REGEX, !queryRequest.getQuery().isLiteral());
132 createFlatKeys(queryRequest, toPopulate);
145 private boolean createFlatKeys(QueryRequest queryRequest, List<KeyValueQueryContent> toPopulate) {
149 final KeywordSearchQuery keywordSearchQuery = queryRequest.getQuery();
150 if (!keywordSearchQuery.validate()) {
158 QueryResults queryResults;
160 queryResults = keywordSearchQuery.performQuery();
161 }
catch (NoOpenCoreException ex) {
162 logger.log(Level.SEVERE,
"Could not perform the query " + keywordSearchQuery.getQueryString(), ex);
167 List<KeyValueQueryContent> tempList =
new ArrayList<>();
168 for (KeywordHit hit : getOneHitPerObject(queryResults)) {
172 Map<String, Object> properties =
new LinkedHashMap<>();
173 Content content = hit.getContent();
174 if (content instanceof AbstractFile) {
175 AbstractFsContentNode.fillPropertyMap(properties, (AbstractFile) content);
177 properties.put(AbstractAbstractFileNode.AbstractFilePropertyType.LOCATION.toString(), content.getName());
183 if (hit.hasSnippet()) {
184 setCommonProperty(properties, CommonPropertyTypes.CONTEXT, hit.getSnippet());
191 final String highlightQueryEscaped = getHighlightQuery(keywordSearchQuery, keywordSearchQuery.isLiteral(), queryResults, content);
193 String name = content.getName();
194 if (hit.isArtifactHit()) {
195 name = hit.getArtifact().getDisplayName() +
" Artifact";
197 tempList.add(
new KeyValueQueryContent(name, properties, ++
id, hit.getSolrObjectId(), content, highlightQueryEscaped, keywordSearchQuery, queryResults));
202 toPopulate.addAll(tempList);
208 new BlackboardResultWriter(queryResults, queryRequest.getQuery().getKeywordList().getName()).execute();
222 Collection<KeywordHit> getOneHitPerObject(QueryResults queryResults) {
223 HashMap<Long, KeywordHit> hits =
new HashMap<Long, KeywordHit>();
224 for (Keyword keyWord : queryResults.getKeywords()) {
225 for (KeywordHit hit : queryResults.getResults(keyWord)) {
227 if (!hits.containsKey(hit.getSolrObjectId())) {
228 hits.put(hit.getSolrObjectId(), hit);
230 if (hit.getChunkId() < hits.get(hit.getSolrObjectId()).getChunkId()) {
231 hits.put(hit.getSolrObjectId(), hit);
236 return hits.values();
249 private String getHighlightQuery(KeywordSearchQuery query,
boolean literal_query, QueryResults queryResults, Content content) {
252 return constructEscapedSolrQuery(query.getQueryString(), literal_query);
256 if (queryResults.getKeywords().size() == 1) {
258 Keyword keyword = queryResults.getKeywords().iterator().next();
259 return constructEscapedSolrQuery(keyword.getSearchTerm(), literal_query);
262 List<Keyword> hitTerms =
new ArrayList<>();
263 for (Keyword keyword : queryResults.getKeywords()) {
264 for (KeywordHit hit : queryResults.getResults(keyword)) {
265 if (hit.getContent().equals(content)) {
266 hitTerms.add(keyword);
272 StringBuilder highlightQuery =
new StringBuilder();
273 final int lastTerm = hitTerms.size() - 1;
275 for (Keyword term : hitTerms) {
277 highlightQuery.append(constructEscapedSolrQuery(term.getSearchTerm(), literal_query));
278 if (lastTerm != curTerm) {
279 highlightQuery.append(
" ");
284 return highlightQuery.toString();
296 private String constructEscapedSolrQuery(String query,
boolean literal_query) {
297 StringBuilder highlightQuery =
new StringBuilder();
298 String highLightField;
300 highLightField = LuceneQuery.HIGHLIGHT_FIELD_LITERAL;
302 highLightField = LuceneQuery.HIGHLIGHT_FIELD_REGEX;
304 highlightQuery.append(highLightField).append(
":").append(
"\"").append(KeywordSearchUtil.escapeLuceneQuery(query)).append(
"\"");
305 return highlightQuery.toString();
309 protected Node createNodeForKey(KeyValueQueryContent key) {
310 final Content content = key.getContent();
311 final String queryStr = key.getQueryStr();;
312 QueryResults hits = key.getHits();
314 Node kvNode =
new KeyValueNode(key, Children.LEAF, Lookups.singleton(content));
318 HighlightedText highlights =
new HighlightedText(key.solrObjectId, queryStr, !key.getQuery().isLiteral(),
false, hits);
319 return new KeywordSearchFilterNode(highlights, kvNode);
326 class KeyValueQueryContent
extends KeyValue {
328 private long solrObjectId;
329 private Content content;
330 private String queryStr;
331 private QueryResults hits;
332 private KeywordSearchQuery query;
349 public KeyValueQueryContent(String name, Map<String, Object> map,
int id,
long solrObjectId, Content content, String queryStr, KeywordSearchQuery query, QueryResults hits) {
350 super(name, map,
id);
351 this.solrObjectId = solrObjectId;
352 this.content = content;
353 this.queryStr = queryStr;
358 Content getContent() {
362 String getQueryStr() {
366 QueryResults getHits() {
370 KeywordSearchQuery getQuery() {
379 static class BlackboardResultWriter
extends SwingWorker<Object, Void> {
381 private static List<BlackboardResultWriter> writers =
new ArrayList<>();
383 private static final ReentrantReadWriteLock rwLock =
new ReentrantReadWriteLock(
true);
385 private ProgressHandle progress;
386 private KeywordSearchQuery query;
387 private String listName;
388 private QueryResults hits;
389 private Collection<BlackboardArtifact> newArtifacts =
new ArrayList<>();
390 private static final int QUERY_DISPLAY_LEN = 40;
392 BlackboardResultWriter(QueryResults hits, String listName) {
394 this.query = hits.getQuery();
395 this.listName = listName;
398 protected void finalizeWorker() {
399 deregisterWriter(
this);
401 EventQueue.invokeLater(
new Runnable() {
410 protected Object doInBackground() throws Exception {
411 registerWriter(
this);
412 final String queryStr = query.getQueryString();
413 final String queryDisp = queryStr.length() > QUERY_DISPLAY_LEN ? queryStr.substring(0, QUERY_DISPLAY_LEN - 1) +
" ..." : queryStr;
418 progress = ProgressHandle.createHandle(
419 NbBundle.getMessage(
this.getClass(),
"KeywordSearchResultFactory.progress.saving", queryDisp),
new Cancellable() {
421 public boolean cancel() {
422 return BlackboardResultWriter.this.cancel(
true);
427 newArtifacts = hits.writeAllHitsToBlackBoard(progress, null,
this,
false);
436 protected void done() {
440 }
catch (InterruptedException | ExecutionException ex) {
441 logger.log(Level.SEVERE,
"Error querying ", ex);
445 private static synchronized void registerWriter(BlackboardResultWriter writer) {
449 private static synchronized void deregisterWriter(BlackboardResultWriter writer) {
450 writers.remove(writer);
453 static synchronized void stopAllWriters() {
454 for (BlackboardResultWriter w : writers) {
synchronized static Logger getLogger(String name)