Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ExcelExportAction.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 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.report.modules.datasourcesummaryexport;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Paths;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.logging.Level;
30 import org.openide.util.NbBundle;
31 import org.openide.util.NbBundle.Messages;
40 import org.sleuthkit.datamodel.DataSource;
41 import org.sleuthkit.datamodel.TskCoreException;
42 
46 @Messages({
47  "ExcelExportAction_moduleName=Data Source Summary",})
48 class ExcelExportAction {
49 
50  private static final Logger logger = Logger.getLogger(ExcelExportAction.class.getName());
51 
57  ExcelExportAction() {
58  }
59 
68  @NbBundle.Messages({
69  "ExcelExportAction_getXLSXPath_directory=DataSourceSummary",})
70  File getXLSXPath(String dataSourceName, String baseReportDir) {
71  // set initial path to reports directory with filename that is
72  // a combination of the data source name and time stamp
73  DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy-HH-mm-ss");
74  String fileName = String.format("%s-%s.xlsx", dataSourceName == null ? "" : FileUtil.escapeFileName(dataSourceName), dateFormat.format(new Date()));
75  File reportsDirFile = Paths.get(baseReportDir, Bundle.ExcelExportAction_getXLSXPath_directory()).toFile();
76  if (!reportsDirFile.exists()) {
77  reportsDirFile.mkdirs();
78  }
79 
80  return Paths.get(reportsDirFile.getAbsolutePath(), fileName).toFile();
81  }
82 
94  @NbBundle.Messages({
95  "ExcelExportAction_exportToXLSX_beginExport=Beginning Export...",
96  "ExcelExportAction_exportToXLSX_gatheringRecentActivityData=Fetching Recent Activity Data",
97  "ExcelExportAction_exportToXLSX_gatheringContainerData=Fetching Container & Image Data",
98  "ExcelExportAction_exportToXLSX_gatheringTimelineData=Fetching Timeline Data",
99  "ExcelExportAction_exportToXLSX_gatheringFileData=Fetching File and MIME Type Data",
100  "ExcelExportAction_exportToXLSX_gatheringAnalysisData=Fetching Analysis Data",
101  "ExcelExportAction_exportToXLSX_gatheringPastData=Fetching Historical Data",
102  "ExcelExportAction_exportToXLSX_gatheringUserData=Fetching User Activity Data",
103  "ExcelExportAction_exportToXLSX_gatheringGeoData=Fetching Geolocation Data",
104  "ExcelExportAction_exportToXLSX_gatheringIngestData=Fetching Ingest History Data",
105  "ExcelExportAction_exportToXLSX_writingToFile=Writing to File...",})
106 
107  void exportToXLSX(ReportProgressPanel progressPanel, DataSource dataSource, String baseReportDir)
108  throws IOException, ExcelExport.ExcelExportException {
109 
110  File reportFile = getXLSXPath(dataSource.getName(), baseReportDir);
111  int totalWeight = 11;
112  int step = 1;
113  progressPanel.setIndeterminate(false);
114  progressPanel.setLabels(dataSource.getName(), reportFile.getPath());
115  progressPanel.setMaximumProgress(totalWeight);
116  progressPanel.updateStatusLabel(Bundle.ExcelExportAction_exportToXLSX_beginExport());
117  List<ExcelExport.ExcelSheetExport> sheetExports = new ArrayList<>();
118 
119  // Export file and MIME type data
120  progressPanel.updateStatusLabel(Bundle.ExcelExportAction_exportToXLSX_gatheringFileData());
121  progressPanel.setProgress(step);
122  List<ExcelExport.ExcelSheetExport> exports = new ExportTypes().getExports(dataSource);
123  if (exports != null) {
124  sheetExports.addAll(exports);
125  }
126 
127  // Export user activity
128  progressPanel.updateStatusLabel(Bundle.ExcelExportAction_exportToXLSX_gatheringUserData());
129  progressPanel.setProgress(++step);
130  exports = new ExportUserActivity().getExports(dataSource);
131  if (exports != null) {
132  sheetExports.addAll(exports);
133  }
134 
135  // Export Recent Activity data
136  progressPanel.updateStatusLabel(Bundle.ExcelExportAction_exportToXLSX_gatheringRecentActivityData());
137  progressPanel.setProgress(++step);
138  exports = new ExportRecentFiles().getExports(dataSource);
139  if (exports != null) {
140  sheetExports.addAll(exports);
141  }
142 
143  // Export hash set hits, keyword hits, and interesting item hits
144  progressPanel.updateStatusLabel(Bundle.ExcelExportAction_exportToXLSX_gatheringAnalysisData());
145  progressPanel.setProgress(++step);
146  exports = new ExportAnalysisResults().getExports(dataSource);
147  if (exports != null) {
148  sheetExports.addAll(exports);
149  }
150 
151  // Export past cases data
152  progressPanel.updateStatusLabel(Bundle.ExcelExportAction_exportToXLSX_gatheringPastData());
153  progressPanel.setProgress(++step);
154  exports = new ExportPastCases().getExports(dataSource);
155  if (exports != null) {
156  sheetExports.addAll(exports);
157  }
158 
159  // Export geolocation data
160  progressPanel.updateStatusLabel(Bundle.ExcelExportAction_exportToXLSX_gatheringGeoData());
161  progressPanel.setProgress(++step);
162  exports = new ExportGeolocation().getExports(dataSource);
163  if (exports != null) {
164  sheetExports.addAll(exports);
165  }
166 
167  // Export Timeline data
168  progressPanel.updateStatusLabel(Bundle.ExcelExportAction_exportToXLSX_gatheringTimelineData());
169  progressPanel.setProgress(++step);
170  exports = new ExportTimeline().getExports(dataSource);
171  if (exports != null) {
172  sheetExports.addAll(exports);
173  }
174 
175  // Export ingest history
176  progressPanel.updateStatusLabel(Bundle.ExcelExportAction_exportToXLSX_gatheringIngestData());
177  progressPanel.setProgress(++step);
178  exports = ExportIngestHistory.getExports(dataSource);
179  if (exports != null) {
180  sheetExports.addAll(exports);
181  }
182 
183  // Export Container & Image info data
184  progressPanel.updateStatusLabel(Bundle.ExcelExportAction_exportToXLSX_gatheringContainerData());
185  progressPanel.setProgress(++step);
186  exports = new ExportContainerInfo().getExports(dataSource);
187  if (exports != null) {
188  sheetExports.addAll(exports);
189  }
190 
191  progressPanel.updateStatusLabel(Bundle.ExcelExportAction_exportToXLSX_writingToFile());
192  progressPanel.setProgress(++step);
193  ExcelExport.writeExcel(sheetExports, reportFile);
194 
195  try {
196  // add to reports
197  Case curCase = Case.getCurrentCaseThrows();
198  curCase.addReport(reportFile.getParent(),
199  Bundle.ExcelExportAction_moduleName(),
200  reportFile.getName(),
201  dataSource);
202  } catch (NoCurrentCaseException | TskCoreException ex) {
203  logger.log(Level.WARNING, "There was an error attaching report to case.", ex);
204  }
205  }
206 
210  protected interface ExcelExportFunction<T> {
211 
221  ExcelSheetExport convert(T data) throws ExcelExportException;
222  }
223 
234  protected static <T> T getFetchResult(
235  DataFetcher<DataSource, T> dataFetcher,
236  String sheetName, DataSource ds) {
237 
238  try {
239  return dataFetcher.runQuery(ds);
240  } catch (Exception ex) {
241  logger.log(Level.WARNING,
242  String.format("There was an error while acquiring data for exporting worksheet(s): '%s' for dataSource: %s",
243  sheetName == null ? "<null>" : sheetName,
244  ds == null || ds.getName() == null ? "<null>" : ds.getName()), ex);
245  return null;
246  }
247  }
248 
260  protected static <T> ExcelSheetExport convertToExcel(ExcelExportFunction<T> excelConverter, T data, String sheetName) {
261  if (data == null) {
262  return null;
263  }
264 
265  try {
266  return excelConverter.convert(data);
267  } catch (ExcelExportException ex) {
268  logger.log(Level.WARNING,
269  String.format("There was an error while preparing export of worksheet(s): '%s'",
270  sheetName == null ? "<null>" : sheetName), ex);
271  return null;
272  }
273  }
274 
287  protected static <T> ExcelSheetExport getExport(
288  DataFetcher<DataSource, T> dataFetcher, ExcelExportFunction<T> excelConverter,
289  String sheetName, DataSource ds) {
290 
291  T data = getFetchResult(dataFetcher, sheetName, ds);
292  return convertToExcel(excelConverter, data, sheetName);
293  }
294 
304  protected static <T, C extends CellModel> ExcelSheetExport getTableExport(List<ColumnModel<T, C>> columnsModel,
305  String sheetName, List<T> data) {
306 
307  return convertToExcel((dataList) -> new ExcelTableExport<>(sheetName, columnsModel, dataList),
308  data,
309  sheetName);
310  }
311 
323  protected static <T, C extends CellModel> ExcelSheetExport getTableExport(
324  DataFetcher<DataSource, List<T>> dataFetcher, List<ColumnModel<T, C>> columnsModel,
325  String sheetName, DataSource ds) {
326 
327  return getExport(dataFetcher,
328  (dataList) -> new ExcelTableExport<>(sheetName, columnsModel, dataList),
329  sheetName,
330  ds);
331  }
332 }

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.