Autopsy  4.6.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ArtifactTextExtractor.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 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.keywordsearch;
20 
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.Reader;
24 import java.nio.charset.StandardCharsets;
25 import java.util.logging.Level;
26 import org.apache.commons.io.IOUtils;
31 import org.sleuthkit.datamodel.AbstractFile;
32 import org.sleuthkit.datamodel.BlackboardArtifact;
33 import org.sleuthkit.datamodel.BlackboardAttribute;
34 import org.sleuthkit.datamodel.Content;
35 import org.sleuthkit.datamodel.SleuthkitCase;
36 import org.sleuthkit.datamodel.TskCoreException;
37 
42 class ArtifactTextExtractor implements TextExtractor<BlackboardArtifact> {
43 
44  static final private Logger logger = Logger.getLogger(ArtifactTextExtractor.class.getName());
45 
57  static Content getDataSource(BlackboardArtifact artifact) throws TskCoreException {
58 
59  Case currentCase;
60  try {
61  currentCase = Case.getOpenCase();
62  } catch (NoCurrentCaseException ignore) {
63  // thorown by Case.getOpenCase() if currentCase is null
64  return null;
65  }
66 
67  SleuthkitCase sleuthkitCase = currentCase.getSleuthkitCase();
68  if (sleuthkitCase == null) {
69  return null;
70 
71  }
72  Content dataSource;
73  AbstractFile abstractFile = sleuthkitCase.getAbstractFileById(artifact.getObjectID());
74  if (abstractFile != null) {
75  dataSource = abstractFile.getDataSource();
76  } else {
77  dataSource = sleuthkitCase.getContentById(artifact.getObjectID());
78  }
79 
80  if (dataSource == null) {
81  return null;
82  }
83  return dataSource;
84  }
85 
86  @Override
87  public boolean isDisabled() {
88  return false;
89  }
90 
91  @Override
92  public void logWarning(final String msg, Exception ex) {
93  logger.log(Level.WARNING, msg, ex); //NON-NLS }
94  }
95 
96  private InputStream getInputStream(BlackboardArtifact artifact) throws TextExtractorException {
97  // Concatenate the string values of all attributes into a single
98  // "content" string to be indexed.
99  StringBuilder artifactContents = new StringBuilder();
100 
101  Content dataSource = null;
102  try {
103  dataSource = getDataSource(artifact);
104  } catch (TskCoreException tskCoreException) {
105  throw new TextExtractorException("Unable to get datasource for artifact: " + artifact.toString(), tskCoreException);
106  }
107  if (dataSource == null) {
108  throw new TextExtractorException("Datasource was null for artifact: " + artifact.toString());
109  }
110 
111  try {
112  for (BlackboardAttribute attribute : artifact.getAttributes()) {
113  artifactContents.append(attribute.getAttributeType().getDisplayName());
114  artifactContents.append(" : ");
115  // We have also discussed modifying BlackboardAttribute.getDisplayString()
116  // to magically format datetime attributes but that is complicated by
117  // the fact that BlackboardAttribute exists in Sleuthkit data model
118  // while the utility to determine the timezone to use is in ContentUtils
119  // in the Autopsy datamodel.
120  switch (attribute.getValueType()) {
121  case DATETIME:
122  artifactContents.append(ContentUtils.getStringTime(attribute.getValueLong(), dataSource));
123  break;
124  default:
125  artifactContents.append(attribute.getDisplayString());
126  }
127  artifactContents.append(System.lineSeparator());
128  }
129  } catch (TskCoreException tskCoreException) {
130  throw new TextExtractorException("Unable to get attributes for artifact: " + artifact.toString(), tskCoreException);
131  }
132 
133  return IOUtils.toInputStream(artifactContents, StandardCharsets.UTF_8);
134  }
135 
136  @Override
137  public Reader getReader(BlackboardArtifact source) throws TextExtractorException {
138  return new InputStreamReader(getInputStream(source), StandardCharsets.UTF_8);
139  }
140 
141  @Override
142  public long getID(BlackboardArtifact source) {
143  return source.getArtifactID();
144  }
145 
146  @Override
147  public String getName(BlackboardArtifact source) {
148  return source.getDisplayName() + "_" + source.getArtifactID();
149  }
150 }

Copyright © 2012-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.