19 package org.sleuthkit.autopsy.commandlineingest;
21 import com.fasterxml.jackson.core.JsonEncoding;
22 import com.fasterxml.jackson.core.JsonFactory;
23 import com.fasterxml.jackson.core.JsonGenerator;
24 import com.fasterxml.jackson.core.util.DefaultIndenter;
25 import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.sql.ResultSet;
31 import java.sql.SQLException;
32 import java.util.List;
34 import java.util.logging.Level;
49 class OutputGenerator {
51 private static final Logger logger = Logger.getLogger(OutputGenerator.class.getName());
53 private OutputGenerator() {
64 static void saveCreateCaseOutput(Case caseForJob, String outputDirPath, String baseCaseName) {
65 JsonFactory jsonGeneratorFactory =
new JsonFactory();
66 String reportOutputPath = outputDirPath + File.separator +
"createCase_" + TimeStampUtils.createTimeStamp() +
".json";
67 java.io.File reportFile = Paths.get(reportOutputPath).toFile();
69 Files.createDirectories(Paths.get(reportFile.getParent()));
70 }
catch (IOException ex) {
71 logger.log(Level.SEVERE,
"Unable to create output file " + reportFile.toString() +
" for 'Create Case' command", ex);
72 System.err.println(
"Unable to create output file " + reportFile.toString() +
" for 'Create Case' command");
76 JsonGenerator jsonGenerator = null;
78 jsonGenerator = jsonGeneratorFactory.createGenerator(reportFile, JsonEncoding.UTF8);
80 jsonGenerator.setPrettyPrinter(
new DefaultPrettyPrinter().withObjectIndenter(
new DefaultIndenter(
" ",
"\n")));
83 jsonGenerator.writeStartObject();
84 jsonGenerator.writeStringField(
"@caseName", baseCaseName);
85 jsonGenerator.writeStringField(
"@caseDir", caseForJob.getCaseDirectory());
86 jsonGenerator.writeEndObject();
87 }
catch (IOException ex) {
88 logger.log(Level.SEVERE,
"Failed to create JSON output for 'Create Case' command", ex);
89 System.err.println(
"Failed to create JSON output for 'Create Case' command");
91 if (jsonGenerator != null) {
93 jsonGenerator.close();
94 }
catch (IOException ex) {
95 logger.log(Level.WARNING,
"Failed to close JSON output file for 'Create Case' command", ex);
96 System.err.println(
"Failed to close JSON output file for 'Create Case' command");
110 static void saveAddDataSourceOutput(Case caseForJob, AutoIngestDataSource dataSource, String outputDirPath) {
112 List<Content> contentObjects = dataSource.getContent();
113 if (contentObjects == null || contentObjects.isEmpty()) {
114 logger.log(Level.SEVERE,
"No content objects for 'Add Data Source' command");
115 System.err.println(
"No content objects for 'Add Data Source' command");
119 JsonFactory jsonGeneratorFactory =
new JsonFactory();
120 String reportOutputPath = outputDirPath + File.separator +
"addDataSource_" + TimeStampUtils.createTimeStamp() +
".json";
121 java.io.File reportFile = Paths.get(reportOutputPath).toFile();
123 Files.createDirectories(Paths.get(reportFile.getParent()));
124 }
catch (IOException ex) {
125 logger.log(Level.SEVERE,
"Unable to create output file " + reportFile.toString() +
" for 'Add Data Source' command", ex);
126 System.err.println(
"Unable to create output file " + reportFile.toString() +
" for 'Add Data Source' command");
130 JsonGenerator jsonGenerator = null;
132 jsonGenerator = jsonGeneratorFactory.createGenerator(reportFile, JsonEncoding.UTF8);
134 jsonGenerator.setPrettyPrinter(
new DefaultPrettyPrinter().withObjectIndenter(
new DefaultIndenter(
" ",
"\n")));
137 for (Content content : contentObjects) {
138 String dataSourceName;
139 if (content.getDataSource() instanceof Image) {
141 dataSourceName = getImageDisplayName(caseForJob, content.getId());
142 if (dataSourceName == null) {
144 dataSourceName = content.getName();
150 AbstractFile file = caseForJob.getSleuthkitCase().getAbstractFileById(content.getId());
151 dataSourceName = file.getName();
155 saveDataSourceInfoToFile(jsonGenerator, dataSourceName, content.getId());
157 }
catch (IOException ex) {
158 logger.log(Level.SEVERE,
"Failed to create JSON output for 'Add Data Source' command", ex);
159 System.err.println(
"Failed to create JSON output for 'Add Data Source' command");
160 }
catch (TskCoreException ex) {
161 logger.log(Level.SEVERE,
"Failed to get data source info for 'Add Data Source' command output", ex);
162 System.err.println(
"Failed to get data source info for 'Add Data Source' command output");
163 }
catch (SQLException ex) {
164 logger.log(Level.SEVERE,
"Failed to get data source display name for 'Add Data Source' command output", ex);
165 System.err.println(
"Failed to get data source display name for 'Add Data Source' command output");
167 if (jsonGenerator != null) {
169 jsonGenerator.close();
170 }
catch (IOException ex) {
171 logger.log(Level.WARNING,
"Failed to close JSON output file for 'Add Data Source' command", ex);
172 System.err.println(
"Failed to close JSON output file for 'Add Data Source' command");
186 static void listAllDataSources(Case caseForJob, String outputDirPath) {
187 JsonFactory jsonGeneratorFactory =
new JsonFactory();
188 String reportOutputPath = outputDirPath + File.separator +
"listAllDataSources_" + TimeStampUtils.createTimeStamp() +
".json";
189 java.io.File reportFile = Paths.get(reportOutputPath).toFile();
191 Files.createDirectories(Paths.get(reportFile.getParent()));
192 }
catch (IOException ex) {
193 logger.log(Level.SEVERE,
"Unable to create output file " + reportFile.toString() +
" for 'List All Data Sources' command", ex);
194 System.err.println(
"Unable to create output file " + reportFile.toString() +
" for 'List All Data Sources' command");
198 JsonGenerator jsonGenerator = null;
200 jsonGenerator = jsonGeneratorFactory.createGenerator(reportFile, JsonEncoding.UTF8);
202 jsonGenerator.setPrettyPrinter(
new DefaultPrettyPrinter().withObjectIndenter(
new DefaultIndenter(
" ",
"\n")));
205 Map<Long, String> imageDataSources = DataSourceLoader.getImageDataSources(caseForJob.getSleuthkitCase());
206 for (Map.Entry<Long, String> entry : imageDataSources.entrySet()) {
207 String dataSourceName;
208 Long dataSourceObjOd = entry.getKey();
209 dataSourceName = getImageDisplayName(caseForJob, dataSourceObjOd);
210 if (dataSourceName == null) {
212 dataSourceName = entry.getValue();
214 saveDataSourceInfoToFile(jsonGenerator, dataSourceName, dataSourceObjOd);
218 Map<Long, String> logicalDataSources = DataSourceLoader.getLogicalDataSources(caseForJob.getSleuthkitCase());
219 for (Map.Entry<Long, String> entry : logicalDataSources.entrySet()) {
220 String dataSourceName = entry.getValue();
221 Long dataSourceObjOd = entry.getKey();
222 saveDataSourceInfoToFile(jsonGenerator, dataSourceName, dataSourceObjOd);
224 }
catch (IOException ex) {
225 logger.log(Level.SEVERE,
"Failed to create JSON output for 'List All Data Sources' command", ex);
226 System.err.println(
"Failed to create JSON output for 'List All Data Sources' command");
227 }
catch (TskCoreException ex) {
228 logger.log(Level.SEVERE,
"Failed to get data source info for 'List All Data Sources' command output", ex);
229 System.err.println(
"Failed to get data source info for 'List All Data Sources' command output");
230 }
catch (SQLException ex) {
231 logger.log(Level.SEVERE,
"Failed to get data source display name for 'List All Data Sources' command output", ex);
232 System.err.println(
"Failed to get data source display name for 'List All Data Sources' command output");
234 if (jsonGenerator != null) {
236 jsonGenerator.close();
237 }
catch (IOException ex) {
238 logger.log(Level.WARNING,
"Failed to close JSON output file for 'List All Data Sources' command", ex);
239 System.err.println(
"Failed to close JSON output file for 'List All Data Sources' command");
253 private static void saveDataSourceInfoToFile(JsonGenerator jsonGenerator, String dataSourceName,
long dataSourceObjId)
throws IOException {
254 jsonGenerator.writeStartObject();
255 jsonGenerator.writeStringField(
"@dataSourceName", dataSourceName);
256 jsonGenerator.writeStringField(
"@dataSourceObjectId", String.valueOf(dataSourceObjId));
257 jsonGenerator.writeEndObject();
269 private static String getImageDisplayName(Case caseForJob, Long dataSourceId)
throws TskCoreException, SQLException {
270 String getImageDataSourceQuery =
"select display_name from tsk_image_info where obj_id = " + dataSourceId;
271 try (SleuthkitCase.CaseDbQuery queryResult = caseForJob.getSleuthkitCase().executeQuery(getImageDataSourceQuery)) {
272 ResultSet resultSet = queryResult.getResultSet();
274 while (resultSet.next()) {
276 return resultSet.getString(1);