Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddHashValuesToDatabaseProgressDialog.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013-2018 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.Color;
22 import java.awt.Component;
23 import java.awt.Dimension;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28 import javax.swing.JLabel;
29 import javax.swing.JOptionPane;
30 import javax.swing.JScrollPane;
31 import javax.swing.JTextArea;
32 import javax.swing.SwingWorker;
33 import org.openide.util.NbBundle;
35 import org.sleuthkit.datamodel.HashEntry;
36 import org.sleuthkit.datamodel.TskCoreException;
37 
41 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
42 public class AddHashValuesToDatabaseProgressDialog extends javax.swing.JDialog {
43 
45  private boolean disposeParent = false;
46  private final HashDb hashDb;
47  private final List<HashEntry> hashes;
48  private final List<String> invalidHashes;
49  private final Pattern md5Pattern;
50  private String errorTitle;
51  private String errorMessage;
52  private final String text;
53 
62  super(parent);
63  initComponents();
64  display(parent);
65  this.hashes = new ArrayList<>();
66  this.invalidHashes = new ArrayList<>();
67  this.md5Pattern = Pattern.compile("^[a-fA-F0-9]{32}$"); // NON-NLS
68  this.parentRef = parent;
69  this.hashDb = hashDb;
70  this.text = text;
71  }
72 
73  private void display(Component parent) {
74  setLocationRelativeTo(parent);
75  setVisible(true);
76  }
77 
82  final void addHashValuesToDatabase() {
83  parentRef.enableAddHashValuesToDatabaseDialog(false);
84  new SwingWorker<Object, Void>() {
85 
86  @Override
87  protected Object doInBackground() throws Exception {
88  // parse the text for md5 hashes.
89  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.parsing"));
90  getHashesFromTextArea(text);
91 
92  // Perform checks for invalid input. Then perform insertion
93  // of hashes in the database.
94  if (!invalidHashes.isEmpty()) {
95  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.invalidHash"));
96  finish(false);
97  errorTitle = NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.invaliHash.msg");
98  errorMessage = "";
99  for (String invalidHash : invalidHashes) {
100  errorMessage = errorMessage + invalidHash + "\n"; // NON-NLS
101  }
102  showErrorsButton.setVisible(true);
103  showErrorsButton.requestFocus();
104  } else if (hashes.isEmpty()) {
105  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.noHashesToAdd"));
106  finish(false);
107  } else {
108  try {
109  hashDb.addHashes(hashes);
110  okButton.requestFocus();
111  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.success", hashes.size()));
112  finish(true);
113  disposeParent = true;
114  } catch (TskCoreException ex) {
115  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.errorAddingValidHash"));
116  finish(false);
117  errorTitle = NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.errorAddingValidHash.msg");
118  errorMessage = ex.toString();
119  showErrorsButton.setVisible(true);
120  showErrorsButton.requestFocus();
121  }
122  }
123  return null;
124  }
125  }.execute();
126  }
127 
134  private void finish(boolean success) {
135  okButton.setEnabled(true);
136  addingHashesToDatabaseProgressBar.setIndeterminate(false);
137  addingHashesToDatabaseProgressBar.setValue(addingHashesToDatabaseProgressBar.getMaximum());
138  if (success) {
139  // a hack to set progressbar color.
140  addingHashesToDatabaseProgressBar.setStringPainted(true);
141  addingHashesToDatabaseProgressBar.setForeground(new Color(50, 205, 50));
142  addingHashesToDatabaseProgressBar.setString("");
143  } else {
144  // a hack to set progressbar color.
145  addingHashesToDatabaseProgressBar.setStringPainted(true);
146  addingHashesToDatabaseProgressBar.setForeground(new Color(178, 34, 34));
147  addingHashesToDatabaseProgressBar.setString("");
148  }
149 
150  }
151 
159  private void getHashesFromTextArea(String text) {
160  String[] linesInTextArea = text.split("\\r?\\n"); // NON-NLS
161  // These entries may be of <MD5> or <MD5, comment> format
162  for (String hashEntry : linesInTextArea) {
163  hashEntry = hashEntry.trim();
164  Matcher m = md5Pattern.matcher(hashEntry);
165  if (m.find()) {
166  // more information can be added to the HashEntry - sha-1, sha-512, comment
167  hashes.add(new HashEntry(null, m.group(0), null, null, null));
168  } else {
169  if (!hashEntry.isEmpty()) {
170  invalidHashes.add(hashEntry);
171  }
172  }
173  }
174  }
175 
181  @SuppressWarnings("unchecked")
182  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
183  private void initComponents() {
184 
185  addingHashesToDatabaseProgressBar = new javax.swing.JProgressBar();
186  okButton = new javax.swing.JButton();
187  statusLabel = new javax.swing.JLabel();
188  showErrorsButton = new javax.swing.JButton();
189 
190  setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
191  setTitle(org.openide.util.NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.title")); // NOI18N
192 
193  addingHashesToDatabaseProgressBar.setIndeterminate(true);
194 
195  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.okButton.text")); // NOI18N
196  okButton.setEnabled(false);
197  okButton.addActionListener(new java.awt.event.ActionListener() {
198  public void actionPerformed(java.awt.event.ActionEvent evt) {
199  okButtonActionPerformed(evt);
200  }
201  });
202 
203  org.openide.awt.Mnemonics.setLocalizedText(statusLabel, org.openide.util.NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.statusLabel.text")); // NOI18N
204 
205  org.openide.awt.Mnemonics.setLocalizedText(showErrorsButton, org.openide.util.NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.showErrorsButton.text")); // NOI18N
206  showErrorsButton.setVisible(false);
207  showErrorsButton.addActionListener(new java.awt.event.ActionListener() {
208  public void actionPerformed(java.awt.event.ActionEvent evt) {
209  showErrorsButtonActionPerformed(evt);
210  }
211  });
212 
213  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
214  getContentPane().setLayout(layout);
215  layout.setHorizontalGroup(
216  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
217  .addGroup(layout.createSequentialGroup()
218  .addContainerGap()
219  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
220  .addGroup(layout.createSequentialGroup()
221  .addComponent(statusLabel)
222  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
223  .addComponent(showErrorsButton))
224  .addGroup(layout.createSequentialGroup()
225  .addComponent(addingHashesToDatabaseProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
226  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
227  .addComponent(okButton)))
228  .addContainerGap())
229  );
230  layout.setVerticalGroup(
231  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
232  .addGroup(layout.createSequentialGroup()
233  .addContainerGap()
234  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
235  .addComponent(addingHashesToDatabaseProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
236  .addComponent(okButton))
237  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
238  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
239  .addComponent(statusLabel)
240  .addComponent(showErrorsButton))
241  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
242  );
243 
244  pack();
245  }// </editor-fold>//GEN-END:initComponents
246 
247  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
248  parentRef.enableAddHashValuesToDatabaseDialog(true);
249  if (disposeParent) {
250  parentRef.dispose();
251  }
252  this.dispose();
253  }//GEN-LAST:event_okButtonActionPerformed
254 
255  private void showErrorsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showErrorsButtonActionPerformed
256  JLabel textLabel = new JLabel(errorTitle);
257  JTextArea textArea = new JTextArea(errorMessage);
258  textArea.setEditable(false);
259  JScrollPane scrollPane = new JScrollPane(textArea);
260  scrollPane.setPreferredSize(new Dimension(250, 100));
261  Object[] jOptionPaneComponents = {textLabel, scrollPane};
262  JOptionPane.showMessageDialog(this, jOptionPaneComponents, "Error:\n", JOptionPane.OK_OPTION); // NON-NLS
263  }//GEN-LAST:event_showErrorsButtonActionPerformed
264 
265  // Variables declaration - do not modify//GEN-BEGIN:variables
266  private javax.swing.JProgressBar addingHashesToDatabaseProgressBar;
267  private javax.swing.JButton okButton;
268  private javax.swing.JButton showErrorsButton;
269  private javax.swing.JLabel statusLabel;
270  // End of variables declaration//GEN-END:variables
271 }

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.