Autopsy  4.12.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddContentToHashDbAction.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.modules.hashdatabase;
20 
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.logging.Level;
26 import javax.swing.AbstractAction;
27 import javax.swing.JMenu;
28 import javax.swing.JMenuItem;
29 import javax.swing.JOptionPane;
30 import org.apache.commons.lang3.StringUtils;
31 import org.openide.util.NbBundle;
32 import org.openide.util.Utilities;
33 import org.openide.util.actions.Presenter;
34 import org.openide.windows.WindowManager;
38 import org.sleuthkit.datamodel.AbstractFile;
39 import org.sleuthkit.datamodel.HashUtility;
40 import org.sleuthkit.datamodel.TskCoreException;
41 
45 final class AddContentToHashDbAction extends AbstractAction implements Presenter.Popup {
46 
47  private static AddContentToHashDbAction instance;
48 
49  private final static String SINGLE_SELECTION_NAME = NbBundle.getMessage(AddContentToHashDbAction.class,
50  "AddContentToHashDbAction.singleSelectionName");
51  private final static String MULTI_SELECTION_NAME = NbBundle.getMessage(AddContentToHashDbAction.class,
52  "AddContentToHashDbAction.multipleSelectionName");
53 
54  //During ingest display strings. This text will be greyed out and unclickable
55  private final static String SINGLE_SELECTION_NAME_DURING_INGEST = NbBundle.getMessage(AddContentToHashDbAction.class,
56  "AddContentToHashDbAction.singleSelectionNameDuringIngest");
57  private final static String MULTI_SELECTION_NAME_DURING_INGEST = NbBundle.getMessage(AddContentToHashDbAction.class,
58  "AddContentToHashDbAction.multipleSelectionNameDuringIngest");
59 
60  //No MD5 Hash and Empty File display strings. This text will be greyed out and unclickable
61  private final static String SINGLE_SELECTION_NAME_EMPTY_FILE = NbBundle.getMessage(AddContentToHashDbAction.class,
62  "AddContentToHashDbAction.singleSelectionNameEmpty");
63  private final static String MULTI_SELECTION_NAME_EMPTY_FILE = NbBundle.getMessage(AddContentToHashDbAction.class,
64  "AddContentToHashDbAction.multipleSelectionNameEmpty");
65  private final static String SINGLE_SELECTION_NAME_NO_MD5 = NbBundle.getMessage(AddContentToHashDbAction.class,
66  "AddContentToHashDbAction.singleSelectionNameNoMD5");
67  private final static String MULTI_SELECTION_NAME_NO_MD5 = NbBundle.getMessage(AddContentToHashDbAction.class,
68  "AddContentToHashDbAction.multipleSelectionNameNoMD5");
69 
76  public static synchronized AddContentToHashDbAction getInstance() {
77  if (null == instance) {
78  instance = new AddContentToHashDbAction();
79  }
80  return instance;
81  }
82 
83  private AddContentToHashDbAction() {
84  }
85 
86  @Override
87  public JMenuItem getPopupPresenter() {
88  return new AddContentToHashDbMenu();
89  }
90 
91  @Override
92  public void actionPerformed(ActionEvent event) {
93  }
94 
95  // Instances of this class are used to implement the a pop up menu for this
96  // action.
97  private final class AddContentToHashDbMenu extends JMenu {
98 
100  super(SINGLE_SELECTION_NAME);
101  // Get any AbstractFile objects from the lookup of the currently focused top component.
102  final Collection<? extends AbstractFile> selectedFiles = Utilities.actionsGlobalContext().lookupAll(AbstractFile.class);
103  int numberOfFilesSelected = selectedFiles.size();
104 
105  // Disable the menu if file ingest is in progress.
107  setEnabled(false);
108  setTextBasedOnNumberOfSelections(numberOfFilesSelected,
109  SINGLE_SELECTION_NAME_DURING_INGEST,
110  MULTI_SELECTION_NAME_DURING_INGEST);
111  return;
112  }
113 
114  if (selectedFiles.isEmpty()) {
115  setEnabled(false);
116  return;
117  } else {
118  setTextBasedOnNumberOfSelections(numberOfFilesSelected,
119  SINGLE_SELECTION_NAME,
120  MULTI_SELECTION_NAME);
121  }
122 
123  // Disable the menu if md5 have not been computed or if the file size
124  // is empty. Display the appropriate reason to the user.
125  for (AbstractFile file : selectedFiles) {
126  if (file.getSize() == 0) {
127  setEnabled(false);
128  setTextBasedOnNumberOfSelections(numberOfFilesSelected,
129  SINGLE_SELECTION_NAME_EMPTY_FILE,
130  MULTI_SELECTION_NAME_EMPTY_FILE);
131  return;
132  } else if (null == file.getMd5Hash() || StringUtils.isBlank(file.getMd5Hash())) {
133  setEnabled(false);
134  setTextBasedOnNumberOfSelections(numberOfFilesSelected,
135  SINGLE_SELECTION_NAME_NO_MD5,
136  MULTI_SELECTION_NAME_NO_MD5);
137  return;
138  }
139  }
140 
141  // Get the current set of updateable hash databases and add each
142  // one to the menu as a separate menu item. Selecting a hash database
143  // adds the selected files to the selected database.
144  final List<HashDb> hashDatabases = HashDbManager.getInstance().getUpdateableHashSets();
145  if (!hashDatabases.isEmpty()) {
146  for (final HashDb database : hashDatabases) {
147  JMenuItem databaseItem = add(database.getHashSetName());
148  databaseItem.addActionListener(new ActionListener() {
149  @Override
150  public void actionPerformed(ActionEvent e) {
151  addFilesToHashSet(selectedFiles, database);
152  }
153  });
154  }
155  } else {
156  JMenuItem empty = new JMenuItem(
157  NbBundle.getMessage(this.getClass(),
158  "AddContentToHashDbAction.ContentMenu.noHashDbsConfigd"));
159  empty.setEnabled(false);
160  add(empty);
161  }
162 
163  // Add a "New Hash Set..." menu item. Selecting this item invokes a
164  // a hash database creation dialog and adds the selected files to the
165  // the new database.
166  addSeparator();
167  JMenuItem newHashSetItem = new JMenuItem(NbBundle.getMessage(this.getClass(),
168  "AddContentToHashDbAction.ContentMenu.createDbItem"));
169  newHashSetItem.addActionListener(new ActionListener() {
170  @Override
171  public void actionPerformed(ActionEvent e) {
172  HashDb hashDb = new HashDbCreateDatabaseDialog().getHashDatabase();
173  if (null != hashDb) {
174  addFilesToHashSet(selectedFiles, hashDb);
175  }
176  }
177  });
178  add(newHashSetItem);
179  }
180 
189  private void setTextBasedOnNumberOfSelections(int numberOfFilesSelected,
190  String singleSelection, String multiSelection) {
191  if (numberOfFilesSelected > 1) {
192  setText(multiSelection);
193  } else {
194  setText(singleSelection);
195  }
196  }
197 
198  private void addFilesToHashSet(final Collection<? extends AbstractFile> files, HashDb hashSet) {
199  for (AbstractFile file : files) {
200  String md5Hash = file.getMd5Hash();
201  if (null != md5Hash) {
202  // don't let them add the hash for an empty file to the DB
203  if (HashUtility.isNoDataMd5(md5Hash)) { //NON-NLS
204  Logger.getLogger(AddContentToHashDbAction.class.getName()).log(Level.INFO, "Not adding " + file.getName() + " to hash set (empty content)"); //NON-NLS
205  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
206  NbBundle.getMessage(this.getClass(),
207  "AddContentToHashDbAction.addFilesToHashSet.unableToAddFileEmptyMsg",
208  file.getName()),
209  NbBundle.getMessage(this.getClass(),
210  "AddContentToHashDbAction.addFilesToHashSet.addToHashDbErr1.text"),
211  JOptionPane.ERROR_MESSAGE);
212  continue;
213  }
214  try {
215  hashSet.addHashes(file);
216  } catch (TskCoreException ex) {
217  Logger.getLogger(AddContentToHashDbAction.class.getName()).log(Level.SEVERE, "Error adding to hash set", ex); //NON-NLS
218  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
219  NbBundle.getMessage(this.getClass(),
220  "AddContentToHashDbAction.addFilesToHashSet.unableToAddFileMsg",
221  file.getName()),
222  NbBundle.getMessage(this.getClass(),
223  "AddContentToHashDbAction.addFilesToHashSet.addToHashDbErr2.text"),
224  JOptionPane.ERROR_MESSAGE);
225  }
226  } else {
227  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
228  NbBundle.getMessage(this.getClass(),
229  "AddContentToHashDbAction.addFilesToHashSet.unableToAddFileSzMsg",
230  files.size() > 1 ? NbBundle
231  .getMessage(this.getClass(),
232  "AddContentToHashDbAction.addFilesToHashSet.files") : NbBundle
233  .getMessage(this.getClass(),
234  "AddContentToHashDbAction.addFilesToHashSet.file")),
235  NbBundle.getMessage(this.getClass(),
236  "AddContentToHashDbAction.addFilesToHashSet.addToHashDbErr3.text"),
237  JOptionPane.ERROR_MESSAGE);
238  break;
239  }
240  }
241  }
242  }
243 }
static synchronized IngestManager getInstance()
void setTextBasedOnNumberOfSelections(int numberOfFilesSelected, String singleSelection, String multiSelection)
void addFilesToHashSet(final Collection<?extends AbstractFile > files, HashDb hashSet)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2018 Basis Technology. Generated on: Wed Sep 18 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.