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

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.