19 package org.sleuthkit.autopsy.modules.hashdatabase;
21 import java.awt.Dimension;
22 import java.awt.Toolkit;
24 import java.io.IOException;
25 import java.util.logging.Level;
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;
45 final class HashDbImportDatabaseDialog
extends javax.swing.JDialog {
47 private JFileChooser fileChooser =
new JFileChooser();
48 private String selectedFilePath =
"";
49 private HashDb selectedHashDb = null;
50 private final static String LAST_FILE_PATH_KEY =
"HashDbImport_Path";
57 HashDbImportDatabaseDialog() {
59 NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.importHashDbMsg"),
71 HashDb getHashDatabase() {
72 return selectedHashDb;
75 private void initFileChooser() {
76 fileChooser.setDragEnabled(
false);
77 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
78 String[] EXTENSION =
new String[]{
"txt",
"kdb",
"idx",
"hash",
"Hash",
"hsh"};
79 FileNameExtensionFilter filter =
new FileNameExtensionFilter(
80 NbBundle.getMessage(
this.getClass(),
"HashDbImportDatabaseDialog.fileNameExtFilter.text"), EXTENSION);
81 fileChooser.setFileFilter(filter);
82 fileChooser.setMultiSelectionEnabled(
false);
85 private void display() {
86 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
87 setLocation((screenDimension.width - getSize().width) / 2, (screenDimension.height - getSize().height) / 2);
91 private static String shortenPath(String path) {
92 String shortenedPath = path;
93 if (shortenedPath.length() > 50) {
94 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));
104 @SuppressWarnings(
"unchecked")
106 private
void initComponents() {
108 buttonGroup1 =
new javax.swing.ButtonGroup();
109 okButton =
new javax.swing.JButton();
110 cancelButton =
new javax.swing.JButton();
111 databasePathTextField =
new javax.swing.JTextField();
112 openButton =
new javax.swing.JButton();
113 knownRadioButton =
new javax.swing.JRadioButton();
114 knownBadRadioButton =
new javax.swing.JRadioButton();
115 jLabel1 =
new javax.swing.JLabel();
116 hashSetNameTextField =
new javax.swing.JTextField();
117 jLabel2 =
new javax.swing.JLabel();
118 sendIngestMessagesCheckbox =
new javax.swing.JCheckBox();
119 jLabel3 =
new javax.swing.JLabel();
121 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
123 org.openide.awt.Mnemonics.setLocalizedText(okButton,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.okButton.text"));
124 okButton.addActionListener(
new java.awt.event.ActionListener() {
125 public void actionPerformed(java.awt.event.ActionEvent evt) {
126 okButtonActionPerformed(evt);
130 org.openide.awt.Mnemonics.setLocalizedText(cancelButton,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.cancelButton.text"));
131 cancelButton.addActionListener(
new java.awt.event.ActionListener() {
132 public void actionPerformed(java.awt.event.ActionEvent evt) {
133 cancelButtonActionPerformed(evt);
137 databasePathTextField.setEditable(
false);
138 databasePathTextField.setText(
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.databasePathTextField.text"));
140 org.openide.awt.Mnemonics.setLocalizedText(openButton,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.openButton.text"));
141 openButton.addActionListener(
new java.awt.event.ActionListener() {
142 public void actionPerformed(java.awt.event.ActionEvent evt) {
143 openButtonActionPerformed(evt);
147 buttonGroup1.add(knownRadioButton);
148 org.openide.awt.Mnemonics.setLocalizedText(knownRadioButton,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.knownRadioButton.text"));
149 knownRadioButton.addActionListener(
new java.awt.event.ActionListener() {
150 public void actionPerformed(java.awt.event.ActionEvent evt) {
151 knownRadioButtonActionPerformed(evt);
155 buttonGroup1.add(knownBadRadioButton);
156 knownBadRadioButton.setSelected(
true);
157 org.openide.awt.Mnemonics.setLocalizedText(knownBadRadioButton,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.knownBadRadioButton.text"));
158 knownBadRadioButton.addActionListener(
new java.awt.event.ActionListener() {
159 public void actionPerformed(java.awt.event.ActionEvent evt) {
160 knownBadRadioButtonActionPerformed(evt);
164 org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.jLabel1.text"));
166 hashSetNameTextField.setText(
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.hashSetNameTextField.text"));
168 org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.jLabel2.text"));
170 sendIngestMessagesCheckbox.setSelected(
true);
171 org.openide.awt.Mnemonics.setLocalizedText(sendIngestMessagesCheckbox,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.sendIngestMessagesCheckbox.text"));
172 sendIngestMessagesCheckbox.addActionListener(
new java.awt.event.ActionListener() {
173 public void actionPerformed(java.awt.event.ActionEvent evt) {
174 sendIngestMessagesCheckboxActionPerformed(evt);
178 org.openide.awt.Mnemonics.setLocalizedText(jLabel3,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.jLabel3.text"));
180 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(getContentPane());
181 getContentPane().setLayout(layout);
182 layout.setHorizontalGroup(
183 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
184 .addGroup(layout.createSequentialGroup()
186 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
187 .addGroup(layout.createSequentialGroup()
188 .addGap(0, 0, Short.MAX_VALUE)
189 .addComponent(okButton)
190 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
191 .addComponent(cancelButton))
192 .addGroup(layout.createSequentialGroup()
193 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
194 .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
195 .addComponent(jLabel1)
196 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
197 .addComponent(hashSetNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE))
198 .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
199 .addComponent(jLabel3)
200 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
201 .addComponent(databasePathTextField)))
202 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
203 .addComponent(openButton))
204 .addGroup(layout.createSequentialGroup()
205 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
206 .addComponent(jLabel2)
207 .addGroup(layout.createSequentialGroup()
209 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
210 .addComponent(knownRadioButton)
211 .addComponent(knownBadRadioButton)))
212 .addComponent(sendIngestMessagesCheckbox))
213 .addGap(0, 0, Short.MAX_VALUE)))
217 layout.linkSize(javax.swing.SwingConstants.HORIZONTAL,
new java.awt.Component[] {cancelButton, okButton});
219 layout.setVerticalGroup(
220 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
221 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
223 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
224 .addComponent(openButton)
225 .addComponent(databasePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
226 .addComponent(jLabel3))
227 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
228 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
229 .addComponent(jLabel1)
230 .addComponent(hashSetNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
231 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
232 .addGroup(layout.createSequentialGroup()
233 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 119, Short.MAX_VALUE)
234 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
235 .addComponent(okButton)
236 .addComponent(cancelButton))
238 .addGroup(layout.createSequentialGroup()
239 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
240 .addComponent(jLabel2)
241 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
242 .addComponent(knownRadioButton)
243 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
244 .addComponent(knownBadRadioButton)
246 .addComponent(sendIngestMessagesCheckbox)
247 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
253 private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {
254 if (ModuleSettings.settingExists(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY)) {
255 fileChooser.setCurrentDirectory(
new File(ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY)));
257 if (fileChooser.showOpenDialog(
this) == JFileChooser.APPROVE_OPTION) {
258 File databaseFile = fileChooser.getSelectedFile();
260 selectedFilePath = databaseFile.getCanonicalPath();
261 databasePathTextField.setText(shortenPath(selectedFilePath));
262 hashSetNameTextField.setText(FilenameUtils.removeExtension(databaseFile.getName()));
263 if (hashSetNameTextField.getText().toLowerCase().contains(
"nsrl")) {
264 knownRadioButton.setSelected(
true);
265 knownRadioButtonActionPerformed(null);
267 ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY, databaseFile.getParent());
268 }
catch (IOException ex) {
269 Logger.getLogger(HashDbImportDatabaseDialog.class.getName()).log(Level.SEVERE,
"Failed to get path of selected database", ex);
270 JOptionPane.showMessageDialog(
this,
271 NbBundle.getMessage(
this.getClass(),
272 "HashDbImportDatabaseDialog.failedToGetDbPathMsg"));
277 private void knownRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
278 sendIngestMessagesCheckbox.setSelected(
false);
279 sendIngestMessagesCheckbox.setEnabled(
false);
282 private void knownBadRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
283 sendIngestMessagesCheckbox.setSelected(
true);
284 sendIngestMessagesCheckbox.setEnabled(
true);
287 private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
291 private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
295 if (hashSetNameTextField.getText().isEmpty()) {
296 JOptionPane.showMessageDialog(
this,
297 NbBundle.getMessage(
this.getClass(),
298 "HashDbCreateDatabaseDialog.mustEnterHashSetNameMsg"),
299 NbBundle.getMessage(
this.getClass(),
300 "HashDbImportDatabaseDialog.importHashDbErr"),
301 JOptionPane.ERROR_MESSAGE);
305 if (selectedFilePath.isEmpty()) {
306 JOptionPane.showMessageDialog(
this,
307 NbBundle.getMessage(
this.getClass(),
308 "HashDbImportDatabaseDialog.mustSelectHashDbFilePathMsg"),
309 NbBundle.getMessage(
this.getClass(),
310 "HashDbImportDatabaseDialog.importHashDbErr"),
311 JOptionPane.ERROR_MESSAGE);
314 File file =
new File(selectedFilePath);
315 if (!file.exists()) {
316 JOptionPane.showMessageDialog(
this,
317 NbBundle.getMessage(
this.getClass(),
318 "HashDbImportDatabaseDialog.hashDbDoesNotExistMsg"),
319 NbBundle.getMessage(
this.getClass(),
320 "HashDbImportDatabaseDialog.importHashDbErr"),
321 JOptionPane.ERROR_MESSAGE);
326 if (knownRadioButton.isSelected()) {
327 type = KnownFilesType.KNOWN;
329 type = KnownFilesType.KNOWN_BAD;
332 String errorMessage = NbBundle.getMessage(this.getClass(),
333 "HashDbImportDatabaseDialog.errorMessage.failedToOpenHashDbMsg",
336 selectedHashDb = HashDbManager.getInstance().addExistingHashDatabaseInternal(hashSetNameTextField.getText(), selectedFilePath,
true, sendIngestMessagesCheckbox.isSelected(), type);
337 }
catch (HashDbManagerException ex) {
338 Logger.getLogger(HashDbImportDatabaseDialog.class.getName()).log(Level.WARNING, errorMessage, ex);
339 JOptionPane.showMessageDialog(
this,
341 NbBundle.getMessage(this.getClass(),
342 "HashDbImportDatabaseDialog.importHashDbErr"),
343 JOptionPane.ERROR_MESSAGE);
345 }
catch (TskCoreException ex) {
346 Logger.getLogger(HashDbCreateDatabaseDialog.class.getName()).log(Level.SEVERE, errorMessage, ex);
347 JOptionPane.showMessageDialog(
this,
349 NbBundle.getMessage(
this.getClass(),
350 "HashDbImportDatabaseDialog.importHashDbErr"),
351 JOptionPane.ERROR_MESSAGE);
358 private void sendIngestMessagesCheckboxActionPerformed(java.awt.event.ActionEvent evt) {
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;