Autopsy  4.5.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ReportGenerator.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013 - 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.report;
20 
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;
27 import java.io.File;
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;
35 import java.util.Map;
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;
49 import org.sleuthkit.datamodel.AbstractFile;
50 import org.sleuthkit.datamodel.BlackboardArtifact;
51 import org.sleuthkit.datamodel.SleuthkitCase;
52 import org.sleuthkit.datamodel.TskCoreException;
53 import org.sleuthkit.datamodel.TskData;
54 
55 class ReportGenerator {
56 
57  private static final Logger logger = Logger.getLogger(ReportGenerator.class.getName());
58 
59  private Case currentCase = Case.getCurrentCase();
60 
64  private ReportProgressPanel progressPanel;
65 
66  private static final String REPORT_PATH_FMT_STR = "%s" + File.separator + "%s %s %s" + File.separator;
67  private final ReportGenerationPanel reportGenerationPanel = new ReportGenerationPanel();
68 
69  static final String REPORTS_DIR = "Reports"; //NON-NLS
70 
71  private List<String> errorList;
72 
77  private void displayReportErrors() {
78  if (!errorList.isEmpty()) {
79  String errorString = "";
80  for (String error : errorList) {
81  errorString += error + "\n";
82  }
83  MessageNotifyUtil.Notify.error(
84  NbBundle.getMessage(this.getClass(), "ReportGenerator.notifyErr.errsDuringRptGen"), errorString);
85  }
86  }
87 
91  ReportGenerator() {
92  this.errorList = new ArrayList<>();
93  }
94 
95 
100  private void displayProgressPanel() {
101  final JDialog dialog = new JDialog((JFrame) WindowManager.getDefault().getMainWindow(), true);
102  dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
103  dialog.setTitle(NbBundle.getMessage(this.getClass(), "ReportGenerator.displayProgress.title.text"));
104  dialog.add(this.reportGenerationPanel);
105  dialog.pack();
106 
107  reportGenerationPanel.addCloseAction(new ActionListener() {
108  @Override
109  public void actionPerformed(ActionEvent e) {
110  dialog.dispose();
111  }
112  });
113 
114  dialog.addWindowListener(new WindowAdapter() {
115  @Override
116  public void windowClosing(WindowEvent e) {
117  reportGenerationPanel.close();
118  }
119  });
120 
121  Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
122  int w = dialog.getSize().width;
123  int h = dialog.getSize().height;
124 
125  // set the location of the popUp Window on the center of the screen
126  dialog.setLocation((screenDimension.width - w) / 2, (screenDimension.height - h) / 2);
127  dialog.setVisible(true);
128  }
129 
133  void generateGeneralReport(GeneralReportModule generalReportModule) throws IOException {
134  if (generalReportModule != null) {
135  String reportDir = createReportDirectory(generalReportModule);
136  setupProgressPanel(generalReportModule, reportDir);
137  ReportWorker worker = new ReportWorker(() -> {
138  generalReportModule.generateReport(reportDir, progressPanel);
139  });
140  worker.execute();
141  displayProgressPanel();
142  }
143  }
144 
153  void generateTableReport(TableReportModule tableReport, Map<BlackboardArtifact.Type, Boolean> artifactTypeSelections, Map<String, Boolean> tagNameSelections) throws IOException {
154  if (tableReport != null && null != artifactTypeSelections) {
155  String reportDir = createReportDirectory(tableReport);
156  setupProgressPanel(tableReport, reportDir);
157  ReportWorker worker = new ReportWorker(() -> {
158  tableReport.startReport(reportDir);
159  TableReportGenerator generator = new TableReportGenerator(artifactTypeSelections, tagNameSelections, progressPanel, tableReport);
160  generator.execute();
161  tableReport.endReport();
162  // finish progress, wrap up
163  progressPanel.complete(ReportProgressPanel.ReportStatus.COMPLETE);
164  errorList = generator.getErrorList();
165  });
166  worker.execute();
167  displayProgressPanel();
168  }
169  }
170 
177  void generateFileListReport(FileReportModule fileReportModule, Map<FileReportDataTypes, Boolean> enabledInfo) throws IOException {
178  if (fileReportModule != null && null != enabledInfo) {
179  String reportDir = createReportDirectory(fileReportModule);
180  List<FileReportDataTypes> enabled = new ArrayList<>();
181  for (Entry<FileReportDataTypes, Boolean> e : enabledInfo.entrySet()) {
182  if (e.getValue()) {
183  enabled.add(e.getKey());
184  }
185  }
186  setupProgressPanel(fileReportModule, reportDir);
187  ReportWorker worker = new ReportWorker(() -> {
188  if (progressPanel.getStatus() != ReportStatus.CANCELED) {
189  progressPanel.start();
190  progressPanel.updateStatusLabel(
191  NbBundle.getMessage(this.getClass(), "ReportGenerator.progress.queryingDb.text"));
192  }
193 
194  List<AbstractFile> files = getFiles();
195  int numFiles = files.size();
196  if (progressPanel.getStatus() != ReportStatus.CANCELED) {
197  fileReportModule.startReport(reportDir);
198  fileReportModule.startTable(enabled);
199  }
200  progressPanel.setIndeterminate(false);
201  progressPanel.setMaximumProgress(numFiles);
202 
203  int i = 0;
204  // Add files to report.
205  for (AbstractFile file : files) {
206  // Check to see if any reports have been cancelled.
207  if (progressPanel.getStatus() == ReportStatus.CANCELED) {
208  return;
209  } else {
210  fileReportModule.addRow(file, enabled);
211  progressPanel.increment();
212  }
213 
214  if ((i % 100) == 0) {
215  progressPanel.updateStatusLabel(
216  NbBundle.getMessage(this.getClass(), "ReportGenerator.progress.processingFile.text",
217  file.getName()));
218  }
219  i++;
220  }
221 
222  fileReportModule.endTable();
223  fileReportModule.endReport();
224  progressPanel.complete(ReportStatus.COMPLETE);
225  });
226  worker.execute();
227  displayProgressPanel();
228  }
229  }
230 
236  private List<AbstractFile> getFiles() {
237  List<AbstractFile> absFiles;
238  try {
239  SleuthkitCase skCase = Case.getCurrentCase().getSleuthkitCase();
240  absFiles = skCase.findAllFilesWhere("meta_type != " + TskData.TSK_FS_META_TYPE_ENUM.TSK_FS_META_TYPE_DIR.getValue()); //NON-NLS
241  return absFiles;
242  } catch (TskCoreException ex) {
243  MessageNotifyUtil.Notify.show(
244  NbBundle.getMessage(this.getClass(), "ReportGenerator.errors.reportErrorTitle"),
245  NbBundle.getMessage(this.getClass(), "ReportGenerator.errors.reportErrorText") + ex.getLocalizedMessage(),
246  MessageNotifyUtil.MessageType.ERROR);
247  logger.log(Level.SEVERE, "failed to generate reports. Unable to get all files in the image.", ex); //NON-NLS
248  return Collections.<AbstractFile>emptyList();
249  }
250  }
251 
252  private void setupProgressPanel(ReportModule module, String reportDir) {
253  String reportFilePath = module.getRelativeFilePath();
254  if (!reportFilePath.isEmpty()) {
255  this.progressPanel = reportGenerationPanel.addReport(module.getName(), reportDir + reportFilePath);
256  } else {
257  this.progressPanel = reportGenerationPanel.addReport(module.getName(), null);
258  }
259  }
260 
261  private static String createReportDirectory(ReportModule module) throws IOException {
262  Case currentCase = Case.getCurrentCase();
263  // Create the root reports directory path of the form: <CASE DIRECTORY>/Reports/<Case fileName> <Timestamp>/
264  DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy-HH-mm-ss");
265  Date date = new Date();
266  String dateNoTime = dateFormat.format(date);
267  String reportPath = String.format(REPORT_PATH_FMT_STR, currentCase.getReportDirectory(), currentCase.getDisplayName(), module.getName(), dateNoTime);
268  // Create the root reports directory.
269  try {
270  FileUtil.createFolder(new File(reportPath));
271  } catch (IOException ex) {
272  throw new IOException("Failed to make report folder, unable to generate reports.", ex);
273  }
274  return reportPath;
275  }
276 
277  private class ReportWorker extends SwingWorker<Void, Void> {
278 
279  private final Runnable doInBackground;
280 
281  private ReportWorker(Runnable doInBackground) {
282  this.doInBackground = doInBackground;
283  }
284 
285  @Override
286  protected Void doInBackground() throws Exception {
287  doInBackground.run();
288  return null;
289  }
290 
291  @Override
292  protected void done() {
293  try {
294  get();
295  } catch (InterruptedException | ExecutionException ex) {
297  NbBundle.getMessage(this.getClass(), "ReportGenerator.errors.reportErrorTitle"),
298  NbBundle.getMessage(this.getClass(), "ReportGenerator.errors.reportErrorText") + ex.getLocalizedMessage(),
300  logger.log(Level.SEVERE, "failed to generate reports", ex); //NON-NLS
301  } // catch and ignore if we were cancelled
302  catch (java.util.concurrent.CancellationException ex) {
303  } finally {
304  displayReportErrors();
305  errorList.clear();
306  }
307  }
308 
309  }
310 }
static void show(String title, String message, MessageType type, ActionListener actionListener)

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