Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
HashDbImportDatabaseDialog.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 - 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.Dimension;
22 import java.awt.Toolkit;
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.file.Paths;
26 import java.util.logging.Level;
27 import javax.swing.JFileChooser;
28 import javax.swing.JFrame;
29 import javax.swing.JOptionPane;
30 import javax.swing.filechooser.FileNameExtensionFilter;
31 import org.apache.commons.io.FilenameUtils;
32 import org.openide.util.NbBundle;
33 import org.openide.windows.WindowManager;
40 
46 final class HashDbImportDatabaseDialog extends javax.swing.JDialog {
47 
48  private JFileChooser fileChooser = new JFileChooser();
49  private String selectedFilePath = "";
50  private HashDb selectedHashDb = null;
51  private final static String LAST_FILE_PATH_KEY = "HashDbImport_Path";
52 
58  HashDbImportDatabaseDialog() {
59  super((JFrame) WindowManager.getDefault().getMainWindow(),
60  NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.importHashDbMsg"),
61  true);
62  initFileChooser();
63  initComponents();
64  display();
65  }
66 
72  HashDb getHashDatabase() {
73  return selectedHashDb;
74  }
75 
76  private void initFileChooser() {
77  fileChooser.setDragEnabled(false);
78  fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
79  String[] EXTENSION = new String[]{"txt", "kdb", "idx", "hash", "Hash", "hsh"}; //NON-NLS
80  FileNameExtensionFilter filter = new FileNameExtensionFilter(
81  NbBundle.getMessage(this.getClass(), "HashDbImportDatabaseDialog.fileNameExtFilter.text"), EXTENSION);
82  fileChooser.setFileFilter(filter);
83  fileChooser.setMultiSelectionEnabled(false);
84  }
85 
86  private void display() {
87  Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
88  setLocation((screenDimension.width - getSize().width) / 2, (screenDimension.height - getSize().height) / 2);
89  setVisible(true);
90  }
91 
92  private static String shortenPath(String path) {
93  String shortenedPath = path;
94  if (shortenedPath.length() > 50) {
95  shortenedPath = shortenedPath.substring(0, 10 + shortenedPath.substring(10).indexOf(File.separator) + 1) + "..." + shortenedPath.substring((shortenedPath.length() - 20) + shortenedPath.substring(shortenedPath.length() - 20).indexOf(File.separator));
96  }
97  return shortenedPath;
98  }
99 
105  @SuppressWarnings("unchecked")
106  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
107  private void initComponents() {
108 
109  buttonGroup1 = new javax.swing.ButtonGroup();
110  okButton = new javax.swing.JButton();
111  cancelButton = new javax.swing.JButton();
112  databasePathTextField = new javax.swing.JTextField();
113  openButton = new javax.swing.JButton();
114  knownRadioButton = new javax.swing.JRadioButton();
115  knownBadRadioButton = new javax.swing.JRadioButton();
116  jLabel1 = new javax.swing.JLabel();
117  hashSetNameTextField = new javax.swing.JTextField();
118  jLabel2 = new javax.swing.JLabel();
119  sendIngestMessagesCheckbox = new javax.swing.JCheckBox();
120  jLabel3 = new javax.swing.JLabel();
121 
122  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
123 
124  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.okButton.text")); // NOI18N
125  okButton.addActionListener(new java.awt.event.ActionListener() {
126  public void actionPerformed(java.awt.event.ActionEvent evt) {
127  okButtonActionPerformed(evt);
128  }
129  });
130 
131  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.cancelButton.text")); // NOI18N
132  cancelButton.addActionListener(new java.awt.event.ActionListener() {
133  public void actionPerformed(java.awt.event.ActionEvent evt) {
134  cancelButtonActionPerformed(evt);
135  }
136  });
137 
138  databasePathTextField.setEditable(false);
139  databasePathTextField.setText(org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.databasePathTextField.text")); // NOI18N
140 
141  org.openide.awt.Mnemonics.setLocalizedText(openButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.openButton.text")); // NOI18N
142  openButton.addActionListener(new java.awt.event.ActionListener() {
143  public void actionPerformed(java.awt.event.ActionEvent evt) {
144  openButtonActionPerformed(evt);
145  }
146  });
147 
148  buttonGroup1.add(knownRadioButton);
149  org.openide.awt.Mnemonics.setLocalizedText(knownRadioButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.knownRadioButton.text")); // NOI18N
150  knownRadioButton.addActionListener(new java.awt.event.ActionListener() {
151  public void actionPerformed(java.awt.event.ActionEvent evt) {
152  knownRadioButtonActionPerformed(evt);
153  }
154  });
155 
156  buttonGroup1.add(knownBadRadioButton);
157  knownBadRadioButton.setSelected(true);
158  org.openide.awt.Mnemonics.setLocalizedText(knownBadRadioButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.knownBadRadioButton.text")); // NOI18N
159  knownBadRadioButton.addActionListener(new java.awt.event.ActionListener() {
160  public void actionPerformed(java.awt.event.ActionEvent evt) {
161  knownBadRadioButtonActionPerformed(evt);
162  }
163  });
164 
165  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.jLabel1.text")); // NOI18N
166 
167  hashSetNameTextField.setText(org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.hashSetNameTextField.text")); // NOI18N
168 
169  org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.jLabel2.text")); // NOI18N
170 
171  sendIngestMessagesCheckbox.setSelected(true);
172  org.openide.awt.Mnemonics.setLocalizedText(sendIngestMessagesCheckbox, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.sendIngestMessagesCheckbox.text")); // NOI18N
173  sendIngestMessagesCheckbox.addActionListener(new java.awt.event.ActionListener() {
174  public void actionPerformed(java.awt.event.ActionEvent evt) {
175  sendIngestMessagesCheckboxActionPerformed(evt);
176  }
177  });
178 
179  org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.jLabel3.text")); // NOI18N
180 
181  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
182  getContentPane().setLayout(layout);
183  layout.setHorizontalGroup(
184  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
185  .addGroup(layout.createSequentialGroup()
186  .addContainerGap()
187  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
188  .addGroup(layout.createSequentialGroup()
189  .addGap(0, 0, Short.MAX_VALUE)
190  .addComponent(okButton)
191  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
192  .addComponent(cancelButton))
193  .addGroup(layout.createSequentialGroup()
194  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
195  .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
196  .addComponent(jLabel1)
197  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
198  .addComponent(hashSetNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE))
199  .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
200  .addComponent(jLabel3)
201  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
202  .addComponent(databasePathTextField)))
203  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
204  .addComponent(openButton))
205  .addGroup(layout.createSequentialGroup()
206  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
207  .addComponent(jLabel2)
208  .addGroup(layout.createSequentialGroup()
209  .addGap(19, 19, 19)
210  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
211  .addComponent(knownRadioButton)
212  .addComponent(knownBadRadioButton)))
213  .addComponent(sendIngestMessagesCheckbox))
214  .addGap(0, 0, Short.MAX_VALUE)))
215  .addContainerGap())
216  );
217 
218  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});
219 
220  layout.setVerticalGroup(
221  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
222  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
223  .addContainerGap()
224  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
225  .addComponent(openButton)
226  .addComponent(databasePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
227  .addComponent(jLabel3))
228  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
229  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
230  .addComponent(jLabel1)
231  .addComponent(hashSetNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
232  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
233  .addGroup(layout.createSequentialGroup()
234  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 119, Short.MAX_VALUE)
235  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
236  .addComponent(okButton)
237  .addComponent(cancelButton))
238  .addContainerGap())
239  .addGroup(layout.createSequentialGroup()
240  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
241  .addComponent(jLabel2)
242  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
243  .addComponent(knownRadioButton)
244  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
245  .addComponent(knownBadRadioButton)
246  .addGap(18, 18, 18)
247  .addComponent(sendIngestMessagesCheckbox)
248  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
249  );
250 
251  pack();
252  }// </editor-fold>//GEN-END:initComponents
253 
254  private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
255  String lastBaseDirectory = Paths.get(PlatformUtil.getUserConfigDirectory(), "HashDatabases").toString();
256  if (ModuleSettings.settingExists(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY)) {
257  lastBaseDirectory = ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY);
258  }
259  File hashDbFolder = new File(lastBaseDirectory);
260  // create the folder if it doesn't exist
261  if (!hashDbFolder.exists()) {
262  hashDbFolder.mkdir();
263  }
264  fileChooser.setCurrentDirectory(hashDbFolder);
265  if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
266  File databaseFile = fileChooser.getSelectedFile();
267  try {
268  selectedFilePath = databaseFile.getCanonicalPath();
269  databasePathTextField.setText(shortenPath(selectedFilePath));
270  hashSetNameTextField.setText(FilenameUtils.removeExtension(databaseFile.getName()));
271  if (hashSetNameTextField.getText().toLowerCase().contains("nsrl")) { //NON-NLS
272  knownRadioButton.setSelected(true);
273  knownRadioButtonActionPerformed(null);
274  }
275  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY, databaseFile.getParent());
276  } catch (IOException ex) {
277  Logger.getLogger(HashDbImportDatabaseDialog.class.getName()).log(Level.SEVERE, "Failed to get path of selected database", ex); //NON-NLS
278  JOptionPane.showMessageDialog(this,
279  NbBundle.getMessage(this.getClass(),
280  "HashDbImportDatabaseDialog.failedToGetDbPathMsg"));
281  }
282  }
283  }//GEN-LAST:event_openButtonActionPerformed
284 
285  private void knownRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_knownRadioButtonActionPerformed
286  sendIngestMessagesCheckbox.setSelected(false);
287  sendIngestMessagesCheckbox.setEnabled(false);
288  }//GEN-LAST:event_knownRadioButtonActionPerformed
289 
290  private void knownBadRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_knownBadRadioButtonActionPerformed
291  sendIngestMessagesCheckbox.setSelected(true);
292  sendIngestMessagesCheckbox.setEnabled(true);
293  }//GEN-LAST:event_knownBadRadioButtonActionPerformed
294 
295  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
296  this.dispose();
297  }//GEN-LAST:event_cancelButtonActionPerformed
298 
299  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
300  // Note that the error handlers in this method call return without disposing of the
301  // dialog to allow the user to try again, if desired.
302 
303  if (hashSetNameTextField.getText().isEmpty()) {
304  JOptionPane.showMessageDialog(this,
305  NbBundle.getMessage(this.getClass(),
306  "HashDbCreateDatabaseDialog.mustEnterHashSetNameMsg"),
307  NbBundle.getMessage(this.getClass(),
308  "HashDbImportDatabaseDialog.importHashDbErr"),
309  JOptionPane.ERROR_MESSAGE);
310  return;
311  }
312 
313  if (selectedFilePath.isEmpty()) {
314  JOptionPane.showMessageDialog(this,
315  NbBundle.getMessage(this.getClass(),
316  "HashDbImportDatabaseDialog.mustSelectHashDbFilePathMsg"),
317  NbBundle.getMessage(this.getClass(),
318  "HashDbImportDatabaseDialog.importHashDbErr"),
319  JOptionPane.ERROR_MESSAGE);
320  return;
321  }
322  File file = new File(selectedFilePath);
323  if (!file.exists()) {
324  JOptionPane.showMessageDialog(this,
325  NbBundle.getMessage(this.getClass(),
326  "HashDbImportDatabaseDialog.hashDbDoesNotExistMsg"),
327  NbBundle.getMessage(this.getClass(),
328  "HashDbImportDatabaseDialog.importHashDbErr"),
329  JOptionPane.ERROR_MESSAGE);
330  return;
331  }
332 
333  KnownFilesType type;
334  if (knownRadioButton.isSelected()) {
335  type = KnownFilesType.KNOWN;
336  } else {
337  type = KnownFilesType.KNOWN_BAD;
338  }
339 
340  String errorMessage = NbBundle.getMessage(this.getClass(),
341  "HashDbImportDatabaseDialog.errorMessage.failedToOpenHashDbMsg",
342  selectedFilePath);
343  try {
344  selectedHashDb = HashDbManager.getInstance().addExistingHashDatabaseNoSave(hashSetNameTextField.getText(), selectedFilePath, true, sendIngestMessagesCheckbox.isSelected(), type);
345  } catch (HashDbManagerException ex) {
346  Logger.getLogger(HashDbImportDatabaseDialog.class.getName()).log(Level.WARNING, errorMessage, ex);
347  JOptionPane.showMessageDialog(this,
348  ex.getMessage(),
349  NbBundle.getMessage(this.getClass(),
350  "HashDbImportDatabaseDialog.importHashDbErr"),
351  JOptionPane.ERROR_MESSAGE);
352  return;
353  }
354 
355  dispose();
356  }//GEN-LAST:event_okButtonActionPerformed
357 
358  private void sendIngestMessagesCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendIngestMessagesCheckboxActionPerformed
359  // TODO add your handling code here:
360  }//GEN-LAST:event_sendIngestMessagesCheckboxActionPerformed
361 
362  // Variables declaration - do not modify//GEN-BEGIN:variables
363  private javax.swing.ButtonGroup buttonGroup1;
364  private javax.swing.JButton cancelButton;
365  private javax.swing.JTextField databasePathTextField;
366  private javax.swing.JTextField hashSetNameTextField;
367  private javax.swing.JLabel jLabel1;
368  private javax.swing.JLabel jLabel2;
369  private javax.swing.JLabel jLabel3;
370  private javax.swing.JRadioButton knownBadRadioButton;
371  private javax.swing.JRadioButton knownRadioButton;
372  private javax.swing.JButton okButton;
373  private javax.swing.JButton openButton;
374  private javax.swing.JCheckBox sendIngestMessagesCheckbox;
375  // End of variables declaration//GEN-END:variables
376 }

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.