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

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