Autopsy  4.14.0
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.filequery;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.stream.Collectors;
29 
33 class SearchResults {
34 
35  private final FileGroup.GroupSortingAlgorithm groupSortingType;
36  private final FileSearch.AttributeType attrType;
37  private final FileSorter fileSorter;
38 
39  private final Map<FileSearch.GroupKey, FileGroup> groupMap = new HashMap<>();
40  private List<FileGroup> groupList = new ArrayList<>();
41 
42  private static final long MAX_OUTPUT_FILES = 2000; // For debug UI - maximum number of lines to print
43 
53  SearchResults(FileGroup.GroupSortingAlgorithm groupSortingType, FileSearch.AttributeType attrType,
54  FileSorter.SortingMethod fileSortingMethod) {
55  this.groupSortingType = groupSortingType;
56  this.attrType = attrType;
57  this.fileSorter = new FileSorter(fileSortingMethod);
58  }
59 
64  SearchResults() {
65  this.groupSortingType = FileGroup.GroupSortingAlgorithm.BY_GROUP_NAME;
66  this.attrType = new FileSearch.FileSizeAttribute();
67  this.fileSorter = new FileSorter(FileSorter.SortingMethod.BY_FILE_NAME);
68  }
69 
75  void add(List<ResultFile> files) {
76  for (ResultFile file : files) {
77  // Add the file to the appropriate group, creating it if necessary
78  FileSearch.GroupKey groupKey = attrType.getGroupKey(file);
79 
80  if (!groupMap.containsKey(groupKey)) {
81  groupMap.put(groupKey, new FileGroup(groupSortingType, groupKey));
82  }
83  groupMap.get(groupKey).addFile(file);
84  }
85  }
86 
91  void sortGroupsAndFiles() {
92 
93  // First sortGroupsAndFiles the files
94  for (FileGroup group : groupMap.values()) {
95  group.sortFiles(fileSorter);
96  }
97 
98  // Now put the groups in a list and sortGroupsAndFiles them
99  groupList = new ArrayList<>(groupMap.values());
100  Collections.sort(groupList);
101  }
102 
103  @Override
104  public String toString() {
105  String result = "";
106  if (groupList == null) {
107  return result;
108  }
109 
110  long count = 0;
111  for (FileGroup group : groupList) {
112  result += group.getDisplayName() + "\n";
113 
114  for (ResultFile file : group.getFiles()) {
115  result += " " + file.toString() + "\n";
116  count++;
117  if (count > MAX_OUTPUT_FILES) {
118  result += "(truncated)";
119  return result;
120  }
121  }
122  }
123  return result;
124  }
125 
131  List<String> getGroupNamesWithCounts() {
132  return groupList.stream().map(p -> p.getDisplayName() + " (" + p.getFiles().size() + ")").collect(Collectors.toList());
133  }
134 
142  List<ResultFile> getResultFilesInGroup(String groupName) {
143  if (groupName != null) {
144  final String modifiedGroupName = groupName.replaceAll(" \\([0-9]+\\)$", "");
145 
146  java.util.Optional<FileGroup> fileGroup = groupList.stream().filter(p -> p.getDisplayName().equals(modifiedGroupName)).findFirst();
147  if (fileGroup.isPresent()) {
148  return fileGroup.get().getFiles();
149  }
150  }
151  return new ArrayList<>();
152  }
153 
159  Map<GroupKey, List<ResultFile>> toLinkedHashMap() throws FileSearchException {
160  Map<GroupKey, List<ResultFile>> map = new LinkedHashMap<>();
161 
162  // Sort the groups and files
163  sortGroupsAndFiles();
164 
165  // groupList is sorted and a LinkedHashMap will preserve that order.
166  for (FileGroup group : groupList) {
167  map.put(group.getGroupKey(), group.getFiles());
168  }
169 
170  return map;
171  }
172 }

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.