Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
AdHocSearchDelegator.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 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.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.logging.Level;
28 import org.openide.nodes.AbstractNode;
29 import org.openide.nodes.Children;
30 import org.openide.nodes.Node;
31 import org.openide.util.NbBundle;
35 
41 class AdHocSearchDelegator {
42 
43  private final List<KeywordList> keywordLists;
44  private List<KeywordSearchQuery> queryDelegates;
45  private final Set<Long> dataSourceIds;
46  private static int resultWindowCount = 0; //keep track of unique window ids to display
47  private static final Logger logger = Logger.getLogger(AdHocSearchDelegator.class.getName());
48 
49  public AdHocSearchDelegator(List<KeywordList> keywordLists, Set<Long> dataSourceIds) {
50  this.keywordLists = keywordLists;
51  this.dataSourceIds = dataSourceIds;
52  init();
53  }
54 
55  private void init() {
56  // make a query for each keyword
57  queryDelegates = new ArrayList<>();
58 
59  for (KeywordList keywordList : keywordLists) {
60  for (Keyword keyword : keywordList.getKeywords()) {
61  KeywordSearchQuery query = KeywordSearchUtil.getQueryForKeyword(keyword, keywordList);
62 
63  //Limit search to a set of data sources
64  if (dataSourceIds != null && !dataSourceIds.isEmpty()) {
65  final KeywordQueryFilter dataSourceFilter = new KeywordQueryFilter(KeywordQueryFilter.FilterType.DATA_SOURCE, dataSourceIds);
66  query.addFilter(dataSourceFilter);
67  }
68 
69  queryDelegates.add(query);
70  }
71  }
72  }
73 
80  public void execute(boolean saveResults) {
81  Collection<AdHocQueryRequest> queryRequests = new ArrayList<>();
82  int queryID = 0;
83  StringBuilder queryConcat = new StringBuilder(); // concatenation of all query strings
84  for (KeywordSearchQuery q : queryDelegates) {
85  Map<String, Object> kvs = new LinkedHashMap<>();
86  final String queryStr = q.getQueryString();
87  queryConcat.append(queryStr).append(" ");
88  queryRequests.add(new AdHocQueryRequest(kvs, ++queryID, q));
89  }
90 
91  String queryConcatStr = queryConcat.toString();
92  final int queryConcatStrLen = queryConcatStr.length();
93  final String queryStrShort = queryConcatStrLen > 15 ? queryConcatStr.substring(0, 14) + "..." : queryConcatStr;
94  final String windowTitle = NbBundle.getMessage(this.getClass(), "KeywordSearchQueryManager.execute.exeWinTitle", ++resultWindowCount, queryStrShort);
95  DataResultTopComponent searchResultWin = DataResultTopComponent.createInstance(windowTitle);
96 
97  Node rootNode;
98  if (queryRequests.size() > 0) {
99  Children childNodes =
100  Children.create(new AdHocSearchChildFactory(queryRequests, saveResults), true);
101 
102  rootNode = new AbstractNode(childNodes);
103  } else {
104  rootNode = Node.EMPTY;
105  }
106 
107  final String pathText = NbBundle.getMessage(this.getClass(), "KeywordSearchQueryManager.pathText.text");
108 
109  DataResultTopComponent.initInstance(pathText, new TableFilterNode(rootNode, true, KeywordSearch.class.getName()),
110  queryRequests.size(), searchResultWin);
111 
112  searchResultWin.requestActive();
113  }
114 
120  public boolean validate() {
121  boolean allValid = true;
122  for (KeywordSearchQuery tcq : queryDelegates) {
123  if (!tcq.validate()) {
124  logger.log(Level.WARNING, "Query has invalid syntax: {0}", tcq.getQueryString()); //NON-NLS
125  allValid = false;
126  break;
127  }
128  }
129  return allValid;
130  }
131 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.