Autopsy  4.16.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-2019 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.HashSet;
25 import java.util.List;
26 import java.util.logging.Level;
27 import javax.swing.AbstractAction;
28 import javax.swing.JMenu;
29 import javax.swing.JMenuItem;
30 import javax.swing.JOptionPane;
31 import org.apache.commons.lang3.StringUtils;
32 import org.openide.util.NbBundle;
33 import org.openide.util.Utilities;
34 import org.openide.util.actions.Presenter;
35 import org.openide.windows.WindowManager;
39 import org.sleuthkit.datamodel.AbstractFile;
40 import org.sleuthkit.datamodel.HashUtility;
41 import org.sleuthkit.datamodel.TskCoreException;
42 
46 public final class AddContentToHashDbAction extends AbstractAction implements Presenter.Popup {
47 
49 
50  private final static String SINGLE_SELECTION_NAME = NbBundle.getMessage(AddContentToHashDbAction.class,
51  "AddContentToHashDbAction.singleSelectionName");
52  private final static String MULTI_SELECTION_NAME = NbBundle.getMessage(AddContentToHashDbAction.class,
53  "AddContentToHashDbAction.multipleSelectionName");
54 
55  //During ingest display strings. This text will be greyed out and unclickable
56  private final static String SINGLE_SELECTION_NAME_DURING_INGEST = NbBundle.getMessage(AddContentToHashDbAction.class,
57  "AddContentToHashDbAction.singleSelectionNameDuringIngest");
58  private final static String MULTI_SELECTION_NAME_DURING_INGEST = NbBundle.getMessage(AddContentToHashDbAction.class,
59  "AddContentToHashDbAction.multipleSelectionNameDuringIngest");
60 
61  //No MD5 Hash and Empty File display strings. This text will be greyed out and unclickable
62  private final static String SINGLE_SELECTION_NAME_EMPTY_FILE = NbBundle.getMessage(AddContentToHashDbAction.class,
63  "AddContentToHashDbAction.singleSelectionNameEmpty");
64  private final static String MULTI_SELECTION_NAME_EMPTY_FILE = NbBundle.getMessage(AddContentToHashDbAction.class,
65  "AddContentToHashDbAction.multipleSelectionNameEmpty");
66  private final static String SINGLE_SELECTION_NAME_NO_MD5 = NbBundle.getMessage(AddContentToHashDbAction.class,
67  "AddContentToHashDbAction.singleSelectionNameNoMD5");
68  private final static String MULTI_SELECTION_NAME_NO_MD5 = NbBundle.getMessage(AddContentToHashDbAction.class,
69  "AddContentToHashDbAction.multipleSelectionNameNoMD5");
70  private static final long serialVersionUID = 1L;
71 
81  public static synchronized AddContentToHashDbAction getInstance() {
82  if (null == instance) {
83  instance = new AddContentToHashDbAction();
84  }
85  return instance;
86  }
87 
89  }
90 
100  public JMenuItem getMenuForFiles(Collection<AbstractFile> selectedFiles) {
101  return new AddContentToHashDbMenu(selectedFiles);
102  }
103 
104  @Override
105  public JMenuItem getPopupPresenter() {
106  return new AddContentToHashDbMenu();
107  }
108 
109  @Override
110  public void actionPerformed(ActionEvent event) {
111  }
112 
113  // Instances of this class are used to implement the a pop up menu for this
114  // action.
115  private final class AddContentToHashDbMenu extends JMenu {
116 
117  private static final long serialVersionUID = 1L;
118 
123  AddContentToHashDbMenu(Collection<AbstractFile> selectedFiles) {
124  super(SINGLE_SELECTION_NAME);
125  int numberOfFilesSelected = selectedFiles.size();
126 
127  // Disable the menu if file ingest is in progress.
129  setEnabled(false);
130  setTextBasedOnNumberOfSelections(numberOfFilesSelected,
131  SINGLE_SELECTION_NAME_DURING_INGEST,
132  MULTI_SELECTION_NAME_DURING_INGEST);
133  return;
134  } else if (numberOfFilesSelected == 0) {
135  setEnabled(false);
136  return;
137  }
138  setTextBasedOnNumberOfSelections(numberOfFilesSelected,
139  SINGLE_SELECTION_NAME,
140  MULTI_SELECTION_NAME);
141 
142  // Disable the menu if md5 have not been computed or if the file size
143  // is empty. Display the appropriate reason to the user.
144  for (AbstractFile file : selectedFiles) {
145  if (file.getSize() == 0) {
146  setEnabled(false);
147  setTextBasedOnNumberOfSelections(numberOfFilesSelected,
148  SINGLE_SELECTION_NAME_EMPTY_FILE,
149  MULTI_SELECTION_NAME_EMPTY_FILE);
150  return;
151  } else if (null == file.getMd5Hash() || StringUtils.isBlank(file.getMd5Hash())) {
152  setEnabled(false);
153  setTextBasedOnNumberOfSelections(numberOfFilesSelected,
154  SINGLE_SELECTION_NAME_NO_MD5,
155  MULTI_SELECTION_NAME_NO_MD5);
156  return;
157  }
158  }
159  addExistingHashDatabases(selectedFiles);
160  // Add a "New Hash Set..." menu item. Selecting this item invokes a
161  // a hash database creation dialog and adds the selected files to the
162  // the new database.
163  addSeparator();
164  JMenuItem newHashSetItem = new JMenuItem(NbBundle.getMessage(this.getClass(),
165  "AddContentToHashDbAction.ContentMenu.createDbItem"));
166  newHashSetItem.addActionListener(new ActionListener() {
167  @Override
168  public void actionPerformed(ActionEvent e) {
169  HashDb hashDb = new HashDbCreateDatabaseDialog().getHashDatabase();
170  if (null != hashDb) {
171  addFilesToHashSet(selectedFiles, hashDb);
172  }
173  }
174  });
175  add(newHashSetItem);
176  }
177 
178  private void addExistingHashDatabases(Collection<AbstractFile> selectedFiles) {
179  // Get the current set of updateable hash databases and add each
180  // one to the menu as a separate menu item. Selecting a hash database
181  // adds the selected files to the selected database.
182  final List<HashDb> hashDatabases = HashDbManager.getInstance().getUpdateableHashSets();
183  if (!hashDatabases.isEmpty()) {
184  for (final HashDb database : hashDatabases) {
185  JMenuItem databaseItem = add(database.getHashSetName());
186  databaseItem.addActionListener(new ActionListener() {
187  @Override
188  public void actionPerformed(ActionEvent e) {
189  addFilesToHashSet(selectedFiles, database);
190  }
191  });
192  }
193  } else {
194  JMenuItem empty = new JMenuItem(
195  NbBundle.getMessage(this.getClass(),
196  "AddContentToHashDbAction.ContentMenu.noHashDbsConfigd"));
197  empty.setEnabled(false);
198  add(empty);
199  }
200  }
201 
207  // Get any AbstractFile objects from the lookup of the currently focused top component.
208  this(new HashSet<>(Utilities.actionsGlobalContext().lookupAll(AbstractFile.class)));
209 
210  }
211 
220  private void setTextBasedOnNumberOfSelections(int numberOfFilesSelected,
221  String singleSelection, String multiSelection) {
222  if (numberOfFilesSelected > 1) {
223  setText(multiSelection);
224  } else {
225  setText(singleSelection);
226  }
227  }
228 
229  private void addFilesToHashSet(final Collection<? extends AbstractFile> files, HashDb hashSet) {
230  for (AbstractFile file : files) {
231  String md5Hash = file.getMd5Hash();
232  if (null != md5Hash) {
233  // don't let them add the hash for an empty file to the DB
234  if (HashUtility.isNoDataMd5(md5Hash)) { //NON-NLS
235  Logger.getLogger(AddContentToHashDbAction.class.getName()).log(Level.INFO, "Not adding " + file.getName() + " to hash set (empty content)"); //NON-NLS
236  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
237  NbBundle.getMessage(this.getClass(),
238  "AddContentToHashDbAction.addFilesToHashSet.unableToAddFileEmptyMsg",
239  file.getName()),
240  NbBundle.getMessage(this.getClass(),
241  "AddContentToHashDbAction.addFilesToHashSet.addToHashDbErr1.text"),
242  JOptionPane.ERROR_MESSAGE);
243  continue;
244  }
245  try {
246  hashSet.addHashes(file);
247  } catch (TskCoreException ex) {
248  Logger.getLogger(AddContentToHashDbAction.class.getName()).log(Level.SEVERE, "Error adding to hash set", ex); //NON-NLS
249  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
250  NbBundle.getMessage(this.getClass(),
251  "AddContentToHashDbAction.addFilesToHashSet.unableToAddFileMsg",
252  file.getName()),
253  NbBundle.getMessage(this.getClass(),
254  "AddContentToHashDbAction.addFilesToHashSet.addToHashDbErr2.text"),
255  JOptionPane.ERROR_MESSAGE);
256  }
257  } else {
258  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
259  NbBundle.getMessage(this.getClass(),
260  "AddContentToHashDbAction.addFilesToHashSet.unableToAddFileSzMsg",
261  files.size() > 1 ? NbBundle
262  .getMessage(this.getClass(),
263  "AddContentToHashDbAction.addFilesToHashSet.files") : NbBundle
264  .getMessage(this.getClass(),
265  "AddContentToHashDbAction.addFilesToHashSet.file")),
266  NbBundle.getMessage(this.getClass(),
267  "AddContentToHashDbAction.addFilesToHashSet.addToHashDbErr3.text"),
268  JOptionPane.ERROR_MESSAGE);
269  break;
270  }
271  }
272  }
273  }
274 }
static synchronized IngestManager getInstance()
void setTextBasedOnNumberOfSelections(int numberOfFilesSelected, String singleSelection, String multiSelection)
void addFilesToHashSet(final Collection<?extends AbstractFile > files, HashDb hashSet)
JMenuItem getMenuForFiles(Collection< AbstractFile > selectedFiles)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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