Autopsy  4.4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ImportHashDatabaseDialog.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-2017 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.centralrepository.optionspanel;
20 
21 import java.awt.Dimension;
22 import java.awt.Toolkit;
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.net.UnknownHostException;
28 import java.nio.file.Files;
29 import java.time.LocalDate;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Set;
35 import java.util.concurrent.ExecutionException;
36 import java.util.logging.Level;
37 import javax.swing.JComboBox;
38 import javax.swing.JFileChooser;
39 import javax.swing.JFrame;
40 import javax.swing.JOptionPane;
41 import javax.swing.JTextField;
42 import javax.swing.SwingUtilities;
43 import javax.swing.SwingWorker;
44 import javax.swing.event.DocumentEvent;
45 import javax.swing.event.DocumentListener;
46 import javax.swing.filechooser.FileNameExtensionFilter;
47 import org.netbeans.api.progress.ProgressHandle;
48 import org.netbeans.spi.options.OptionsPanelController;
49 import org.openide.util.Exceptions;
50 import org.openide.util.NbBundle;
51 import org.openide.util.NbBundle.Messages;
52 import org.openide.windows.WindowManager;
63 import org.sleuthkit.datamodel.TskData;
64 
70 final class ImportHashDatabaseDialog extends javax.swing.JDialog {
71  private static final Logger LOGGER = Logger.getLogger(ImportHashDatabaseDialog.class.getName());
72 
73  private final JFileChooser fileChooser = new JFileChooser();
74  private final static String LAST_FILE_PATH_KEY = "CentralRepositoryImport_Path"; // NON-NLS
75  private final int HASH_IMPORT_THRESHOLD = 10000;
76  private EamOrganization selectedOrg = null;
77  private List<EamOrganization> orgs = null;
78  private final Collection<JTextField> textBoxes;
79  private final TextBoxChangedListener textBoxChangedListener;
80 
81 
87  @Messages({"ImportHashDatabaseDialog.importHashDbMsg=Import Hash Database"})
88  ImportHashDatabaseDialog() {
89  super((JFrame) WindowManager.getDefault().getMainWindow(),
90  Bundle.ImportHashDatabaseDialog_importHashDbMsg(),
91  true); // NON-NLS
92  textBoxes = new ArrayList<>();
93  textBoxChangedListener = new TextBoxChangedListener();
94  initFileChooser();
95  initComponents();
96  customizeComponents();
97  display();
98  }
99 
100  @Messages({"ImportHashDatabaseDialog.fileNameExtFilter.text=Hash Database File",})
101  private void initFileChooser() {
102  fileChooser.setDragEnabled(false);
103  fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
104  String[] EXTENSION = new String[]{"idx"}; //NON-NLS
105  FileNameExtensionFilter filter = new FileNameExtensionFilter(
106  Bundle.ImportHashDatabaseDialog_fileNameExtFilter_text(),
107  EXTENSION); // NON-NLS
108  fileChooser.setFileFilter(filter);
109  fileChooser.setMultiSelectionEnabled(false);
110 
111  if (ModuleSettings.settingExists(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY)) {
112  String lastBaseDirectory = ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY);
113  File hashDbFolder = new File(lastBaseDirectory);
114  if(hashDbFolder.exists()){
115  fileChooser.setCurrentDirectory(hashDbFolder);
116  }
117  }
118  }
119 
120  private void customizeComponents() {
121  populateCombobox();
122  setTextBoxListeners();
123  enableOkButton(false);
124  }
125 
129  private void setTextBoxListeners() {
130  textBoxes.add(tfFilePath);
131  textBoxes.add(tfDatabaseName);
132  textBoxes.add(tfDatabaseVersion);
133  addDocumentListeners(textBoxes, textBoxChangedListener);
134  }
135 
136  private void populateCombobox() {
137  comboboxSourceOrganization.removeAllItems();
138  try {
139  EamDb dbManager = EamDb.getInstance();
140  orgs = dbManager.getOrganizations();
141  orgs.forEach((org) -> {
142  comboboxSourceOrganization.addItem(org.getName());
143  });
144  if (!orgs.isEmpty()) {
145  selectedOrg = orgs.get(0);
146  }
147  valid();
148  } catch (EamDbException ex) {
149  LOGGER.log(Level.SEVERE, "Failure populating combobox with organizations.", ex);
150  }
151  }
152 
159  private static void addDocumentListeners(Collection<JTextField> textFields, TextBoxChangedListener listener) {
160  textFields.forEach((textField) -> {
161  textField.getDocument().addDocumentListener(listener);
162  });
163  }
164 
171  private boolean textFieldsArePopulated() {
172  return !tfDatabaseName.getText().trim().isEmpty()
173  && !tfDatabaseVersion.getText().trim().isEmpty()
174  && !tfFilePath.getText().trim().isEmpty();
175  }
176 
182  @Messages({"ImportHashDatabaseDialog.validation.incompleteFields=Fill in all values"})
183  private boolean checkFields() {
184  boolean result = true;
185 
186  boolean allPopulated = textFieldsArePopulated();
187 
188  if (!allPopulated) {
189  // We don't even have everything filled out
190  result = false;
191  lbWarningMsg.setText(Bundle.ImportHashDatabaseDialog_validation_incompleteFields());
192  }
193  return result;
194  }
195 
201  @Messages({"ImportHashDatabaseDialog.validation.notEnabled=Central Repository is not enabled."})
202  public boolean valid() {
203  lbWarningMsg.setText("");
204  if (!EamDb.isEnabled()) {
205  lbWarningMsg.setText(Bundle.ImportHashDatabaseDialog_validation_notEnabled());
206  return false;
207  }
208 
209  return enableOkButton(checkFields() && null != selectedOrg);
210  }
211 
219  private boolean enableOkButton(Boolean enable) {
220  okButton.setEnabled(enable);
221  return enable;
222  }
223 
228  private class TextBoxChangedListener implements DocumentListener {
229 
230  @Override
231  public void changedUpdate(DocumentEvent e) {
232  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
233  valid();
234  }
235 
236  @Override
237  public void insertUpdate(DocumentEvent e) {
238  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
239  valid();
240  }
241 
242  @Override
243  public void removeUpdate(DocumentEvent e) {
244  firePropertyChange(OptionsPanelController.PROP_CHANGED, null, null);
245  valid();
246  }
247  }
248 
249  private void display() {
250  Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
251  setLocation((screenDimension.width - getSize().width) / 2, (screenDimension.height - getSize().height) / 2);
252  setVisible(true);
253  }
254 
260  @SuppressWarnings("unchecked")
261  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
262  private void initComponents() {
263 
264  buttonGroup1 = new javax.swing.ButtonGroup();
265  okButton = new javax.swing.JButton();
266  cancelButton = new javax.swing.JButton();
267  tfFilePath = new javax.swing.JTextField();
268  openButton = new javax.swing.JButton();
269  knownRadioButton = new javax.swing.JRadioButton();
270  knownBadRadioButton = new javax.swing.JRadioButton();
271  lbDatabaseType = new javax.swing.JLabel();
272  lbDatabasePath = new javax.swing.JLabel();
273  lbDatabaseAttribution = new javax.swing.JLabel();
274  lbSourceOrganization = new javax.swing.JLabel();
275  lbDatabaseName = new javax.swing.JLabel();
276  lbDatabaseVersion = new javax.swing.JLabel();
277  comboboxSourceOrganization = new javax.swing.JComboBox<>();
278  tfDatabaseName = new javax.swing.JTextField();
279  tfDatabaseVersion = new javax.swing.JTextField();
280  bnNewOrganization = new javax.swing.JButton();
281  lbWarningMsg = new javax.swing.JLabel();
282  lbInstructions = new javax.swing.JLabel();
283 
284  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
285 
286  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.okButton.text")); // NOI18N
287  okButton.addActionListener(new java.awt.event.ActionListener() {
288  public void actionPerformed(java.awt.event.ActionEvent evt) {
289  okButtonActionPerformed(evt);
290  }
291  });
292 
293  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.cancelButton.text")); // NOI18N
294  cancelButton.addActionListener(new java.awt.event.ActionListener() {
295  public void actionPerformed(java.awt.event.ActionEvent evt) {
296  cancelButtonActionPerformed(evt);
297  }
298  });
299 
300  tfFilePath.setText(org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.tfFilePath.text")); // NOI18N
301 
302  org.openide.awt.Mnemonics.setLocalizedText(openButton, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.openButton.text")); // NOI18N
303  openButton.addActionListener(new java.awt.event.ActionListener() {
304  public void actionPerformed(java.awt.event.ActionEvent evt) {
305  openButtonActionPerformed(evt);
306  }
307  });
308 
309  buttonGroup1.add(knownRadioButton);
310  org.openide.awt.Mnemonics.setLocalizedText(knownRadioButton, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.knownRadioButton.text")); // NOI18N
311 
312  buttonGroup1.add(knownBadRadioButton);
313  knownBadRadioButton.setSelected(true);
314  org.openide.awt.Mnemonics.setLocalizedText(knownBadRadioButton, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.knownBadRadioButton.text")); // NOI18N
315 
316  org.openide.awt.Mnemonics.setLocalizedText(lbDatabaseType, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.lbDatabaseType.text")); // NOI18N
317 
318  org.openide.awt.Mnemonics.setLocalizedText(lbDatabasePath, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.lbFilePath.text")); // NOI18N
319 
320  org.openide.awt.Mnemonics.setLocalizedText(lbDatabaseAttribution, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.lbDatabaseAttribution.text")); // NOI18N
321 
322  org.openide.awt.Mnemonics.setLocalizedText(lbSourceOrganization, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.lbSourceOrganization.text")); // NOI18N
323 
324  org.openide.awt.Mnemonics.setLocalizedText(lbDatabaseName, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.lbDatabaseName.text")); // NOI18N
325 
326  org.openide.awt.Mnemonics.setLocalizedText(lbDatabaseVersion, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.lbDatabaseVersion.text")); // NOI18N
327 
328  comboboxSourceOrganization.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
329  comboboxSourceOrganization.addActionListener(new java.awt.event.ActionListener() {
330  public void actionPerformed(java.awt.event.ActionEvent evt) {
331  comboboxSourceOrganizationActionPerformed(evt);
332  }
333  });
334 
335  tfDatabaseName.setToolTipText(org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.tfDatabaseName.tooltip")); // NOI18N
336 
337  tfDatabaseVersion.setToolTipText(org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.tfDatabaseVersion.tooltip.text")); // NOI18N
338 
339  org.openide.awt.Mnemonics.setLocalizedText(bnNewOrganization, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.bnNewOrganization.text")); // NOI18N
340  bnNewOrganization.addActionListener(new java.awt.event.ActionListener() {
341  public void actionPerformed(java.awt.event.ActionEvent evt) {
342  bnNewOrganizationActionPerformed(evt);
343  }
344  });
345 
346  lbWarningMsg.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
347  lbWarningMsg.setForeground(new java.awt.Color(255, 0, 0));
348 
349  lbInstructions.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
350  org.openide.awt.Mnemonics.setLocalizedText(lbInstructions, org.openide.util.NbBundle.getMessage(ImportHashDatabaseDialog.class, "ImportHashDatabaseDialog.lbInstructions.text")); // NOI18N
351 
352  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
353  getContentPane().setLayout(layout);
354  layout.setHorizontalGroup(
355  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
356  .addGroup(layout.createSequentialGroup()
357  .addContainerGap()
358  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
359  .addComponent(lbInstructions, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
360  .addGroup(layout.createSequentialGroup()
361  .addGap(0, 0, Short.MAX_VALUE)
362  .addComponent(okButton)
363  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
364  .addComponent(cancelButton))
365  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
366  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
367  .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
368  .addComponent(lbDatabasePath)
369  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
370  .addComponent(tfFilePath))
371  .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
372  .addGap(23, 23, 23)
373  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
374  .addGroup(layout.createSequentialGroup()
375  .addComponent(lbDatabaseVersion)
376  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
377  .addComponent(tfDatabaseVersion, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
378  .addGap(0, 0, Short.MAX_VALUE))
379  .addGroup(layout.createSequentialGroup()
380  .addComponent(lbSourceOrganization)
381  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
382  .addComponent(bnNewOrganization))
383  .addGroup(layout.createSequentialGroup()
384  .addComponent(lbDatabaseName)
385  .addGap(12, 12, 12)
386  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
387  .addComponent(comboboxSourceOrganization, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
388  .addComponent(tfDatabaseName))))))
389  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
390  .addComponent(openButton))
391  .addGroup(layout.createSequentialGroup()
392  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
393  .addComponent(lbDatabaseType)
394  .addGroup(layout.createSequentialGroup()
395  .addGap(19, 19, 19)
396  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
397  .addComponent(knownRadioButton)
398  .addComponent(knownBadRadioButton)))
399  .addComponent(lbDatabaseAttribution))
400  .addGap(0, 0, Short.MAX_VALUE))
401  .addComponent(lbWarningMsg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
402  .addContainerGap())
403  );
404 
405  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});
406 
407  layout.setVerticalGroup(
408  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
409  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
410  .addContainerGap()
411  .addComponent(lbInstructions, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
412  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
413  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
414  .addComponent(openButton)
415  .addComponent(tfFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
416  .addComponent(lbDatabasePath))
417  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
418  .addGroup(layout.createSequentialGroup()
419  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
420  .addComponent(lbDatabaseType)
421  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
422  .addComponent(knownRadioButton)
423  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
424  .addComponent(knownBadRadioButton)
425  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
426  .addComponent(lbDatabaseAttribution)
427  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
428  .addComponent(bnNewOrganization)
429  .addGroup(layout.createSequentialGroup()
430  .addGap(3, 3, 3)
431  .addComponent(lbSourceOrganization, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
432  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
433  .addComponent(comboboxSourceOrganization, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
434  .addGap(18, 18, 18)
435  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
436  .addComponent(tfDatabaseName)
437  .addComponent(lbDatabaseName, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
438  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
439  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
440  .addComponent(lbDatabaseVersion, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
441  .addComponent(tfDatabaseVersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
442  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
443  .addComponent(lbWarningMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
444  .addContainerGap(44, Short.MAX_VALUE))
445  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
446  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
447  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
448  .addComponent(okButton)
449  .addComponent(cancelButton))
450  .addContainerGap())))
451  );
452 
453  pack();
454  }// </editor-fold>//GEN-END:initComponents
455 
456  @Messages({"ImportHashDatabaseDialog.failedToGetDbPathMsg=Failed to get the path of the selected database.",})
457  private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
458  if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
459  File databaseFile = fileChooser.getSelectedFile();
460  try {
461  tfFilePath.setText(databaseFile.getCanonicalPath());
462  if (databaseFile.getName().toLowerCase().contains("nsrl")) { //NON-NLS
463  knownRadioButton.setSelected(true);
464  }
465  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY, databaseFile.getParent());
466  } catch (IOException ex) {
467  Logger.getLogger(ImportHashDatabaseDialog.class.getName()).log(Level.SEVERE, "Failed to get path of selected database", ex); // NON-NLS
468  lbWarningMsg.setText(Bundle.ImportHashDatabaseDialog_failedToGetDbPathMsg());
469  }
470  }
471  valid();
472  }//GEN-LAST:event_openButtonActionPerformed
473 
474  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
475  dispose();
476  }//GEN-LAST:event_cancelButtonActionPerformed
477 
484  private int createGlobalSet() throws EamDbException {
485  EamDb dbManager = EamDb.getInstance();
486  EamGlobalSet eamGlobalSet = new EamGlobalSet(
487  selectedOrg.getOrgID(),
488  tfDatabaseName.getText().trim(),
489  tfDatabaseVersion.getText().trim(),
490  LocalDate.now());
491  return dbManager.newReferencelSet(eamGlobalSet);
492  }
493 
494  @Messages({"ImportHashDatabaseDialog.createGlobalSet.failedMsg.text=Failed to store attribution details.",
495  "ImportHashDatabaseDialog.mustSelectHashDbFilePathMsg=Missing hash database file path.",
496  "ImportHashDatabaseDialog.hashDbDoesNotExistMsg=The selected hash database does not exist.",
497  "# {0} - selected file path",
498  "ImportHashDatabaseDialog.errorMessage.failedToOpenHashDbMsg=Failed to open hash database at {0}.",
499 })
500  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
501  // Note that the error handlers in this method call return without disposing of the
502  // dialog to allow the user to try again, if desired.
503  String selectedFilePath = tfFilePath.getText();
504 
505  // have valid file path
506  if (selectedFilePath.isEmpty()) {
507  lbWarningMsg.setText(Bundle.ImportHashDatabaseDialog_mustSelectHashDbFilePathMsg());
508  return;
509  }
510  File file = new File(selectedFilePath);
511  if (!file.exists()) {
512  lbWarningMsg.setText(Bundle.ImportHashDatabaseDialog_hashDbDoesNotExistMsg());
513  return;
514  }
515 
516  // create global set
517  int globalSetID;
518  try {
519  globalSetID = createGlobalSet();
520  } catch (EamDbException ex) {
521  LOGGER.log(Level.SEVERE, "Failed to create global set.", ex);
522  lbWarningMsg.setText(Bundle.ImportHashDatabaseDialog_createGlobalSet_failedMsg_text());
523  return;
524  }
525 
526  // insert hashes
527  TskData.FileKnown knownStatus = TskData.FileKnown.UNKNOWN;
528  if (knownRadioButton.isSelected()) {
529  knownStatus = TskData.FileKnown.KNOWN;
530  } else if (knownBadRadioButton.isSelected()) {
531  knownStatus = TskData.FileKnown.BAD;
532  }
533 
534  String errorMessage = Bundle.ImportHashDatabaseDialog_errorMessage_failedToOpenHashDbMsg(selectedFilePath);
535  // Future, make UI handle more than the "FILES" type.
536  try {
537  EamDb dbManager = EamDb.getInstance();
538  CorrelationAttribute.Type contentType = dbManager.getCorrelationTypeById(CorrelationAttribute.FILES_TYPE_ID); // get "FILES" type
539  // run in the background and close dialog
540  SwingUtilities.invokeLater(new ImportHashDatabaseWorker(selectedFilePath, knownStatus, globalSetID, contentType)::execute);
541  dispose();
542  } catch (EamDbException | UnknownHostException ex) {
543  Logger.getLogger(ImportHashDatabaseDialog.class.getName()).log(Level.SEVERE, errorMessage, ex);
544  lbWarningMsg.setText(ex.getMessage());
545  }
546 
547  }//GEN-LAST:event_okButtonActionPerformed
548 
549  @SuppressWarnings({"unchecked"})
550  private void bnNewOrganizationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnNewOrganizationActionPerformed
551  AddNewOrganizationDialog dialogO = new AddNewOrganizationDialog();
552  // update the combobox options
553  if (dialogO.isChanged()) {
554  populateCombobox();
555  }
556  }//GEN-LAST:event_bnNewOrganizationActionPerformed
557 
558  @SuppressWarnings({"unchecked"})
559  private void comboboxSourceOrganizationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboboxSourceOrganizationActionPerformed
560  JComboBox<String> cb = (JComboBox<String>)evt.getSource();
561  String orgName = (String)cb.getSelectedItem();
562  if (null == orgName) return;
563 
564  for (EamOrganization org : orgs) {
565  if (org.getName().equals(orgName)) {
566  selectedOrg = org;
567  return;
568  }
569  }
570  valid();
571  }//GEN-LAST:event_comboboxSourceOrganizationActionPerformed
572 
573  @NbBundle.Messages({"ImportHashDatabaseDialog.ImportHashDatabaseWorker.displayName=Importing Hash Database"})
574  private class ImportHashDatabaseWorker extends SwingWorker<Void, Void> {
575 
576  private final File file;
577  private final TskData.FileKnown knownStatus;
578  private final int globalSetID;
579  private final ProgressHandle progress;
581 
582  public ImportHashDatabaseWorker(String filename, TskData.FileKnown knownStatus, int globalSetID, CorrelationAttribute.Type contentType) throws EamDbException, UnknownHostException {
583  this.file = new File(filename);
584  this.knownStatus = knownStatus;
585  this.globalSetID = globalSetID;
586  this.contentType = contentType;
587  this.progress = ProgressHandle.createHandle(Bundle.ImportHashDatabaseDialog_ImportHashDatabaseWorker_displayName());
588 
589  if (!EamDb.isEnabled()) {
590  throw new EamDbException("Central repository database is not enabled."); // NON-NLS
591  }
592  }
593 
594  @Override
595  protected Void doInBackground() throws Exception {
596  importHashDatabase();
597  return null;
598  }
599 
600  @Override
601  @Messages({"ImportHashDatabaseDialog.ImportHashDatabaseWorker.error=Failed to import hash database."})
602  protected void done() {
603  progress.finish();
604  try {
605  get();
606  } catch (InterruptedException | ExecutionException ex) {
607  Logger.getLogger(ImportHashDatabaseDialog.class.getName()).log(Level.SEVERE, Bundle.ImportHashDatabaseDialog_ImportHashDatabaseWorker_error(), ex);
608  MessageNotifyUtil.Notify.show(Bundle.ImportHashDatabaseDialog_ImportHashDatabaseWorker_error(),
609  ex.getMessage(),
611  }
612  }
613 
614  private long numberOfLinesInFile(File f) throws IOException {
615  return Files.lines(f.toPath()).count();
616  }
617 
618  @Messages({"# {0} - value content",
619  "ImportHashDatabaseDialog.ImportHashDatabaseWorker.duplicate=Duplicate value {0} found in import file."})
620  private void importHashDatabase() throws EamDbException, IOException {
621  BufferedReader reader = new BufferedReader(new FileReader(file));
622  String line;
623  EamDb dbManager = EamDb.getInstance();
624  Set<EamGlobalFileInstance> globalInstances = new HashSet<>();
625 
626  long totalLines = numberOfLinesInFile(file);
627  if (totalLines <= Integer.MAX_VALUE) {
628  progress.start((int) totalLines);
629  } else {
630  progress.start();
631  }
632 
633  int numLines = 0;
634  LOGGER.log(Level.INFO, "Importing hash database {0}", file.getName());
635  while ((line = reader.readLine()) != null) {
636  progress.progress(++numLines);
637 
638  String[] parts = line.split("\\|");
639 
640  // Header lines start with a 41 character dummy hash, 1 character longer than a SHA-1 hash
641  if (parts.length != 2 || parts[0].length() == 41) {
642  continue;
643  }
644 
645  EamGlobalFileInstance eamGlobalFileInstance = new EamGlobalFileInstance(
646  globalSetID,
647  parts[0].toLowerCase(),
648  knownStatus,
649  "");
650 
651  globalInstances.add(eamGlobalFileInstance);
652 
653  if(numLines % HASH_IMPORT_THRESHOLD == 0){
654  dbManager.bulkInsertReferenceTypeEntries(globalInstances, contentType);
655  globalInstances.clear();
656  }
657  }
658 
659  dbManager.bulkInsertReferenceTypeEntries(globalInstances, contentType);
660  LOGGER.log(Level.INFO, "Finished importing hash database. Total entries: {0}", numLines);
661 
662  }
663  }
664 
665  // Variables declaration - do not modify//GEN-BEGIN:variables
666  private javax.swing.JButton bnNewOrganization;
667  private javax.swing.ButtonGroup buttonGroup1;
668  private javax.swing.JButton cancelButton;
669  private javax.swing.JComboBox<String> comboboxSourceOrganization;
670  private javax.swing.JRadioButton knownBadRadioButton;
671  private javax.swing.JRadioButton knownRadioButton;
672  private javax.swing.JLabel lbDatabaseAttribution;
673  private javax.swing.JLabel lbDatabaseName;
674  private javax.swing.JLabel lbDatabasePath;
675  private javax.swing.JLabel lbDatabaseType;
676  private javax.swing.JLabel lbDatabaseVersion;
677  private javax.swing.JLabel lbInstructions;
678  private javax.swing.JLabel lbSourceOrganization;
679  private javax.swing.JLabel lbWarningMsg;
680  private javax.swing.JButton okButton;
681  private javax.swing.JButton openButton;
682  private javax.swing.JTextField tfDatabaseName;
683  private javax.swing.JTextField tfDatabaseVersion;
684  private javax.swing.JTextField tfFilePath;
685  // End of variables declaration//GEN-END:variables
686 }
ImportHashDatabaseWorker(String filename, TskData.FileKnown knownStatus, int globalSetID, CorrelationAttribute.Type contentType)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
static void show(String title, String message, MessageType type, ActionListener actionListener)
void bulkInsertReferenceTypeEntries(Set< EamGlobalFileInstance > globalInstances, CorrelationAttribute.Type contentType)

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