Autopsy  4.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 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.openide.util.NbBundle;
31 import org.openide.util.Utilities;
32 import org.openide.util.actions.Presenter;
37 import org.sleuthkit.datamodel.AbstractFile;
38 import org.sleuthkit.datamodel.HashUtility;
39 import org.sleuthkit.datamodel.TskCoreException;
40 
44 final class AddContentToHashDbAction extends AbstractAction implements Presenter.Popup {
45 
46  private static AddContentToHashDbAction instance;
47  private final static String SINGLE_SELECTION_NAME = NbBundle.getMessage(AddContentToHashDbAction.class,
48  "AddContentToHashDbAction.singleSelectionName");
49  private final static String MULTIPLE_SELECTION_NAME = NbBundle.getMessage(AddContentToHashDbAction.class,
50  "AddContentToHashDbAction.multipleSelectionName");
51 
58  public static synchronized AddContentToHashDbAction getInstance() {
59  if (null == instance) {
60  instance = new AddContentToHashDbAction();
61  }
62  return instance;
63  }
64 
65  private AddContentToHashDbAction() {
66  }
67 
68  @Override
69  public JMenuItem getPopupPresenter() {
70  return new AddContentToHashDbMenu();
71  }
72 
73  @Override
74  public void actionPerformed(ActionEvent event) {
75  }
76 
77  // Instances of this class are used to implement the a pop up menu for this
78  // action.
79  private final class AddContentToHashDbMenu extends JMenu {
80 
82  super(SINGLE_SELECTION_NAME);
83 
84  // Disable the menu if file ingest is in progress.
86  setEnabled(false);
87  return;
88  }
89 
90  // Get any AbstractFile objects from the lookup of the currently focused top component.
91  final Collection<? extends AbstractFile> selectedFiles = Utilities.actionsGlobalContext().lookupAll(AbstractFile.class);
92  if (selectedFiles.isEmpty()) {
93  setEnabled(false);
94  return;
95  } else if (selectedFiles.size() > 1) {
96  setText(MULTIPLE_SELECTION_NAME);
97  }
98 
99  // Disable the menu if hashes have not been calculated.
100  for (AbstractFile file : selectedFiles) {
101  if (null == file.getMd5Hash()) {
102  setEnabled(false);
103  return;
104  }
105  }
106 
107  // Get the current set of updateable hash databases and add each
108  // one to the menu as a separate menu item. Selecting a hash database
109  // adds the selected files to the selected database.
110  final List<HashDb> hashDatabases = HashDbManager.getInstance().getUpdateableHashSets();
111  if (!hashDatabases.isEmpty()) {
112  for (final HashDb database : hashDatabases) {
113  JMenuItem databaseItem = add(database.getHashSetName());
114  databaseItem.addActionListener(new ActionListener() {
115  @Override
116  public void actionPerformed(ActionEvent e) {
117  addFilesToHashSet(selectedFiles, database);
118  }
119  });
120  }
121  } else {
122  JMenuItem empty = new JMenuItem(
123  NbBundle.getMessage(this.getClass(),
124  "AddContentToHashDbAction.ContentMenu.noHashDbsConfigd"));
125  empty.setEnabled(false);
126  add(empty);
127  }
128 
129  // Add a "New Hash Set..." menu item. Selecting this item invokes a
130  // a hash database creation dialog and adds the selected files to the
131  // the new database.
132  addSeparator();
133  JMenuItem newHashSetItem = new JMenuItem(NbBundle.getMessage(this.getClass(),
134  "AddContentToHashDbAction.ContentMenu.createDbItem"));
135  newHashSetItem.addActionListener(new ActionListener() {
136  @Override
137  public void actionPerformed(ActionEvent e) {
138  HashDb hashDb = new HashDbCreateDatabaseDialog().getHashDatabase();
139  if (null != hashDb) {
140  HashDbManager.getInstance().save();
141  addFilesToHashSet(selectedFiles, hashDb);
142  }
143  }
144  });
145  add(newHashSetItem);
146  }
147 
148  private void addFilesToHashSet(final Collection<? extends AbstractFile> files, HashDb hashSet) {
149  for (AbstractFile file : files) {
150  String md5Hash = file.getMd5Hash();
151  if (null != md5Hash) {
152  // don't let them add the hash for an empty file to the DB
153  if (HashUtility.isNoDataMd5(md5Hash)) { //NON-NLS
154  Logger.getLogger(AddContentToHashDbAction.class.getName()).log(Level.INFO, "Not adding " + file.getName() + " to database (empty content)"); //NON-NLS
155  JOptionPane.showMessageDialog(null,
156  NbBundle.getMessage(this.getClass(),
157  "AddContentToHashDbAction.addFilesToHashSet.unableToAddFileEmptyMsg",
158  file.getName()),
159  NbBundle.getMessage(this.getClass(),
160  "AddContentToHashDbAction.addFilesToHashSet.addToHashDbErr1.text"),
161  JOptionPane.ERROR_MESSAGE);
162  continue;
163  }
164  try {
165  hashSet.addHashes(file);
166  } catch (TskCoreException ex) {
167  Logger.getLogger(AddContentToHashDbAction.class.getName()).log(Level.SEVERE, "Error adding to hash database", ex); //NON-NLS
168  JOptionPane.showMessageDialog(null,
169  NbBundle.getMessage(this.getClass(),
170  "AddContentToHashDbAction.addFilesToHashSet.unableToAddFileMsg",
171  file.getName()),
172  NbBundle.getMessage(this.getClass(),
173  "AddContentToHashDbAction.addFilesToHashSet.addToHashDbErr2.text"),
174  JOptionPane.ERROR_MESSAGE);
175  }
176  } else {
177  JOptionPane.showMessageDialog(null,
178  NbBundle.getMessage(this.getClass(),
179  "AddContentToHashDbAction.addFilesToHashSet.unableToAddFileSzMsg",
180  files.size() > 1 ? NbBundle
181  .getMessage(this.getClass(),
182  "AddContentToHashDbAction.addFilesToHashSet.files") : NbBundle
183  .getMessage(this.getClass(),
184  "AddContentToHashDbAction.addFilesToHashSet.file")),
185  NbBundle.getMessage(this.getClass(),
186  "AddContentToHashDbAction.addFilesToHashSet.addToHashDbErr3.text"),
187  JOptionPane.ERROR_MESSAGE);
188  break;
189  }
190  }
191  }
192  }
193 }
static synchronized IngestManager getInstance()
void addFilesToHashSet(final Collection<?extends AbstractFile > files, HashDb hashSet)
synchronized static Logger getLogger(String name)
Definition: Logger.java:166

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.