19 package org.sleuthkit.autopsy.report;
21 import java.awt.Dimension;
22 import java.awt.Toolkit;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.awt.event.WindowAdapter;
26 import java.awt.event.WindowEvent;
28 import java.io.IOException;
29 import java.text.DateFormat;
30 import java.text.SimpleDateFormat;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Date;
34 import java.util.List;
36 import java.util.Map.Entry;
37 import java.util.concurrent.ExecutionException;
38 import java.util.logging.Level;
39 import javax.swing.JDialog;
40 import javax.swing.JFrame;
41 import javax.swing.SwingWorker;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.util.NbBundle;
44 import org.openide.windows.WindowManager;
55 class ReportGenerator {
57 private static final Logger logger = Logger.getLogger(ReportGenerator.class.getName());
59 private Case currentCase = Case.getCurrentCase();
64 private ReportProgressPanel progressPanel;
66 private final String reportPath;
67 private final ReportGenerationPanel reportGenerationPanel =
new ReportGenerationPanel();
69 static final String REPORTS_DIR =
"Reports";
71 private List<String> errorList;
77 private void displayReportErrors() {
78 if (!errorList.isEmpty()) {
79 String errorString =
"";
80 for (String error : errorList) {
81 errorString += error +
"\n";
83 MessageNotifyUtil.Notify.error(
84 NbBundle.getMessage(
this.getClass(),
"ReportGenerator.notifyErr.errsDuringRptGen"), errorString);
93 DateFormat dateFormat =
new SimpleDateFormat(
"MM-dd-yyyy-HH-mm-ss");
94 Date date =
new Date();
95 String dateNoTime = dateFormat.format(date);
96 this.reportPath = currentCase.getReportDirectory() + File.separator + currentCase.getName() +
" " + dateNoTime + File.separator;
98 this.errorList =
new ArrayList<>();
102 FileUtil.createFolder(
new File(this.reportPath));
103 }
catch (IOException ex) {
104 errorList.add(NbBundle.getMessage(
this.getClass(),
"ReportGenerator.errList.failedMakeRptFolder"));
105 logger.log(Level.SEVERE,
"Failed to make report folder, may be unable to generate reports.", ex);
113 private void displayProgressPanel() {
114 final JDialog dialog =
new JDialog((JFrame) WindowManager.getDefault().getMainWindow(),
true);
115 dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
116 dialog.setTitle(NbBundle.getMessage(
this.getClass(),
"ReportGenerator.displayProgress.title.text"));
117 dialog.add(this.reportGenerationPanel);
120 reportGenerationPanel.addCloseAction(
new ActionListener() {
122 public void actionPerformed(ActionEvent e) {
127 dialog.addWindowListener(
new WindowAdapter() {
129 public void windowClosing(WindowEvent e) {
130 reportGenerationPanel.close();
134 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
135 int w = dialog.getSize().width;
136 int h = dialog.getSize().height;
139 dialog.setLocation((screenDimension.width - w) / 2, (screenDimension.height - h) / 2);
140 dialog.setVisible(
true);
146 void generateGeneralReport(GeneralReportModule generalReportModule) {
147 if (generalReportModule != null) {
148 setupProgressPanel(generalReportModule);
149 ReportWorker worker =
new ReportWorker(() -> {
150 generalReportModule.generateReport(reportPath, progressPanel);
153 displayProgressPanel();
165 void generateTableReport(TableReportModule tableReport, Map<BlackboardArtifact.Type, Boolean> artifactTypeSelections, Map<String, Boolean> tagNameSelections) {
166 if (tableReport != null && null != artifactTypeSelections) {
167 setupProgressPanel(tableReport);
168 ReportWorker worker =
new ReportWorker(() -> {
169 tableReport.startReport(reportPath);
170 TableReportGenerator generator =
new TableReportGenerator(artifactTypeSelections, tagNameSelections, progressPanel, tableReport);
172 tableReport.endReport();
173 errorList = generator.getErrorList();
176 displayProgressPanel();
186 void generateFileListReport(FileReportModule fileReportModule, Map<FileReportDataTypes, Boolean> enabledInfo) {
187 if (fileReportModule != null && null != enabledInfo) {
188 List<FileReportDataTypes> enabled =
new ArrayList<>();
189 for (Entry<FileReportDataTypes, Boolean> e : enabledInfo.entrySet()) {
191 enabled.add(e.getKey());
194 setupProgressPanel(fileReportModule);
195 ReportWorker worker =
new ReportWorker(() -> {
196 if (progressPanel.getStatus() != ReportStatus.CANCELED) {
197 progressPanel.start();
198 progressPanel.updateStatusLabel(
199 NbBundle.getMessage(
this.getClass(),
"ReportGenerator.progress.queryingDb.text"));
202 List<AbstractFile> files = getFiles();
203 int numFiles = files.size();
204 if (progressPanel.getStatus() != ReportStatus.CANCELED) {
205 fileReportModule.startReport(reportPath);
206 fileReportModule.startTable(enabled);
208 progressPanel.setIndeterminate(
false);
209 progressPanel.setMaximumProgress(numFiles);
213 for (AbstractFile file : files) {
215 if (progressPanel.getStatus() == ReportStatus.CANCELED) {
218 fileReportModule.addRow(file, enabled);
219 progressPanel.increment();
222 if ((i % 100) == 0) {
223 progressPanel.updateStatusLabel(
224 NbBundle.getMessage(
this.getClass(),
"ReportGenerator.progress.processingFile.text",
230 fileReportModule.endTable();
231 fileReportModule.endReport();
232 progressPanel.complete(ReportStatus.COMPLETE);
235 displayProgressPanel();
244 private List<AbstractFile> getFiles() {
245 List<AbstractFile> absFiles;
247 SleuthkitCase skCase = Case.getCurrentCase().getSleuthkitCase();
248 absFiles = skCase.findAllFilesWhere(
"meta_type != " + TskData.TSK_FS_META_TYPE_ENUM.TSK_FS_META_TYPE_DIR.getValue());
250 }
catch (TskCoreException ex) {
251 MessageNotifyUtil.Notify.show(
252 NbBundle.getMessage(
this.getClass(),
"ReportGenerator.errors.reportErrorTitle"),
253 NbBundle.getMessage(
this.getClass(),
"ReportGenerator.errors.reportErrorText") + ex.getLocalizedMessage(),
254 MessageNotifyUtil.MessageType.ERROR);
255 logger.log(Level.SEVERE,
"failed to generate reports. Unable to get all files in the image.", ex);
256 return Collections.<AbstractFile>emptyList();
260 private void setupProgressPanel(ReportModule module) {
261 String reportFilePath = module.getRelativeFilePath();
262 if (!reportFilePath.isEmpty()) {
263 this.progressPanel = reportGenerationPanel.addReport(module.getName(), reportPath + reportFilePath);
265 this.progressPanel = reportGenerationPanel.addReport(module.getName(), null);
279 doInBackground.run();
287 }
catch (InterruptedException | ExecutionException ex) {
289 NbBundle.getMessage(
this.getClass(),
"ReportGenerator.errors.reportErrorTitle"),
290 NbBundle.getMessage(
this.getClass(),
"ReportGenerator.errors.reportErrorText") + ex.getLocalizedMessage(),
292 logger.log(Level.SEVERE,
"failed to generate reports", ex);
294 catch (java.util.concurrent.CancellationException ex) {
296 displayReportErrors();
static void show(String title, String message, MessageType type, ActionListener actionListener)
final Runnable doInBackground
ReportWorker(Runnable doInBackground)