Autopsy  4.17.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ReportWizardAction.java
Go to the documentation of this file.
1 /*
2  *
3  * Autopsy Forensic Browser
4  *
5  * Copyright 2012-2020 Basis Technology Corp.
6  *
7  * Copyright 2012 42six Solutions.
8  * Contact: aebadirad <at> 42six <dot> com
9  * Project Contact/Architect: carrier <at> sleuthkit <dot> org
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.sleuthkit.autopsy.report.infrastructure;
24 
27 import java.awt.Component;
28 import java.awt.Cursor;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.beans.PropertyChangeEvent;
32 import java.text.MessageFormat;
33 import java.util.EnumSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37 import java.util.concurrent.ExecutionException;
38 import java.util.logging.Level;
39 import javax.swing.ImageIcon;
40 import javax.swing.JButton;
41 import javax.swing.SwingWorker;
42 import org.openide.DialogDisplayer;
43 import org.openide.NotifyDescriptor;
44 import org.openide.WizardDescriptor;
45 import org.openide.awt.ActionID;
46 import org.openide.awt.ActionReference;
47 import org.openide.awt.ActionReferences;
48 import org.openide.awt.ActionRegistration;
49 import org.openide.util.HelpCtx;
50 import org.openide.util.NbBundle;
51 import org.openide.util.actions.CallableSystemAction;
52 import org.openide.util.actions.Presenter;
53 import org.openide.windows.WindowManager;
59 
60 @ActionID(category = "Tools", id = "org.sleuthkit.autopsy.report.infrastructure.ReportWizardAction")
61 @ActionRegistration(displayName = "#CTL_ReportWizardAction", lazy = false)
62 @ActionReferences(value = {
63  @ActionReference(path = "Menu/Tools", position = 301, separatorAfter = 399)
64  ,
65  @ActionReference(path = "Toolbars/Case", position = 106)})
66 public final class ReportWizardAction extends CallableSystemAction implements Presenter.Toolbar, ActionListener {
67 
68  private static final Logger logger = Logger.getLogger(ReportWizardAction.class.getName());
69  private static final String REPORTING_CONFIGURATION_NAME = "ReportAction";
70  private static final boolean DISPLAY_CASE_SPECIFIC_DATA = true;
71  private static final boolean RUN_REPORTS = true;
72  private final JButton toolbarButton = new JButton();
73  private static final String ACTION_NAME = NbBundle.getMessage(ReportWizardAction.class, "ReportWizardAction.actionName.text");
74  private static ReportGenerationPanel panel;
75 
87  @SuppressWarnings("unchecked")
88  public static void doReportWizard(String configName, boolean displayCaseSpecificData, boolean runReports) {
89  WizardDescriptor wiz = new WizardDescriptor(new ReportWizardIterator(configName, displayCaseSpecificData));
90  wiz.setTitleFormat(new MessageFormat("{0} {1}"));
91  wiz.setTitle(NbBundle.getMessage(ReportWizardAction.class, "ReportWizardAction.reportWiz.title"));
92  if (DialogDisplayer.getDefault().notify(wiz) == WizardDescriptor.FINISH_OPTION) {
93 
94  // save reporting configuration
95  try {
96  saveReportingConfiguration(configName, wiz);
97  } catch (ReportConfigException ex) {
98  logger.log(Level.SEVERE, "Failed to save reporting configuration " + configName, ex); //NON-NLS
99  NotifyDescriptor descriptor = new NotifyDescriptor.Message(
100  NbBundle.getMessage(ReportWizardAction.class, "ReportWizardAction.unableToSaveConfig.errorLabel.text"),
101  NotifyDescriptor.ERROR_MESSAGE);
102  DialogDisplayer.getDefault().notify(descriptor);
103  }
104 
105  if (runReports) {
106  // generate reports in a separate thread
107  panel = new ReportGenerationPanel();
108  Map<String, ReportModule> modules = (Map<String, ReportModule>) wiz.getProperty("modules");
109  ReportGenerator generator = new ReportGenerator(configName, panel); //NON-NLS
110  ReportWorker worker = new ReportWorker(() -> {
111  generator.generateReports(modules);
112  });
113  worker.execute();
114  generator.displayProgressPanel();
115  }
116  }
117  }
118 
119  @SuppressWarnings(value = "unchecked")
120  private static void saveReportingConfiguration(String configName, WizardDescriptor wiz) throws ReportConfigException {
121 
122  ReportingConfig reportingConfig = new ReportingConfig(configName);
123  List<Long> selectedDataSourceIds = (List<Long>) wiz.getProperty("dataSourceSelections");
124 
125  // Set the selected data source ids.
126  FileReportSettings fileSettings = (FileReportSettings) wiz.getProperty("fileReportSettings");
127  TableReportSettings tableSettings = (TableReportSettings) wiz.getProperty("tableReportSettings");
128  GeneralReportSettings generalSettings = new GeneralReportSettings();
129  if(selectedDataSourceIds != null) {
130  generalSettings.setSelectedDataSources(selectedDataSourceIds);
131  if(fileSettings != null) {
132  fileSettings.setSelectedDataSources(selectedDataSourceIds);
133  }
134  if(tableSettings != null) {
135  tableSettings.setSelectedDataSources(selectedDataSourceIds);
136  }
137  }
138 
139  reportingConfig.setFileReportSettings(fileSettings);
140  reportingConfig.setTableReportSettings(tableSettings);
141  reportingConfig.setGeneralReportSettings(generalSettings);
142 
143  Map<String, ReportModuleConfig> moduleConfigs = (Map<String, ReportModuleConfig>) wiz.getProperty("moduleConfigs");
144 
145  // update portable case settings
146  ReportModuleConfig config = moduleConfigs.get(PortableCaseReportModule.class.getCanonicalName());
147  PortableCaseReportModuleSettings portableCaseReportSettings = (PortableCaseReportModuleSettings) wiz.getProperty("portableCaseReportSettings");
148  if (portableCaseReportSettings != null) {
149  config.setModuleSettings(portableCaseReportSettings);
150  moduleConfigs.put(PortableCaseReportModule.class.getCanonicalName(), config);
151  }
152 
153  // set module configs
154  reportingConfig.setModuleConfigs(moduleConfigs);
155 
156  // save reporting configuration
157  ReportingConfigLoader.saveConfig(reportingConfig);
158  }
159 
161  setEnabled(false);
162  Case.addEventTypeSubscriber(EnumSet.of(Case.Events.CURRENT_CASE), (PropertyChangeEvent evt) -> {
163  if (evt.getPropertyName().equals(Case.Events.CURRENT_CASE.toString())) {
164  Case newCase = (Case) evt.getNewValue();
165  setEnabled(newCase != null && RuntimeProperties.runningWithGUI());
166  }
167  });
168 
169  // Initialize the Generate Report button
170  toolbarButton.addActionListener(ReportWizardAction.this::actionPerformed);
171  }
172 
173  @Override
174  @SuppressWarnings("unchecked")
175  public void actionPerformed(ActionEvent e) {
176  WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
177  doReportWizard(REPORTING_CONFIGURATION_NAME, DISPLAY_CASE_SPECIFIC_DATA, RUN_REPORTS);
178  WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
179  }
180 
181  @Override
182  public void performAction() {
183  }
184 
185  @Override
186  public String getName() {
187  return ACTION_NAME;
188  }
189 
190  @Override
191  public HelpCtx getHelpCtx() {
192  return HelpCtx.DEFAULT_HELP;
193  }
194 
200  @Override
201  public Component getToolbarPresenter() {
202  ImageIcon icon = new ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/report/images/btn_icon_generate_report.png")); //NON-NLS
203  toolbarButton.setIcon(icon);
204  toolbarButton.setText(NbBundle.getMessage(this.getClass(), "ReportWizardAction.toolBarButton.text"));
205  return toolbarButton;
206  }
207 
215  public static Set<String> getReportConfigNames() {
216  Set<String> nameList = ReportingConfigLoader.getListOfReportConfigs();
217  //Remove this default name, users cannot change this report.
218  nameList.remove(REPORTING_CONFIGURATION_NAME);
219 
220  return nameList;
221  }
222 
228  @Override
229  public void setEnabled(boolean value) {
230  super.setEnabled(value);
231  toolbarButton.setEnabled(value);
232  }
233 
234  private static class ReportWorker extends SwingWorker<Void, Void> {
235 
236  private final Runnable doInBackground;
237 
238  private ReportWorker(Runnable doInBackground) {
239  this.doInBackground = doInBackground;
240  }
241 
242  @Override
243  protected Void doInBackground() throws Exception {
244  doInBackground.run();
245  return null;
246  }
247 
248  @Override
249  protected void done() {
250  try {
251  get();
252  } catch (InterruptedException | ExecutionException ex) {
253  panel.getProgressPanel().updateStatusLabel(NbBundle.getMessage(this.getClass(), "ReportGenerator.errors.reportErrorText") + ex.getLocalizedMessage());
254  logger.log(Level.SEVERE, "failed to generate reports", ex); //NON-NLS
255  } // catch and ignore if we were cancelled
256  catch (java.util.concurrent.CancellationException ex) {
257  }
258  }
259  }
260 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static void addEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:491
void setSelectedDataSources(List< Long > selectedDataSources)

Copyright © 2012-2021 Basis Technology. Generated on: Tue Jan 19 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.