19 package org.sleuthkit.autopsy.discovery.search;
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;
29 import java.util.stream.Collectors;
30 import org.apache.commons.lang3.StringUtils;
38 private final Group.GroupSortingAlgorithm groupSortingType;
39 private final DiscoveryAttributes.AttributeType attrType;
40 private final ResultsSorter fileSorter;
42 private final Map<GroupKey, Group> groupMap =
new HashMap<>();
43 private List<Group> groupList =
new ArrayList<>();
45 private static final long MAX_OUTPUT_FILES = 2000;
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);
68 this.groupSortingType = Group.GroupSortingAlgorithm.BY_GROUP_NAME;
69 this.attrType =
new DiscoveryAttributes.FileSizeAttribute();
70 this.fileSorter =
new ResultsSorter(ResultsSorter.SortingMethod.BY_FILE_NAME);
78 void add(List<Result> results) {
79 for (Result result : results) {
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);
97 addResultToGroupMap(groupKey, result);
101 GroupKey groupKey = attrType.getGroupKey(result);
102 addResultToGroupMap(groupKey, result);
114 private void addResultToGroupMap(GroupKey groupKey, Result result) {
115 if (!groupMap.containsKey(groupKey)) {
116 groupMap.put(groupKey,
new Group(groupSortingType, groupKey));
118 groupMap.get(groupKey).addResult(result);
125 void sortGroupsAndFiles() {
128 for (Group group : groupMap.values()) {
129 group.sortResults(fileSorter);
133 groupList =
new ArrayList<>(groupMap.values());
134 Collections.sort(groupList);
138 public String toString() {
139 String resultString =
"";
140 if (groupList == null) {
145 for (Group group : groupList) {
146 resultString += group.getDisplayName() +
"\n";
148 for (Result result : group.getResults()) {
149 resultString +=
" " + result.toString() +
"\n";
151 if (count > MAX_OUTPUT_FILES) {
152 resultString +=
"(truncated)";
165 List<String> getGroupNamesWithCounts() {
166 return groupList.stream().map(p -> p.getDisplayName() +
" (" + p.getResults().size() +
")").collect(Collectors.toList());
176 List<Result> getResultFilesInGroup(String groupName) {
177 if (groupName != null) {
178 final String modifiedGroupName = groupName.replaceAll(
" \\([0-9]+\\)$",
"");
180 java.util.Optional<Group> group = groupList.stream().filter(p -> p.getDisplayName().equals(modifiedGroupName)).findFirst();
181 if (group.isPresent()) {
182 return group.get().getResults();
185 return new ArrayList<>();
193 Map<GroupKey, List<Result>> toLinkedHashMap() throws DiscoveryException {
194 Map<GroupKey, List<Result>> map =
new LinkedHashMap<>();
197 sortGroupsAndFiles();
200 for (Group group : groupList) {
201 map.put(group.getGroupKey(), group.getResults());