Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
SearchResults.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.discovery.search;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.stream.Collectors;
30 import org.apache.commons.lang3.StringUtils;
32 
36 class SearchResults {
37 
38  private final Group.GroupSortingAlgorithm groupSortingType;
39  private final DiscoveryAttributes.AttributeType attrType;
40  private final ResultsSorter fileSorter;
41 
42  private final Map<GroupKey, Group> groupMap = new HashMap<>();
43  private List<Group> groupList = new ArrayList<>();
44 
45  private static final long MAX_OUTPUT_FILES = 2000; // For debug UI - maximum number of lines to print
46 
56  SearchResults(Group.GroupSortingAlgorithm groupSortingType, DiscoveryAttributes.AttributeType attrType,
57  ResultsSorter.SortingMethod fileSortingMethod) {
58  this.groupSortingType = groupSortingType;
59  this.attrType = attrType;
60  this.fileSorter = new ResultsSorter(fileSortingMethod);
61  }
62 
67  SearchResults() {
68  this.groupSortingType = Group.GroupSortingAlgorithm.BY_GROUP_NAME;
69  this.attrType = new DiscoveryAttributes.FileSizeAttribute();
70  this.fileSorter = new ResultsSorter(ResultsSorter.SortingMethod.BY_FILE_NAME);
71  }
72 
78  void add(List<Result> results) {
79  for (Result result : results) {
80  // Add the file to the appropriate group, creating it if necessary
81  if (result.getType() == SearchData.Type.DOMAIN && attrType instanceof DiscoveryAttributes.DomainCategoryAttribute) {
88  for (String category : ((ResultDomain) result).getWebCategories()) {
89  if (!StringUtils.isBlank(category)) {
90  ResultDomain currentResult = (ResultDomain) result;
91  Set<String> newCategorySet = new HashSet<>();
92  newCategorySet.add(category);
93  ResultDomain copyOfResult = new ResultDomain(currentResult);
94  copyOfResult.addWebCategories(newCategorySet);
95  GroupKey groupKey = attrType.getGroupKey(copyOfResult);
96  //purposefully adding original instead of copy so it will display all categories when looking at domain
97  addResultToGroupMap(groupKey, result);
98  }
99  }
100  } else {
101  GroupKey groupKey = attrType.getGroupKey(result);
102  addResultToGroupMap(groupKey, result);
103  }
104  }
105  }
106 
114  private void addResultToGroupMap(GroupKey groupKey, Result result) {
115  if (!groupMap.containsKey(groupKey)) {
116  groupMap.put(groupKey, new Group(groupSortingType, groupKey));
117  }
118  groupMap.get(groupKey).addResult(result);
119  }
120 
125  void sortGroupsAndFiles() {
126 
127  // First sortGroupsAndFiles the files
128  for (Group group : groupMap.values()) {
129  group.sortResults(fileSorter);
130  }
131 
132  // Now put the groups in a list and sortGroupsAndFiles them
133  groupList = new ArrayList<>(groupMap.values());
134  Collections.sort(groupList);
135  }
136 
137  @Override
138  public String toString() {
139  String resultString = "";
140  if (groupList == null) {
141  return resultString;
142  }
143 
144  long count = 0;
145  for (Group group : groupList) {
146  resultString += group.getDisplayName() + "\n";
147 
148  for (Result result : group.getResults()) {
149  resultString += " " + result.toString() + "\n";
150  count++;
151  if (count > MAX_OUTPUT_FILES) {
152  resultString += "(truncated)";
153  return resultString;
154  }
155  }
156  }
157  return resultString;
158  }
159 
165  List<String> getGroupNamesWithCounts() {
166  return groupList.stream().map(p -> p.getDisplayName() + " (" + p.getResults().size() + ")").collect(Collectors.toList());
167  }
168 
176  List<Result> getResultFilesInGroup(String groupName) {
177  if (groupName != null) {
178  final String modifiedGroupName = groupName.replaceAll(" \\([0-9]+\\)$", "");
179 
180  java.util.Optional<Group> group = groupList.stream().filter(p -> p.getDisplayName().equals(modifiedGroupName)).findFirst();
181  if (group.isPresent()) {
182  return group.get().getResults();
183  }
184  }
185  return new ArrayList<>();
186  }
187 
193  Map<GroupKey, List<Result>> toLinkedHashMap() throws DiscoveryException {
194  Map<GroupKey, List<Result>> map = new LinkedHashMap<>();
195 
196  // Sort the groups and files
197  sortGroupsAndFiles();
198 
199  // groupList is sorted and a LinkedHashMap will preserve that order.
200  for (Group group : groupList) {
201  map.put(group.getGroupKey(), group.getResults());
202  }
203  return map;
204  }
205 }

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