Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
KeywordSearchQueryDelegator.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2014 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 
20 package org.sleuthkit.autopsy.keywordsearch;
21 
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.logging.Level;
28 import org.openide.util.NbBundle;
30 import org.openide.nodes.AbstractNode;
31 import org.openide.nodes.Children;
32 import org.openide.nodes.Node;
34 
40 class KeywordSearchQueryDelegator {
41  private List<KeywordList> keywordLists;
42  private List<KeywordSearchQuery> queryDelegates;
43  private static int resultWindowCount = 0; //keep track of unique window ids to display
44  private static Logger logger = Logger.getLogger(KeywordSearchQueryDelegator.class.getName());
45 
46 
47  public KeywordSearchQueryDelegator(List<KeywordList> keywordLists) {
48  this.keywordLists = keywordLists;
49  init();
50  }
51 
52  private void init() {
53  // make a query for each keyword
54  queryDelegates = new ArrayList<>();
55 
56  for (KeywordList keywordList : keywordLists) {
57  for (Keyword keyword : keywordList.getKeywords()) {
58  KeywordSearchQuery query;
59  if (keyword.isLiteral()) {
60  // literal, exact match
61  if (keyword.isWholeword()) {
62  query = new LuceneQuery(keywordList, keyword);
63  query.escape();
64  }
65  // literal, substring match
66  else {
67  query = new TermComponentQuery(keywordList, keyword);
68  query.escape();
69  query.setSubstringQuery();
70  }
71  }
72  // regexp
73  else {
74  query = new TermComponentQuery(keywordList, keyword);
75  }
76  queryDelegates.add(query);
77  }
78  }
79  }
80 
85  public void execute() {
86  Collection<QueryRequest> queryRequests = new ArrayList<>();
87  int queryID = 0;
88  StringBuilder queryConcat = new StringBuilder(); // concatenation of all query strings
89  for (KeywordSearchQuery q : queryDelegates) {
90  Map<String, Object> kvs = new LinkedHashMap<>();
91  final String queryStr = q.getQueryString();
92  queryConcat.append(queryStr).append(" ");
93  queryRequests.add(new QueryRequest(kvs, ++queryID, q));
94  }
95 
96  String queryConcatStr = queryConcat.toString();
97  final int queryConcatStrLen = queryConcatStr.length();
98  final String queryStrShort = queryConcatStrLen > 15 ? queryConcatStr.substring(0, 14) + "..." : queryConcatStr;
99  final String windowTitle = NbBundle.getMessage(this.getClass(), "KeywordSearchQueryManager.execute.exeWinTitle", ++resultWindowCount, queryStrShort);
100  DataResultTopComponent searchResultWin = DataResultTopComponent.createInstance(windowTitle);
101 
102  Node rootNode;
103  if (queryRequests.size() > 0) {
104  Children childNodes =
105  Children.create(new KeywordSearchResultFactory(queryRequests, searchResultWin), true);
106 
107  rootNode = new AbstractNode(childNodes);
108  } else {
109  rootNode = Node.EMPTY;
110  }
111 
112  final String pathText = NbBundle.getMessage(this.getClass(), "KeywordSearchQueryManager.pathText.text");
113 
114  DataResultTopComponent.initInstance(pathText, rootNode, queryRequests.size(), searchResultWin);
115 
116  searchResultWin.requestActive();
117  }
118 
123  public boolean validate() {
124  boolean allValid = true;
125  for (KeywordSearchQuery tcq : queryDelegates) {
126  if (!tcq.validate()) {
127  logger.log(Level.WARNING, "Query has invalid syntax: {0}", tcq.getQueryString()); //NON-NLS
128  allValid = false;
129  break;
130  }
131  }
132  return allValid;
133  }
134 }

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.