Autopsy  4.20.0
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 2013-2021 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.io.File;
22 import java.io.IOException;
23 import java.nio.file.Paths;
24 import java.util.List;
25 import java.util.logging.Level;
26 import javax.swing.JFileChooser;
27 import javax.swing.JFrame;
28 import javax.swing.JOptionPane;
29 import javax.swing.filechooser.FileNameExtensionFilter;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.io.FilenameUtils;
32 import org.openide.util.NbBundle;
33 import org.openide.windows.WindowManager;
46 
52 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
53 final class HashDbImportDatabaseDialog extends javax.swing.JDialog {
54 
55  private JFileChooser fileChooser;
56  private String selectedFilePath = "";
57  private HashDb selectedHashDb = null;
58  private final static String LAST_FILE_PATH_KEY = "HashDbImport_Path";
59  private CentralRepoOrganization selectedOrg = null;
60  private List<CentralRepoOrganization> orgs = null;
61  private final JFileChooserFactory chooserHelper;
62 
68  HashDbImportDatabaseDialog() {
69  super((JFrame) WindowManager.getDefault().getMainWindow(),
70  NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.importHashDbMsg"),
71  true);
72  chooserHelper = new JFileChooserFactory();
73  initComponents();
74  enableComponents();
75  display();
76  }
77 
83  HashDb getHashDatabase() {
84  return selectedHashDb;
85  }
86 
87  private void display() {
88  setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
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 
100  private void enableComponents() {
101 
102  if (!CentralRepository.isEnabled() || !FeatureAccessUtils.canAddHashSetsToCentralRepo()) {
103  centralRepoRadioButton.setEnabled(false);
104  fileTypeRadioButton.setSelected(true);
105  } else {
106  populateCombobox();
107  }
108 
109  boolean isFileType = fileTypeRadioButton.isSelected();
110 
111  // Central repo only
112  lbVersion.setEnabled((!isFileType) && (readOnlyCheckbox.isSelected()));
113  versionTextField.setEnabled((!isFileType) && (readOnlyCheckbox.isSelected()));
114 
115  lbOrg.setEnabled(!isFileType);
116  orgButton.setEnabled(!isFileType);
117  orgComboBox.setEnabled(!isFileType);
118  readOnlyCheckbox.setEnabled(!isFileType);
119  }
120 
121  @NbBundle.Messages({"HashDbImportDatabaseDialog.populateOrgsError.message=Failure loading organizations."})
122  private void populateCombobox() {
123  orgComboBox.removeAllItems();
124  try {
125  CentralRepository dbManager = CentralRepository.getInstance();
126  orgs = dbManager.getOrganizations();
127  orgs.forEach((org) -> {
128  orgComboBox.addItem(org.getName());
129  if (CentralRepoDbUtil.isDefaultOrg(org)) {
130  orgComboBox.setSelectedItem(org.getName());
131  selectedOrg = org;
132  }
133  });
134  if ((selectedOrg == null) && (!orgs.isEmpty())) {
135  selectedOrg = orgs.get(0);
136  }
137  } catch (CentralRepoException ex) {
138  JOptionPane.showMessageDialog(this, Bundle.HashDbImportDatabaseDialog_populateOrgsError_message());
139  Logger.getLogger(ImportCentralRepoDbProgressDialog.class.getName()).log(Level.SEVERE, "Failure loading organizations", ex);
140  }
141  }
142 
148  @SuppressWarnings("unchecked")
149  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
150  private void initComponents() {
151 
152  buttonGroup1 = new javax.swing.ButtonGroup();
153  storageTypeButtonGroup = new javax.swing.ButtonGroup();
154  okButton = new javax.swing.JButton();
155  cancelButton = new javax.swing.JButton();
156  databasePathTextField = new javax.swing.JTextField();
157  openButton = new javax.swing.JButton();
158  knownRadioButton = new javax.swing.JRadioButton();
159  knownBadRadioButton = new javax.swing.JRadioButton();
160  jLabel1 = new javax.swing.JLabel();
161  hashSetNameTextField = new javax.swing.JTextField();
162  jLabel2 = new javax.swing.JLabel();
163  sendIngestMessagesCheckbox = new javax.swing.JCheckBox();
164  jLabel3 = new javax.swing.JLabel();
165  lbVersion = new javax.swing.JLabel();
166  versionTextField = new javax.swing.JTextField();
167  lbOrg = new javax.swing.JLabel();
168  orgComboBox = new javax.swing.JComboBox<>();
169  orgButton = new javax.swing.JButton();
170  readOnlyCheckbox = new javax.swing.JCheckBox();
171  fileTypeRadioButton = new javax.swing.JRadioButton();
172  centralRepoRadioButton = new javax.swing.JRadioButton();
173  jLabel4 = new javax.swing.JLabel();
174  saveInUserConfigFolderCheckbox = new javax.swing.JCheckBox();
175  noChangeRadioButton = new javax.swing.JRadioButton();
176 
177  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
178 
179  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.okButton.text")); // NOI18N
180  okButton.addActionListener(new java.awt.event.ActionListener() {
181  public void actionPerformed(java.awt.event.ActionEvent evt) {
182  okButtonActionPerformed(evt);
183  }
184  });
185 
186  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.cancelButton.text")); // NOI18N
187  cancelButton.addActionListener(new java.awt.event.ActionListener() {
188  public void actionPerformed(java.awt.event.ActionEvent evt) {
189  cancelButtonActionPerformed(evt);
190  }
191  });
192 
193  databasePathTextField.setEditable(false);
194  databasePathTextField.setText(org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.databasePathTextField.text")); // NOI18N
195 
196  org.openide.awt.Mnemonics.setLocalizedText(openButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.openButton.text")); // NOI18N
197  openButton.addActionListener(new java.awt.event.ActionListener() {
198  public void actionPerformed(java.awt.event.ActionEvent evt) {
199  openButtonActionPerformed(evt);
200  }
201  });
202 
203  buttonGroup1.add(knownRadioButton);
204  org.openide.awt.Mnemonics.setLocalizedText(knownRadioButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.knownRadioButton.text")); // NOI18N
205  knownRadioButton.addActionListener(new java.awt.event.ActionListener() {
206  public void actionPerformed(java.awt.event.ActionEvent evt) {
207  knownRadioButtonActionPerformed(evt);
208  }
209  });
210 
211  buttonGroup1.add(knownBadRadioButton);
212  knownBadRadioButton.setSelected(true);
213  org.openide.awt.Mnemonics.setLocalizedText(knownBadRadioButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.knownBadRadioButton.text")); // NOI18N
214  knownBadRadioButton.addActionListener(new java.awt.event.ActionListener() {
215  public void actionPerformed(java.awt.event.ActionEvent evt) {
216  knownBadRadioButtonActionPerformed(evt);
217  }
218  });
219 
220  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.jLabel1.text")); // NOI18N
221 
222  hashSetNameTextField.setText(org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.hashSetNameTextField.text")); // NOI18N
223 
224  org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.jLabel2.text")); // NOI18N
225 
226  sendIngestMessagesCheckbox.setSelected(true);
227  org.openide.awt.Mnemonics.setLocalizedText(sendIngestMessagesCheckbox, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.sendIngestMessagesCheckbox.text")); // NOI18N
228  sendIngestMessagesCheckbox.addActionListener(new java.awt.event.ActionListener() {
229  public void actionPerformed(java.awt.event.ActionEvent evt) {
230  sendIngestMessagesCheckboxActionPerformed(evt);
231  }
232  });
233 
234  org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.jLabel3.text")); // NOI18N
235 
236  org.openide.awt.Mnemonics.setLocalizedText(lbVersion, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.lbVersion.text")); // NOI18N
237 
238  versionTextField.setText(org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.versionTextField.text")); // NOI18N
239 
240  org.openide.awt.Mnemonics.setLocalizedText(lbOrg, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.lbOrg.text")); // NOI18N
241 
242  orgComboBox.addActionListener(new java.awt.event.ActionListener() {
243  public void actionPerformed(java.awt.event.ActionEvent evt) {
244  orgComboBoxActionPerformed(evt);
245  }
246  });
247 
248  org.openide.awt.Mnemonics.setLocalizedText(orgButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.orgButton.text")); // NOI18N
249  orgButton.addActionListener(new java.awt.event.ActionListener() {
250  public void actionPerformed(java.awt.event.ActionEvent evt) {
251  orgButtonActionPerformed(evt);
252  }
253  });
254 
255  readOnlyCheckbox.setSelected(true);
256  org.openide.awt.Mnemonics.setLocalizedText(readOnlyCheckbox, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.readOnlyCheckbox.text")); // NOI18N
257  readOnlyCheckbox.addActionListener(new java.awt.event.ActionListener() {
258  public void actionPerformed(java.awt.event.ActionEvent evt) {
259  readOnlyCheckboxActionPerformed(evt);
260  }
261  });
262 
263  storageTypeButtonGroup.add(fileTypeRadioButton);
264  fileTypeRadioButton.setSelected(true);
265  org.openide.awt.Mnemonics.setLocalizedText(fileTypeRadioButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.fileTypeRadioButton.text")); // NOI18N
266  fileTypeRadioButton.addActionListener(new java.awt.event.ActionListener() {
267  public void actionPerformed(java.awt.event.ActionEvent evt) {
268  fileTypeRadioButtonActionPerformed(evt);
269  }
270  });
271 
272  storageTypeButtonGroup.add(centralRepoRadioButton);
273  org.openide.awt.Mnemonics.setLocalizedText(centralRepoRadioButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.centralRepoRadioButton.text")); // NOI18N
274  centralRepoRadioButton.addActionListener(new java.awt.event.ActionListener() {
275  public void actionPerformed(java.awt.event.ActionEvent evt) {
276  centralRepoRadioButtonActionPerformed(evt);
277  }
278  });
279 
280  org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.jLabel4.text")); // NOI18N
281 
282  org.openide.awt.Mnemonics.setLocalizedText(saveInUserConfigFolderCheckbox, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.saveInUserConfigFolderCheckbox.text")); // NOI18N
283  saveInUserConfigFolderCheckbox.setToolTipText(org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.saveInUserConfigFolderCheckbox.toolTipText")); // NOI18N
284 
285  buttonGroup1.add(noChangeRadioButton);
286  org.openide.awt.Mnemonics.setLocalizedText(noChangeRadioButton, org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.noChangeRadioButton.text")); // NOI18N
287  noChangeRadioButton.setToolTipText(org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, "HashDbImportDatabaseDialog.noChangeRadioButton.toolTipText")); // NOI18N
288  noChangeRadioButton.addActionListener(new java.awt.event.ActionListener() {
289  public void actionPerformed(java.awt.event.ActionEvent evt) {
290  noChangeRadioButtonActionPerformed(evt);
291  }
292  });
293 
294  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
295  getContentPane().setLayout(layout);
296  layout.setHorizontalGroup(
297  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
298  .addGroup(layout.createSequentialGroup()
299  .addContainerGap()
300  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
301  .addGroup(layout.createSequentialGroup()
302  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
303  .addComponent(jLabel3)
304  .addComponent(jLabel4))
305  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
306  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
307  .addGroup(layout.createSequentialGroup()
308  .addComponent(fileTypeRadioButton)
309  .addGap(26, 26, 26)
310  .addComponent(centralRepoRadioButton)
311  .addGap(0, 0, Short.MAX_VALUE))
312  .addGroup(layout.createSequentialGroup()
313  .addComponent(databasePathTextField)
314  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
315  .addComponent(openButton))))
316  .addGroup(layout.createSequentialGroup()
317  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
318  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
319  .addComponent(lbOrg)
320  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
321  .addComponent(orgComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
322  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
323  .addComponent(orgButton))
324  .addGroup(layout.createSequentialGroup()
325  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
326  .addComponent(jLabel1)
327  .addComponent(lbVersion))
328  .addGap(40, 40, 40)
329  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
330  .addComponent(versionTextField)
331  .addComponent(hashSetNameTextField))))
332  .addGap(81, 81, 81))
333  .addGroup(layout.createSequentialGroup()
334  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
335  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
336  .addComponent(sendIngestMessagesCheckbox)
337  .addGap(0, 0, Short.MAX_VALUE))
338  .addGroup(layout.createSequentialGroup()
339  .addGap(0, 0, Short.MAX_VALUE)
340  .addComponent(okButton)))
341  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
342  .addComponent(cancelButton))
343  .addGroup(layout.createSequentialGroup()
344  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
345  .addComponent(jLabel2)
346  .addGroup(layout.createSequentialGroup()
347  .addGap(19, 19, 19)
348  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
349  .addComponent(knownRadioButton)
350  .addComponent(knownBadRadioButton)
351  .addComponent(noChangeRadioButton)))
352  .addComponent(saveInUserConfigFolderCheckbox)
353  .addComponent(readOnlyCheckbox))
354  .addGap(0, 0, Short.MAX_VALUE)))
355  .addContainerGap())
356  );
357 
358  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});
359 
360  layout.setVerticalGroup(
361  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
362  .addGroup(layout.createSequentialGroup()
363  .addContainerGap()
364  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
365  .addComponent(databasePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
366  .addComponent(jLabel3)
367  .addComponent(openButton))
368  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
369  .addComponent(fileTypeRadioButton)
370  .addComponent(centralRepoRadioButton)
371  .addComponent(jLabel4))
372  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
373  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
374  .addComponent(jLabel1)
375  .addComponent(hashSetNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
376  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
377  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
378  .addComponent(lbVersion)
379  .addComponent(versionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
380  .addGap(5, 5, 5)
381  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
382  .addComponent(orgButton)
383  .addComponent(orgComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
384  .addComponent(lbOrg))
385  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
386  .addComponent(jLabel2)
387  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
388  .addComponent(knownRadioButton)
389  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
390  .addComponent(knownBadRadioButton)
391  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
392  .addComponent(noChangeRadioButton)
393  .addGap(5, 5, 5)
394  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
395  .addGroup(layout.createSequentialGroup()
396  .addComponent(readOnlyCheckbox)
397  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
398  .addComponent(sendIngestMessagesCheckbox)
399  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
400  .addComponent(saveInUserConfigFolderCheckbox)
401  .addGap(0, 0, Short.MAX_VALUE))
402  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
403  .addGap(81, 81, 81)
404  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
405  .addComponent(cancelButton)
406  .addComponent(okButton))))
407  .addGap(18, 18, 18))
408  );
409 
410  pack();
411  }// </editor-fold>//GEN-END:initComponents
412 
413  private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
414  if(fileChooser == null) {
415  fileChooser = chooserHelper.getChooser();
416  fileChooser.setDragEnabled(false);
417  fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
418  String[] EXTENSION = new String[]{"txt", "kdb", "idx", "hash", "Hash", "hsh"}; //NON-NLS
419  FileNameExtensionFilter filter = new FileNameExtensionFilter(
420  NbBundle.getMessage(this.getClass(), "HashDbImportDatabaseDialog.fileNameExtFilter.text"), EXTENSION);
421  fileChooser.setFileFilter(filter);
422  fileChooser.setMultiSelectionEnabled(false);
423  }
424 
425  String lastBaseDirectory = HashLookupSettings.getDefaultDbPath();
426  if (ModuleSettings.settingExists(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY)) {
427  lastBaseDirectory = ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY);
428  }
429  File hashDbFolder = new File(lastBaseDirectory);
430  // create the folder if it doesn't exist
431  if (!hashDbFolder.exists()) {
432  hashDbFolder.mkdirs();
433  }
434  fileChooser.setCurrentDirectory(hashDbFolder);
435  if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
436  File databaseFile = fileChooser.getSelectedFile();
437  try {
438  selectedFilePath = databaseFile.getCanonicalPath();
439  databasePathTextField.setText(shortenPath(selectedFilePath));
440  hashSetNameTextField.setText(FilenameUtils.removeExtension(databaseFile.getName()));
441  if (hashSetNameTextField.getText().toLowerCase().contains("nsrl")) { //NON-NLS
442  knownRadioButton.setSelected(true);
443  knownRadioButtonActionPerformed(null);
444  }
445  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY, databaseFile.getParent());
446  } catch (IOException ex) {
447  Logger.getLogger(HashDbImportDatabaseDialog.class.getName()).log(Level.SEVERE, "Failed to get path of selected hash set", ex); //NON-NLS
448  JOptionPane.showMessageDialog(this,
449  NbBundle.getMessage(this.getClass(),
450  "HashDbImportDatabaseDialog.failedToGetDbPathMsg"));
451  }
452  }
453  }//GEN-LAST:event_openButtonActionPerformed
454 
455  private void knownRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_knownRadioButtonActionPerformed
456  sendIngestMessagesCheckbox.setSelected(KnownFilesType.KNOWN.isDefaultInboxMessages());
457  sendIngestMessagesCheckbox.setEnabled(KnownFilesType.KNOWN.isInboxMessagesAllowed());
458  }//GEN-LAST:event_knownRadioButtonActionPerformed
459 
460  private void knownBadRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_knownBadRadioButtonActionPerformed
461  sendIngestMessagesCheckbox.setSelected(KnownFilesType.KNOWN_BAD.isDefaultInboxMessages());
462  sendIngestMessagesCheckbox.setEnabled(KnownFilesType.KNOWN_BAD.isInboxMessagesAllowed());
463  }//GEN-LAST:event_knownBadRadioButtonActionPerformed
464 
465  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
466  this.dispose();
467  }//GEN-LAST:event_cancelButtonActionPerformed
468 
469  @NbBundle.Messages({"HashDbImportDatabaseDialog.missingVersion=A version must be entered",
470  "HashDbImportDatabaseDialog.missingOrg=An organization must be selected",
471  "HashDbImportDatabaseDialog.duplicateName=A hashset with this name and version already exists",
472  "HashDbImportDatabaseDialog.databaseLookupError=Error accessing central repository",
473  "HashDbImportDatabaseDialog.mustEnterHashSetNameMsg=A hash set name must be entered."
474  })
475  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
476  // Note that the error handlers in this method call return without disposing of the
477  // dialog to allow the user to try again, if desired.
478 
479  if (hashSetNameTextField.getText().isEmpty()) {
480  JOptionPane.showMessageDialog(this,
481  NbBundle.getMessage(this.getClass(),
482  "HashDbImportDatabaseDialog.mustEnterHashSetNameMsg"),
483  NbBundle.getMessage(this.getClass(),
484  "HashDbImportDatabaseDialog.importHashDbErr"),
485  JOptionPane.ERROR_MESSAGE);
486  return;
487  }
488 
489  if (centralRepoRadioButton.isSelected()) {
490  if (readOnlyCheckbox.isSelected() && versionTextField.getText().isEmpty()) {
491  JOptionPane.showMessageDialog(this,
492  NbBundle.getMessage(this.getClass(),
493  "HashDbImportDatabaseDialog.missingVersion"),
494  NbBundle.getMessage(this.getClass(),
495  "HashDbImportDatabaseDialog.importHashDbErr"),
496  JOptionPane.ERROR_MESSAGE);
497  return;
498  }
499 
500  if (selectedOrg == null) {
501  JOptionPane.showMessageDialog(this,
502  NbBundle.getMessage(this.getClass(),
503  "HashDbImportDatabaseDialog.missingOrg"),
504  NbBundle.getMessage(this.getClass(),
505  "HashDbImportDatabaseDialog.importHashDbErr"),
506  JOptionPane.ERROR_MESSAGE);
507  return;
508  }
509  }
510 
511  if (selectedFilePath.isEmpty()) {
512  JOptionPane.showMessageDialog(this,
513  NbBundle.getMessage(this.getClass(),
514  "HashDbImportDatabaseDialog.mustSelectHashDbFilePathMsg"),
515  NbBundle.getMessage(this.getClass(),
516  "HashDbImportDatabaseDialog.importHashDbErr"),
517  JOptionPane.ERROR_MESSAGE);
518  return;
519  }
520 
521  File file = new File(selectedFilePath);
522  if (!file.exists()) {
523  JOptionPane.showMessageDialog(this,
524  NbBundle.getMessage(this.getClass(),
525  "HashDbImportDatabaseDialog.hashDbDoesNotExistMsg"),
526  NbBundle.getMessage(this.getClass(),
527  "HashDbImportDatabaseDialog.importHashDbErr"),
528  JOptionPane.ERROR_MESSAGE);
529  return;
530  }
531 
532  if (saveInUserConfigFolderCheckbox.isSelected()) {
533  // copy the hash database to user configuration directory and use that path instead (JIRA-4177)
534  String locationInUserConfigDir = Paths.get(HashLookupSettings.getDefaultDbPath(), hashSetNameTextField.getText(), file.getName()).toString();
535  try {
536  File newFileLoc = new File(locationInUserConfigDir);
537  newFileLoc.getParentFile().mkdirs();
538  FileUtils.copyFile(file, newFileLoc);
539  // update the hash database location
540  selectedFilePath = locationInUserConfigDir;
541  } catch (IOException ex) {
542  String errorMessage = NbBundle.getMessage(this.getClass(), "HashDbImportDatabaseDialog.unableToCopyToUserDirMsg", locationInUserConfigDir);
543  Logger.getLogger(HashDbImportDatabaseDialog.class.getName()).log(Level.SEVERE, errorMessage, ex);
544  JOptionPane.showMessageDialog(this, errorMessage, NbBundle.getMessage(this.getClass(), "HashDbImportDatabaseDialog.importHashDbErr"),
545  JOptionPane.ERROR_MESSAGE);
546  return;
547  }
548  }
549 
550  KnownFilesType type;
551  if (knownRadioButton.isSelected()) {
552  type = KnownFilesType.KNOWN;
553  } else if (noChangeRadioButton.isSelected()) {
554  type = KnownFilesType.NO_CHANGE;
555  } else {
556  type = KnownFilesType.KNOWN_BAD;
557  }
558 
559  String errorMessage = NbBundle.getMessage(this.getClass(),
560  "HashDbImportDatabaseDialog.errorMessage.failedToOpenHashDbMsg",
561  selectedFilePath);
562  if (fileTypeRadioButton.isSelected()) {
563 
564  try {
565  selectedHashDb = HashDbManager.getInstance().addExistingHashDatabaseNoSave(hashSetNameTextField.getText(), selectedFilePath, true, sendIngestMessagesCheckbox.isSelected(), type);
566  } catch (HashDbManagerException ex) {
567  Logger.getLogger(HashDbImportDatabaseDialog.class.getName()).log(Level.WARNING, errorMessage, ex);
568  JOptionPane.showMessageDialog(this,
569  ex.getMessage(),
570  NbBundle.getMessage(this.getClass(),
571  "HashDbImportDatabaseDialog.importHashDbErr"),
572  JOptionPane.ERROR_MESSAGE);
573  return;
574  }
575  } else {
576 
577  // Check if a hash set with the same name/version already exists
578  try {
579  if (CentralRepository.getInstance().referenceSetExists(hashSetNameTextField.getText(), versionTextField.getText())) {
580  JOptionPane.showMessageDialog(this,
581  NbBundle.getMessage(this.getClass(),
582  "HashDbImportDatabaseDialog.duplicateName"),
583  NbBundle.getMessage(this.getClass(),
584  "HashDbImportDatabaseDialog.importHashDbErr"),
585  JOptionPane.ERROR_MESSAGE);
586  return;
587  }
588  } catch (CentralRepoException ex) {
589  Logger.getLogger(HashDbImportDatabaseDialog.class.getName()).log(Level.SEVERE, "Error looking up reference set", ex);
590  JOptionPane.showMessageDialog(this,
591  NbBundle.getMessage(this.getClass(),
592  "HashDbImportDatabaseDialog.databaseLookupError"),
593  NbBundle.getMessage(this.getClass(),
594  "HashDbImportDatabaseDialog.importHashDbErr"),
595  JOptionPane.ERROR_MESSAGE);
596  return;
597  }
598 
599  String version;
600  if (readOnlyCheckbox.isSelected()) {
601  version = versionTextField.getText();
602  } else {
603  // Editable databases don't have a version
604  version = "";
605  }
606  ImportCentralRepoDbProgressDialog progressDialog = new ImportCentralRepoDbProgressDialog();
607  progressDialog.importFile(hashSetNameTextField.getText(), version,
608  selectedOrg.getOrgID(), true, sendIngestMessagesCheckbox.isSelected(), type,
609  readOnlyCheckbox.isSelected(), selectedFilePath);
610  selectedHashDb = progressDialog.getDatabase();
611  }
612 
613  dispose();
614  }//GEN-LAST:event_okButtonActionPerformed
615 
616  private void sendIngestMessagesCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendIngestMessagesCheckboxActionPerformed
617  // TODO add your handling code here:
618  }//GEN-LAST:event_sendIngestMessagesCheckboxActionPerformed
619 
620  private void fileTypeRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileTypeRadioButtonActionPerformed
621  enableComponents();
622  }//GEN-LAST:event_fileTypeRadioButtonActionPerformed
623 
624  private void centralRepoRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_centralRepoRadioButtonActionPerformed
625  enableComponents();
626  }//GEN-LAST:event_centralRepoRadioButtonActionPerformed
627 
628  private void orgButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_orgButtonActionPerformed
629  ManageOrganizationsDialog dialog = new ManageOrganizationsDialog();
630  // update the combobox options
631  if (dialog.isChanged()) {
632  populateCombobox();
633  }
634  }//GEN-LAST:event_orgButtonActionPerformed
635 
636  private void orgComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_orgComboBoxActionPerformed
637  if (null == orgComboBox.getSelectedItem()) {
638  return;
639  }
640  String orgName = this.orgComboBox.getSelectedItem().toString();
641  for (CentralRepoOrganization org : orgs) {
642  if (org.getName().equals(orgName)) {
643  selectedOrg = org;
644  return;
645  }
646  }
647  }//GEN-LAST:event_orgComboBoxActionPerformed
648 
649  private void readOnlyCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_readOnlyCheckboxActionPerformed
650  enableComponents();
651  }//GEN-LAST:event_readOnlyCheckboxActionPerformed
652 
653  private void noChangeRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_noChangeRadioButtonActionPerformed
654  sendIngestMessagesCheckbox.setSelected(KnownFilesType.NO_CHANGE.isDefaultInboxMessages());
655  sendIngestMessagesCheckbox.setEnabled(KnownFilesType.NO_CHANGE.isInboxMessagesAllowed());
656  }//GEN-LAST:event_noChangeRadioButtonActionPerformed
657 
658  // Variables declaration - do not modify//GEN-BEGIN:variables
659  private javax.swing.ButtonGroup buttonGroup1;
660  private javax.swing.JButton cancelButton;
661  private javax.swing.JRadioButton centralRepoRadioButton;
662  private javax.swing.JTextField databasePathTextField;
663  private javax.swing.JRadioButton fileTypeRadioButton;
664  private javax.swing.JTextField hashSetNameTextField;
665  private javax.swing.JLabel jLabel1;
666  private javax.swing.JLabel jLabel2;
667  private javax.swing.JLabel jLabel3;
668  private javax.swing.JLabel jLabel4;
669  private javax.swing.JRadioButton knownBadRadioButton;
670  private javax.swing.JRadioButton knownRadioButton;
671  private javax.swing.JLabel lbOrg;
672  private javax.swing.JLabel lbVersion;
673  private javax.swing.JRadioButton noChangeRadioButton;
674  private javax.swing.JButton okButton;
675  private javax.swing.JButton openButton;
676  private javax.swing.JButton orgButton;
677  private javax.swing.JComboBox<String> orgComboBox;
678  private javax.swing.JCheckBox readOnlyCheckbox;
679  private javax.swing.JCheckBox saveInUserConfigFolderCheckbox;
680  private javax.swing.JCheckBox sendIngestMessagesCheckbox;
681  private javax.swing.ButtonGroup storageTypeButtonGroup;
682  private javax.swing.JTextField versionTextField;
683  // End of variables declaration//GEN-END:variables
684 }

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.