19 package org.sleuthkit.autopsy.contentviewers.application;
 
   21 import java.util.ArrayList;
 
   22 import java.util.Arrays;
 
   23 import java.util.List;
 
   24 import java.util.function.Function;
 
   25 import java.util.logging.Level;
 
   26 import java.util.stream.Collectors;
 
   27 import org.apache.commons.lang.StringUtils;
 
   28 import org.apache.commons.lang3.tuple.Pair;
 
   29 import org.jsoup.Jsoup;
 
   30 import org.jsoup.nodes.Document;
 
   31 import org.jsoup.nodes.Element;
 
   32 import org.openide.nodes.Node;
 
   33 import org.openide.util.NbBundle;
 
   59         "Annotations.title=Annotations",
 
   60         "Annotations.toolTip=Displays tags and comments associated with the selected content.",
 
   61         "Annotations.centralRepositoryEntry.title=Central Repository Comments",
 
   62         "Annotations.centralRepositoryEntryDataLabel.case=Case:",
 
   63         "Annotations.centralRepositoryEntryDataLabel.type=Type:",
 
   64         "Annotations.centralRepositoryEntryDataLabel.comment=Comment:",
 
   65         "Annotations.centralRepositoryEntryDataLabel.path=Path:",
 
   66         "Annotations.tagEntry.title=Tags",
 
   67         "Annotations.tagEntryDataLabel.tag=Tag:",
 
   68         "Annotations.tagEntryDataLabel.tagUser=Examiner:",
 
   69         "Annotations.tagEntryDataLabel.comment=Comment:",
 
   70         "Annotations.fileHitEntry.artifactCommentTitle=Artifact Comment",
 
   71         "Annotations.fileHitEntry.hashSetHitTitle=Hash Set Hit Comments",
 
   72         "Annotations.fileHitEntry.interestingFileHitTitle=Interesting File Hit Comments",
 
   73         "Annotations.fileHitEntry.setName=Set Name:",
 
   74         "Annotations.fileHitEntry.comment=Comment:",
 
   75         "Annotations.sourceFile.title=Source File",
 
   76         "Annotations.onEmpty=No annotations were found for this particular item." 
   81     private static final String 
EMPTY_HTML = 
"<html><head></head><body></body></html>";
 
   84     private static final List<ItemEntry<Tag>> 
