Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
AnalysisResultsViewModel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2021-2021 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.contentviewers.analysisresults;
20 
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Objects;
25 import java.util.Optional;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
30 import org.apache.commons.lang3.tuple.Pair;
31 import org.openide.nodes.Node;
32 import org.openide.util.NbBundle;
35 import org.sleuthkit.datamodel.AnalysisResult;
36 import org.sleuthkit.datamodel.Content;
37 import org.sleuthkit.datamodel.Score;
38 import org.sleuthkit.datamodel.TskCoreException;
39 
44 
45  private static final Logger logger = Logger.getLogger(AnalysisResultsViewModel.class.getName());
46 
50  static class ResultDisplayAttributes {
51 
52  private final AnalysisResult analysisResult;
53  private final List<Pair<String, String>> attributesToDisplay;
54 
63  ResultDisplayAttributes(AnalysisResult analysisResult, List<Pair<String, String>> attributesToDisplay) {
64  this.analysisResult = analysisResult;
65  this.attributesToDisplay = attributesToDisplay;
66  }
67 
73  List<Pair<String, String>> getAttributesToDisplay() {
74  return Collections.unmodifiableList(attributesToDisplay);
75  }
76 
82  AnalysisResult getAnalysisResult() {
83  return analysisResult;
84  }
85  }
86 
91  static class NodeResults {
92 
93  private final List<ResultDisplayAttributes> analysisResults;
94  private final Optional<AnalysisResult> selectedResult;
95  private final Optional<Score> aggregateScore;
96  private final Optional<Content> content;
97 
107  NodeResults(List<ResultDisplayAttributes> analysisResults, Optional<AnalysisResult> selectedResult, Optional<Score> aggregateScore, Optional<Content> content) {
108  this.analysisResults = analysisResults;
109  this.selectedResult = selectedResult;
110  this.aggregateScore = aggregateScore;
111  this.content = content;
112  }
113 
119  List<ResultDisplayAttributes> getAnalysisResults() {
120  return Collections.unmodifiableList(analysisResults);
121  }
122 
128  Optional<AnalysisResult> getSelectedResult() {
129  return selectedResult;
130  }
131 
137  Optional<Score> getAggregateScore() {
138  return aggregateScore;
139  }
140 
148  Optional<Content> getContent() {
149  return content;
150  }
151  }
152 
161  private String normalizeAttr(String originalAttrStr) {
162  return (originalAttrStr == null) ? "" : originalAttrStr.trim();
163  }
164 
172  @NbBundle.Messages({
173  "AnalysisResultsViewModel_displayAttributes_score=Score",
174  "AnalysisResultsViewModel_displayAttributes_type=Type",
175  "AnalysisResultsViewModel_displayAttributes_configuration=Configuration",
176  "AnalysisResultsViewModel_displayAttributes_conclusion=Conclusion"
177  })
178  private ResultDisplayAttributes getDisplayAttributes(AnalysisResult analysisResult) {
179  // The type of BlackboardArtifact.Type of the analysis result.
180  String type = "";
181  try {
182  type = normalizeAttr(analysisResult.getType().getDisplayName());
183  } catch (TskCoreException ex) {
184  logger.log(Level.SEVERE, "Unable to get type for analysis result with id: " + analysisResult.getArtifactID(), ex);
185  }
186 
187  // The standard attributes to display (score, type, configuration, conclusion)
188  Stream<Pair<String, String>> baseAnalysisAttrs = Stream.of(
189  Pair.of(Bundle.AnalysisResultsViewModel_displayAttributes_score(),
190  normalizeAttr(analysisResult.getScore().getSignificance().getDisplayName())),
191  Pair.of(Bundle.AnalysisResultsViewModel_displayAttributes_type(),
192  normalizeAttr(type)),
193  Pair.of(Bundle.AnalysisResultsViewModel_displayAttributes_configuration(),
194  normalizeAttr(analysisResult.getConfiguration())),
195  Pair.of(Bundle.AnalysisResultsViewModel_displayAttributes_conclusion(),
196  normalizeAttr(analysisResult.getConclusion()))
197  );
198 
199  // The BlackboardAttributes sorted by type display name.
200  Stream<Pair<String, String>> blackboardAttributes = Stream.empty();
201  try {
202 
203  blackboardAttributes = analysisResult.getAttributes().stream()
204  .filter(attr -> attr != null && attr.getAttributeType() != null && attr.getAttributeType().getDisplayName() != null)
205  .map(attr -> Pair.of(attr.getAttributeType().getDisplayName(), normalizeAttr(attr.getDisplayString())))
206  .sorted((a, b) -> a.getKey().compareToIgnoreCase(b.getKey()));
207  } catch (TskCoreException ex) {
208  logger.log(Level.SEVERE, "Unable to get attributes for analysis result with id: " + analysisResult.getArtifactID(), ex);
209  }
210 
211  // return the standard attributes along with the key value pairs of the BlackboardAttribute values.
212  List<Pair<String, String>> allDisplayAttributes = Stream.concat(baseAnalysisAttrs, blackboardAttributes)
213  .collect(Collectors.toList());
214 
215  return new ResultDisplayAttributes(analysisResult, allDisplayAttributes);
216  }
217 
218  private List<ResultDisplayAttributes> getOrderedDisplayAttributes(Collection<AnalysisResult> analysisResults) {
219  return analysisResults.stream()
220  .filter(ar -> ar != null && ar.getScore() != null)
221  // reverse order to push more important scores to the top
222  .sorted((a, b) -> -a.getScore().compareTo(b.getScore()))
223  .map((ar) -> getDisplayAttributes(ar))
224  .collect(Collectors.toList());
225  }
226 
235  NodeResults getAnalysisResults(Node node) {
236  if (node == null) {
237  return new NodeResults(Collections.emptyList(), Optional.empty(), Optional.empty(), Optional.empty());
238  }
239 
240  Content analyzedContent = null;
241  AnalysisResult selectedAnalysisResult = null;
242  Score aggregateScore = null;
243  List<AnalysisResult> analysisResults = Collections.emptyList();
244  long selectedObjectId = 0;
245  try {
246  AnalysisResultItem analysisResultItem = node.getLookup().lookup(AnalysisResultItem.class);
247  if (Objects.nonNull(analysisResultItem)) {
248  /*
249  * The content represented by the Node is an analysis result.
250  * Set this analysis result as the analysis result to be
251  * selected in the content viewer and get the analyzed content
252  * as the source of the analysis results to display.
253  */
254  selectedAnalysisResult = analysisResultItem.getTskContent();
255  selectedObjectId = selectedAnalysisResult.getId();
256  analyzedContent = selectedAnalysisResult.getParent();
257  } else {
258  /*
259  * The content represented by the Node is something other than
260  * an analysis result. Use it as the source of the analysis
261  * results to display.
262  */
263  TskContentItem<?> contentItem = node.getLookup().lookup(TskContentItem.class);
264  analyzedContent = contentItem.getTskContent();
265  selectedObjectId = analyzedContent.getId();
266  }
267  aggregateScore = analyzedContent.getAggregateScore();
268  analysisResults = analyzedContent.getAllAnalysisResults();
269  } catch (TskCoreException ex) {
270  logger.log(Level.SEVERE, String.format("Error getting analysis result data for selected Content (object ID=%d)", selectedObjectId), ex);
271  }
272 
273  /*
274  * Use the data collected above to construct the view model.
275  */
276  List<ResultDisplayAttributes> displayAttributes = getOrderedDisplayAttributes(analysisResults);
277  return new NodeResults(displayAttributes, Optional.ofNullable(selectedAnalysisResult), Optional.ofNullable(aggregateScore), Optional.ofNullable(analyzedContent));
278  }
279 
280 }
List< ResultDisplayAttributes > getOrderedDisplayAttributes(Collection< AnalysisResult > analysisResults)

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.