Autopsy  4.10.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SearchQuery.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019 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.multicase;
20 
21 import javax.annotation.concurrent.Immutable;
23 
27 @Immutable
28 final class SearchQuery {
29 
30  private static final String SEARCH_TERM_CHARS_TO_ESCAPE = "/+-&|!(){}[]^\"~*?:\\";
31  private static final String SOLR_DOC_CONTENT_STR_FIELD = Server.Schema.CONTENT_STR.toString(); //NON-NLS
32  private final String searchTerm;
33 
40  SearchQuery(QueryType queryType, String searchTerm) {
41  switch (queryType) {
42  case EXACT_MATCH:
43  this.searchTerm = prepareExactMatchSearchTerm(searchTerm);
44  break;
45  case SUBSTRING:
46  this.searchTerm = prepareSubstringSearchTerm(searchTerm);
47  break;
48  case REGEX:
49  this.searchTerm = prepareRegexSearchTerm(searchTerm);
50  break;
51  default:
52  this.searchTerm = searchTerm;
53  break;
54  }
55  }
56 
62  String getSearchTerm() {
63  return searchTerm;
64  }
65 
74  private static String prepareExactMatchSearchTerm(String searchTerm) {
75  String escapedSearchTerm = escapeSearchTerm(searchTerm);
76  if (!searchTerm.startsWith("\"")) {
77  escapedSearchTerm = "\"" + escapedSearchTerm;
78  }
79  if (!searchTerm.endsWith("\"")) {
80  escapedSearchTerm += "\"";
81  }
82  return escapedSearchTerm;
83  }
84 
93  private static String prepareRegexSearchTerm(String searchTerm) {
94  /*
95  * Add slash delimiters and, if necessary, wildcards (.*) at the
96  * beginning and end of the search term. The wildcards are added because
97  * Lucerne automatically adds a '^' prefix and '$' suffix to the search
98  * terms for regex searches. Without the '.*' wildcards, the search term
99  * will have to match the entire content_str field, which is not
100  * generally the intent of the user.
101  */
102  String regexSearchTerm = SOLR_DOC_CONTENT_STR_FIELD
103  + ":/"
104  + (searchTerm.startsWith(".*") ? "" : ".*")
105  + searchTerm.toLowerCase()
106  + (searchTerm.endsWith(".*") ? "" : ".*")
107  + "/";
108  return regexSearchTerm;
109  }
110 
119  private static String prepareSubstringSearchTerm(String searchTerm) {
120  String escapedSearchTerm = escapeSearchTerm(searchTerm);
121  return prepareRegexSearchTerm(escapedSearchTerm);
122  }
123 
131  public static String escapeSearchTerm(String searchTerm) {
132  String rawSearchTerm = searchTerm.trim();
133  if (0 == rawSearchTerm.length()) {
134  return rawSearchTerm;
135  }
136  StringBuilder escapedSearchTerm = new StringBuilder(rawSearchTerm.length());
137  for (int i = 0; i < rawSearchTerm.length(); ++i) {
138  final char nextChar = rawSearchTerm.charAt(i);
139  if (SEARCH_TERM_CHARS_TO_ESCAPE.contains(Character.toString(nextChar))) {
140  escapedSearchTerm.append("\\");
141  }
142  escapedSearchTerm.append(nextChar);
143  }
144  return escapedSearchTerm.toString();
145  }
146 
150  enum QueryType {
151  EXACT_MATCH,
152  SUBSTRING,
153  REGEX;
154  }
155 
156 }

Copyright © 2012-2018 Basis Technology. Generated on: Fri Mar 22 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.