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;
44 final class HashDbImportDatabaseDialog
extends javax.swing.JDialog {
46 private JFileChooser fileChooser =
new JFileChooser();
47 private String selectedFilePath =
"";
48 private HashDb selectedHashDb = null;
55 HashDbImportDatabaseDialog() {
57 NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.importHashDbMsg"),
68 HashDb getHashDatabase() {
69 return selectedHashDb;
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"};
76 FileNameExtensionFilter filter =
new FileNameExtensionFilter(
77 NbBundle.getMessage(
this.getClass(),
"HashDbImportDatabaseDialog.fileNameExtFilter.text"), EXTENSION);
78 fileChooser.setFileFilter(filter);
79 fileChooser.setMultiSelectionEnabled(
false);
82 private void display() {
83 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
84 setLocation((screenDimension.width - getSize().width) / 2, (screenDimension.height - getSize().height) / 2);
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));
101 @SuppressWarnings(
"unchecked")
103 private
void initComponents() {
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();
118 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
120 org.openide.awt.Mnemonics.setLocalizedText(okButton,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.okButton.text"));
121 okButton.addActionListener(
new java.awt.event.ActionListener() {
122 public void actionPerformed(java.awt.event.ActionEvent evt) {
123 okButtonActionPerformed(evt);
127 org.openide.awt.Mnemonics.setLocalizedText(cancelButton,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.cancelButton.text"));
128 cancelButton.addActionListener(
new java.awt.event.ActionListener() {
129 public void actionPerformed(java.awt.event.ActionEvent evt) {
130 cancelButtonActionPerformed(evt);
134 databasePathTextField.setEditable(
false);
135 databasePathTextField.setText(
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.databasePathTextField.text"));
137 org.openide.awt.Mnemonics.setLocalizedText(openButton,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.openButton.text"));
138 openButton.addActionListener(
new java.awt.event.ActionListener() {
139 public void actionPerformed(java.awt.event.ActionEvent evt) {
140 openButtonActionPerformed(evt);
144 buttonGroup1.add(knownRadioButton);
145 org.openide.awt.Mnemonics.setLocalizedText(knownRadioButton,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.knownRadioButton.text"));
146 knownRadioButton.addActionListener(
new java.awt.event.ActionListener() {
147 public void actionPerformed(java.awt.event.ActionEvent evt) {
148 knownRadioButtonActionPerformed(evt);
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"));
155 knownBadRadioButton.addActionListener(
new java.awt.event.ActionListener() {
156 public void actionPerformed(java.awt.event.ActionEvent evt) {
157 knownBadRadioButtonActionPerformed(evt);
161 org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.jLabel1.text"));
163 hashSetNameTextField.setText(
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.hashSetNameTextField.text"));
165 org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.jLabel2.text"));
167 sendIngestMessagesCheckbox.setSelected(
true);
168 org.openide.awt.Mnemonics.setLocalizedText(sendIngestMessagesCheckbox,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.sendIngestMessagesCheckbox.text"));
169 sendIngestMessagesCheckbox.addActionListener(
new java.awt.event.ActionListener() {
170 public void actionPerformed(java.awt.event.ActionEvent evt) {
171 sendIngestMessagesCheckboxActionPerformed(evt);
175 org.openide.awt.Mnemonics.setLocalizedText(jLabel3,
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class,
"HashDbImportDatabaseDialog.jLabel3.text"));
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()
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()
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)))
214 layout.linkSize(javax.swing.SwingConstants.HORIZONTAL,
new java.awt.Component[] {cancelButton, okButton});
216 layout.setVerticalGroup(
217 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
218 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
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))
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)
243 .addComponent(sendIngestMessagesCheckbox)
244 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
250 private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {
251 if (fileChooser.showOpenDialog(
this) == JFileChooser.APPROVE_OPTION) {
252 File databaseFile = fileChooser.getSelectedFile();
254 selectedFilePath = databaseFile.getCanonicalPath();
255 databasePathTextField.setText(shortenPath(selectedFilePath));
256 hashSetNameTextField.setText(FilenameUtils.removeExtension(databaseFile.getName()));
257 if (hashSetNameTextField.getText().toLowerCase().contains(
"nsrl")) {
258 knownRadioButton.setSelected(
true);
259 knownRadioButtonActionPerformed(null);
262 catch (IOException ex) {
263 Logger.getLogger(HashDbImportDatabaseDialog.class.getName()).log(Level.SEVERE,
"Failed to get path of selected database", ex);
264 JOptionPane.showMessageDialog(
this,
265 NbBundle.getMessage(
this.getClass(),
266 "HashDbImportDatabaseDialog.failedToGetDbPathMsg"));
271 private void knownRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
272 sendIngestMessagesCheckbox.setSelected(
false);
273 sendIngestMessagesCheckbox.setEnabled(
false);
276 private void knownBadRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
277 sendIngestMessagesCheckbox.setSelected(
true);
278 sendIngestMessagesCheckbox.setEnabled(
true);
281 private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
285 private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
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);
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);
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);
320 if (knownRadioButton.isSelected()) {
321 type = KnownFilesType.KNOWN;
324 type = KnownFilesType.KNOWN_BAD;
327 String errorMessage = NbBundle.getMessage(this.getClass(),
328 "HashDbImportDatabaseDialog.errorMessage.failedToOpenHashDbMsg",
331 selectedHashDb = HashDbManager.getInstance().addExistingHashDatabaseInternal(hashSetNameTextField.getText(), selectedFilePath,
true, sendIngestMessagesCheckbox.isSelected(), type);
333 catch (HashDbManagerException ex) {
334 Logger.getLogger(HashDbImportDatabaseDialog.class.getName()).log(Level.WARNING, errorMessage, ex);
335 JOptionPane.showMessageDialog(
this,
337 NbBundle.getMessage(this.getClass(),
338 "HashDbImportDatabaseDialog.importHashDbErr"),
339 JOptionPane.ERROR_MESSAGE);
342 catch (TskCoreException ex) {
343 Logger.getLogger(HashDbCreateDatabaseDialog.class.getName()).log(Level.SEVERE, errorMessage, ex);
344 JOptionPane.showMessageDialog(
this,
346 NbBundle.getMessage(
this.getClass(),
347 "HashDbImportDatabaseDialog.importHashDbErr"),
348 JOptionPane.ERROR_MESSAGE);
355 private void sendIngestMessagesCheckboxActionPerformed(java.awt.event.ActionEvent evt) {
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;