TAG_ENTRIES = Arrays.asList(
 
   85             new ItemEntry<>(Bundle.Annotations_tagEntryDataLabel_tag(),
 
   86                     (tag) -> (tag.getName() != null) ? tag.getName().getDisplayName() : null),
 
   87             new ItemEntry<>(Bundle.Annotations_tagEntryDataLabel_tagUser(), (tag) -> tag.getUserName()),
 
   88             new ItemEntry<>(Bundle.Annotations_tagEntryDataLabel_comment(), (tag) -> tag.getComment())
 
   91     private static final SectionConfig<Tag> 
TAG_CONFIG 
   92             = 
new SectionConfig<>(Bundle.Annotations_tagEntry_title(), 
TAG_ENTRIES);
 
   96             new ItemEntry<>(Bundle.Annotations_fileHitEntry_setName(),
 
   97                     (bba) -> 
tryGetAttribute(bba, BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME)),
 
   98             new ItemEntry<>(Bundle.Annotations_fileHitEntry_comment(),
 
   99                     (bba) -> 
tryGetAttribute(bba, BlackboardAttribute.ATTRIBUTE_TYPE.TSK_COMMENT))
 
  105     private static final SectionConfig<BlackboardArtifact> 
HASHSET_CONFIG 
  113             new ItemEntry<>(Bundle.Annotations_centralRepositoryEntryDataLabel_case(),
 
  114                     cai -> (cai.getCorrelationCase() != null) ? cai.getCorrelationCase().getDisplayName() : null),
 
  115             new ItemEntry<>(Bundle.Annotations_centralRepositoryEntryDataLabel_comment(), cai -> cai.getComment()),
 
  116             new ItemEntry<>(Bundle.Annotations_centralRepositoryEntryDataLabel_path(), cai -> cai.getFilePath())
 
  139         Document html = Jsoup.parse(EMPTY_HTML);
 
  140         Element body = html.getElementsByTag(
"body").first();
 
  142         BlackboardArtifact artifact = node.getLookup().lookup(BlackboardArtifact.class);
 
  143         Content sourceFile = null;
 
  146             if (artifact != null) {
 
  151                 sourceFile = artifact.getSleuthkitCase().getAbstractFileById(artifact.getObjectID());
 
  158                 sourceFile = node.getLookup().lookup(AbstractFile.class);
 
  160         } 
catch (TskCoreException ex) {
 
  161             logger.log(Level.SEVERE, String.format(
 
  162                     "Exception while trying to retrieve a Content instance from the BlackboardArtifact '%s' (id=%d).",
 
  163                     artifact.getDisplayName(), artifact.getArtifactID()), ex);
 
  166         boolean somethingWasRendered = 
false;
 
  167         if (artifact != null) {
 
  168             somethingWasRendered = 
renderArtifact(body, artifact, sourceFile);
 
  170             somethingWasRendered = 
renderContent(body, sourceFile, 
false);
 
  173         if (!somethingWasRendered) {
 
  190     private static boolean renderArtifact(Element parent, BlackboardArtifact bba, Content sourceContent) {
 
  196             contentRendered = contentRendered || crRendered;
 
  200         if ((BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT.getTypeID() == bba.getArtifactTypeID()
 
  201                 || BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT.getTypeID() == bba.getArtifactTypeID())
 
  205             contentRendered = contentRendered || filesetRendered;
 
  208         Element sourceFileSection = 
appendSection(parent, Bundle.Annotations_sourceFile_title());
 
  211         Element sourceFileContainer = sourceFileSection.appendElement(
"div");
 
  214         boolean sourceFileRendered = 
renderContent(sourceFileContainer, sourceContent, 
true);
 
  216         if (!sourceFileRendered) {
 
  217             sourceFileSection.remove();
 
  220         return contentRendered || sourceFileRendered;
 
  233     private static boolean renderContent(Element parent, Content sourceContent, 
boolean isSubheader) {
 
  236         if (sourceContent instanceof AbstractFile) {
 
  237             AbstractFile sourceFile = (AbstractFile) sourceContent;
 
  243                 contentRendered = contentRendered || crRendered;
 
  247                     getFileSetHits(sourceFile, BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT),
 
  252                     getFileSetHits(sourceFile, BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT),
 
  256             contentRendered = contentRendered || hashsetRendered || interestingFileRendered;
 
  258         return contentRendered;
 
  268     private static List<ContentTag> 
getTags(Content sourceContent) {
 
  271             return tskCase.getContentTagsByContent(sourceContent);
 
  273             logger.log(Level.SEVERE, 
"Exception while getting open case.", ex); 
 
  274         } 
catch (TskCoreException ex) {
 
  275             logger.log(Level.SEVERE, 
"Exception while getting tags from the case database.", ex); 
 
  277         return new ArrayList<>();
 
  287     private static List<BlackboardArtifactTag> 
getTags(BlackboardArtifact bba) {
 
  290             return tskCase.getBlackboardArtifactTagsByArtifact(bba);
 
  292             logger.log(Level.SEVERE, 
"Exception while getting open case.", ex); 
 
  293         } 
catch (TskCoreException ex) {
 
  294             logger.log(Level.SEVERE, 
"Exception while getting tags from the case database.", ex); 
 
  296         return new ArrayList<>();
 
  308     private static List<BlackboardArtifact> 
getFileSetHits(AbstractFile sourceFile, BlackboardArtifact.ARTIFACT_TYPE type) {
 
  311             return tskCase.getBlackboardArtifacts(type, sourceFile.getId()).stream()
 
  313                     .collect(Collectors.toList());
 
  315             logger.log(Level.SEVERE, 
"Exception while getting open case.", ex); 
 
  316         } 
catch (TskCoreException ex) {
 
  317             logger.log(Level.SEVERE, 
"Exception while getting file set hits from the case database.", ex); 
 
  319         return new ArrayList<>();
 
  330         return StringUtils.isNotBlank(
tryGetAttribute(artifact, BlackboardAttribute.ATTRIBUTE_TYPE.TSK_COMMENT));
 
  342     private static String 
tryGetAttribute(BlackboardArtifact artifact, BlackboardAttribute.ATTRIBUTE_TYPE attributeType) {
 
  343         if (artifact == null) {
 
  347         BlackboardAttribute attr = null;
 
  349             attr = artifact.getAttribute(
new BlackboardAttribute.Type(attributeType));
 
  350         } 
catch (TskCoreException ex) {
 
  351             logger.log(Level.WARNING, String.format(
"Unable to fetch attribute of type %s for artifact %s", attributeType, artifact), ex);
 
  358         return attr.getValueString();
 
  371         if (artifact == null) {
 
  372             return new ArrayList<>();
 
  377                 .map(cai -> Pair.of(cai.getCorrelationType(), cai.getCorrelationValue()))
 
  378                 .collect(Collectors.toList());
 
  393         if (sourceFile == null || StringUtils.isEmpty(sourceFile.getMd5Hash())) {
 
  394             return new ArrayList<>();
 
  401             logger.log(Level.SEVERE, 
"Error connecting to the Central Repository database.", ex); 
 
  404         if (artifactTypes == null || artifactTypes.isEmpty()) {
 
  405             return new ArrayList<>();
 
  408         String md5 = sourceFile.getMd5Hash();
 
  413                 .map((attributeType) -> Pair.of(attributeType, md5))
 
  414                 .collect(Collectors.toList());
 
  428         List<CorrelationAttributeInstance> instancesToRet = 
new ArrayList<>();
 
  432             for (Pair<CorrelationAttributeInstance.Type, String> typeVal : lookupKeys) {
 
  437                         .filter((cai) -> StringUtils.isNotBlank(cai.getComment()))
 
  438                         .collect(Collectors.toList()));
 
  442             logger.log(Level.SEVERE, 
"Error connecting to the Central Repository database.", ex); 
 
  444             logger.log(Level.SEVERE, 
"Error normalizing instance from Central Repository database.", ex); 
 
  447         return instancesToRet;
 
  468             boolean isSubsection, 
boolean isFirstSection) {
 
  469         if (items == null || items.isEmpty()) {
 
  474         if (!isFirstSection) {
 
  478         Element sectionContainer = sectionDiv.appendElement(
"div");
 
  499         boolean isFirst = 
true;
 
  500         for (T item : items) {
 
  505             List<List<String>> tableData = rowHeaders.stream()
 
  506                     .map(row -> Arrays.asList(row.getItemName(), row.retrieveValue(item)))
 
  507                     .collect(Collectors.toList());
 
  509             Element childTable = 
appendTable(parent, 2, tableData, null);
 
  533     private static Element 
appendTable(Element parent, 
int columnNumber, List<List<String>> content, List<String> columnHeaders) {
 
  534         Element table = parent.appendElement(
"table")
 
  535                 .attr(
"valign", 
"top")
 
  536                 .attr(
"align", 
"left");
 
  538         if (columnHeaders != null && !columnHeaders.isEmpty()) {
 
  539             Element header = table.appendElement(
"thead");
 
  540             appendRow(header, columnHeaders, columnNumber, 
true);
 
  542         Element tableBody = table.appendElement(
"tbody");
 
  544         content.forEach((rowData) -> 
appendRow(tableBody, rowData, columnNumber, 
false));
 
  559     private static Element 
appendRow(Element rowParent, List<String> data, 
int columnNumber, 
boolean isHeader) {
 
  560         String cellType = isHeader ? 
"th" : 
"td";
 
  561         Element row = rowParent.appendElement(
"tr");
 
  562         for (
int i = 0; i < columnNumber; i++) {
 
  563             Element cell = row.appendElement(cellType);
 
  569             if (data != null && i < data.size()) {
 
  570                 cell.appendElement(
"span")
 
  572                         .text(StringUtils.isEmpty(data.get(i)) ? 
"" : data.get(i));
 
  587         Element sectionDiv = parent.appendElement(
"div");
 
  588         Element header = sectionDiv.appendElement(
"h1");
 
  589         header.text(headerText);
 
  603         Element subsectionDiv = parent.appendElement(
"div");
 
  604         Element header = subsectionDiv.appendElement(
"h2");
 
  605         header.text(headerText);
 
  607         return subsectionDiv;
 
  621         Element messageEl = parent.appendElement(
"p");
 
  622         messageEl.text(message);
 
  634     static class ItemEntry<T> {
 
  636         private final String itemName;
 
  637         private final Function<T, String> valueRetriever;
 
  639         ItemEntry(String itemName, Function<T, String> valueRetriever) {
 
  640             this.itemName = itemName;
 
  641             this.valueRetriever = valueRetriever;
 
  644         String getItemName() {
 
  648         Function<T, String> getValueRetriever() {
 
  649             return valueRetriever;
 
  652         String retrieveValue(T 
object) {
 
  653             return valueRetriever.apply(
object);
 
  662     static class SectionConfig<T> {
 
  664         private final String title;
 
  665         private final List<ItemEntry<T>> attributes;
 
  667         SectionConfig(String title, List<ItemEntry<T>> attributes) {
 
  669             this.attributes = attributes;
 
  683         List<ItemEntry<T>> getAttributes() {
 
static< T > Element appendVerticalEntryTables(Element parent, List<?extends T > items, List< ItemEntry< T >> rowHeaders)
static List< ContentTag > getTags(Content sourceContent)
static String getSpacedSectionClassName()
static boolean renderArtifact(Element parent, BlackboardArtifact bba, Content sourceContent)
static final SectionConfig< BlackboardArtifact > INTERESTING_FILE_CONFIG
static final SectionConfig< BlackboardArtifact > ARTIFACT_COMMENT_CONFIG
static Element appendRow(Element rowParent, List< String > data, int columnNumber, boolean isHeader)
static final String EMPTY_HTML
static Element appendSubsection(Element parent, String headerText)
static String getTextClassName()
static Element appendSection(Element parent, String headerText)
static List< BlackboardArtifact > getFileSetHits(AbstractFile sourceFile, BlackboardArtifact.ARTIFACT_TYPE type)
List< CorrelationAttributeInstance > getArtifactInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String value)
static List< CorrelationAttributeInstance > getCentralRepositoryData(AbstractFile sourceFile)
static< T > boolean appendEntries(Element parent, Annotations.SectionConfig< T > config, List<?extends T > items, boolean isSubsection, boolean isFirstSection)
static final Logger logger
static Document buildDocument(Node node)
static final SectionConfig< Tag > TAG_CONFIG
static String getKeyColumnClassName()
static String getIndentedClassName()
List< CorrelationAttributeInstance.Type > getDefinedCorrelationTypes()
static List< CorrelationAttributeInstance > makeCorrAttrsForCorrelation(BlackboardArtifact artifact)
static Element appendTable(Element parent, int columnNumber, List< List< String >> content, List< String > columnHeaders)
static List< BlackboardArtifactTag > getTags(BlackboardArtifact bba)
static List< CorrelationAttributeInstance > getCorrelationAttributeComments(List< Pair< CorrelationAttributeInstance.Type, String >> lookupKeys)
static boolean hasTskComment(BlackboardArtifact artifact)
SleuthkitCase getSleuthkitCase()
static final SectionConfig< BlackboardArtifact > HASHSET_CONFIG
static boolean renderContent(Element parent, Content sourceContent, boolean isSubheader)
static String getHeaderClassName()
static String getMessageClassName()
static String tryGetAttribute(BlackboardArtifact artifact, BlackboardAttribute.ATTRIBUTE_TYPE attributeType)
static final List< ItemEntry< CorrelationAttributeInstance > > CR_COMMENTS_ENTRIES
static List< CorrelationAttributeInstance > getCentralRepositoryData(BlackboardArtifact artifact)
synchronized static Logger getLogger(String name)
static Case getCurrentCaseThrows()
static final List< ItemEntry< Tag > > TAG_ENTRIES
static final SectionConfig< CorrelationAttributeInstance > CR_COMMENTS_CONFIG
static final List< ItemEntry< BlackboardArtifact > > FILESET_HIT_ENTRIES
static CentralRepository getInstance()
static final int FILES_TYPE_ID
static boolean isEnabled()
static Element appendMessage(Element parent, String message)