Autopsy  4.19.3
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.apache.commons.lang3.StringUtils;
34 import org.openide.util.NbBundle;
36 import org.sleuthkit.datamodel.HashEntry;
37 import org.sleuthkit.datamodel.TskCoreException;
38 
42 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
43 public class AddHashValuesToDatabaseProgressDialog extends javax.swing.JDialog {
44 
46  private boolean disposeParent = false;
47  private final HashDb hashDb;
48  private final List<HashEntry> hashes;
49  private final List<String> invalidHashes;
50 
51  // Matches hash with optional comma separated comment.
52  private static final Pattern HASH_LINE_PATTERN = Pattern.compile("^([a-fA-F0-9]{32})(,(.*))?$");
53  // The regex group for the hash.
54  private static final int HASH_GROUP = 1;
55  // The regex group for the comment.
56  private static final int COMMENT_GROUP = 3;
57 
58  private String errorTitle;
59  private String errorMessage;
60  private final String text;
61 
70  super(parent);
71  initComponents();
72  display(parent);
73  this.hashes = new ArrayList<>();
74  this.invalidHashes = new ArrayList<>();
75  this.parentRef = parent;
76  this.hashDb = hashDb;
77  this.text = text;
78  }
79 
80  private void display(Component parent) {
81  setLocationRelativeTo(parent);
82  setVisible(true);
83  }
84 
89  final void addHashValuesToDatabase() {
90  parentRef.enableAddHashValuesToDatabaseDialog(false);
91  new SwingWorker<Object, Void>() {
92 
93  @Override
94  protected Object doInBackground() throws Exception {
95  // parse the text for md5 hashes.
96  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.parsing"));
97  getHashesFromTextArea(text);
98 
99  // Perform checks for invalid input. Then perform insertion
100  // of hashes in the database.
101  if (!invalidHashes.isEmpty()) {
102  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.invalidHash"));
103  finish(false);
104  errorTitle = NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.invaliHash.msg");
105  errorMessage = "";
106  for (String invalidHash : invalidHashes) {
107  errorMessage = errorMessage + invalidHash + "\n"; // NON-NLS
108  }
109  showErrorsButton.setVisible(true);
110  showErrorsButton.requestFocus();
111  } else if (hashes.isEmpty()) {
112  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.noHashesToAdd"));
113  finish(false);
114  } else {
115  try {
116  hashDb.addHashes(hashes);
117  okButton.requestFocus();
118  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.success", hashes.size()));
119  finish(true);
120  disposeParent = true;
121  } catch (TskCoreException ex) {
122  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.errorAddingValidHash"));
123  finish(false);
124  errorTitle = NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.errorAddingValidHash.msg");
125  errorMessage = ex.toString();
126  showErrorsButton.setVisible(true);
127  showErrorsButton.requestFocus();
128  }
129  }
130  return null;
131  }
132  }.execute();
133  }
134 
141  private void finish(boolean success) {
142  okButton.setEnabled(true);
143  addingHashesToDatabaseProgressBar.setIndeterminate(false);
144  addingHashesToDatabaseProgressBar.setValue(addingHashesToDatabaseProgressBar.getMaximum());
145  if (success) {
146  // a hack to set progressbar color.
147  addingHashesToDatabaseProgressBar.setStringPainted(true);
148  addingHashesToDatabaseProgressBar.setForeground(new Color(50, 205, 50));
149  addingHashesToDatabaseProgressBar.setString("");
150  } else {
151  // a hack to set progressbar color.
152  addingHashesToDatabaseProgressBar.setStringPainted(true);
153  addingHashesToDatabaseProgressBar.setForeground(new Color(178, 34, 34));
154  addingHashesToDatabaseProgressBar.setString("");
155  }
156 
157  }
158 
166  private void getHashesFromTextArea(String text) {
167  String[] linesInTextArea = text.split("\\r?\\n"); // NON-NLS
168  // These entries may be of <MD5> or <MD5, comment> format
169  for (String hashEntry : linesInTextArea) {
170  hashEntry = hashEntry.trim();
171  Matcher m = HASH_LINE_PATTERN.matcher(hashEntry);
172  if (m.find()) {
173  String hash = m.group(HASH_GROUP);
174 
175  // if there was a match and the match is not empty, assign to comment
176  String comment = StringUtils.isNotBlank(m.group(COMMENT_GROUP)) ?
177  m.group(COMMENT_GROUP).trim() : null;
178 
179  hashes.add(new HashEntry(null, hash, null, null, comment));
180  } else {
181  if (!hashEntry.isEmpty()) {
182  invalidHashes.add(hashEntry);
183  }
184  }
185  }
186  }
187 
193  @SuppressWarnings("unchecked")
194  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
195  private void initComponents() {
196 
197  addingHashesToDatabaseProgressBar = new javax.swing.JProgressBar();
198  okButton = new javax.swing.JButton();
199  statusLabel = new javax.swing.JLabel();
200  showErrorsButton = new javax.swing.JButton();
201 
202  setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
203  setTitle(org.openide.util.NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.title")); // NOI18N
204 
205  addingHashesToDatabaseProgressBar.setIndeterminate(true);
206 
207  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.okButton.text")); // NOI18N
208  okButton.setEnabled(false);
209  okButton.addActionListener(new java.awt.event.ActionListener() {
210  public void actionPerformed(java.awt.event.ActionEvent evt) {
211  okButtonActionPerformed(evt);
212  }
213  });
214 
215  org.openide.awt.Mnemonics.setLocalizedText(statusLabel, org.openide.util.NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.statusLabel.text")); // NOI18N
216 
217  org.openide.awt.Mnemonics.setLocalizedText(showErrorsButton, org.openide.util.NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.showErrorsButton.text")); // NOI18N
218  showErrorsButton.setVisible(false);
219  showErrorsButton.addActionListener(new java.awt.event.ActionListener() {
220  public void actionPerformed(java.awt.event.ActionEvent evt) {
221  showErrorsButtonActionPerformed(evt);
222  }
223  });
224 
225  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
226  getContentPane().setLayout(layout);
227  layout.setHorizontalGroup(
228  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
229  .addGroup(layout.createSequentialGroup()
230  .addContainerGap()
231  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
232  .addGroup(layout.createSequentialGroup()
233  .addComponent(statusLabel)
234  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
235  .addComponent(showErrorsButton))
236  .addGroup(layout.createSequentialGroup()
237  .addComponent(addingHashesToDatabaseProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
238  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
239  .addComponent(okButton)))
240  .addContainerGap())
241  );
242  layout.setVerticalGroup(
243  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
244  .addGroup(layout.createSequentialGroup()
245  .addContainerGap()
246  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
247  .addComponent(addingHashesToDatabaseProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
248  .addComponent(okButton))
249  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
250  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
251  .addComponent(statusLabel)
252  .addComponent(showErrorsButton))
253  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
254  );
255 
256  pack();
257  }// </editor-fold>//GEN-END:initComponents
258 
259  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
260  parentRef.enableAddHashValuesToDatabaseDialog(true);
261  if (disposeParent) {
262  parentRef.dispose();
263  }
264  this.dispose();
265  }//GEN-LAST:event_okButtonActionPerformed
266 
267  private void showErrorsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showErrorsButtonActionPerformed
268  JLabel textLabel = new JLabel(errorTitle);
269  JTextArea textArea = new JTextArea(errorMessage);
270  textArea.setEditable(false);
271  JScrollPane scrollPane = new JScrollPane(textArea);
272  scrollPane.setPreferredSize(new Dimension(250, 100));
273  Object[] jOptionPaneComponents = {textLabel, scrollPane};
274  JOptionPane.showMessageDialog(this, jOptionPaneComponents, "Error:\n", JOptionPane.OK_OPTION); // NON-NLS
275  }//GEN-LAST:event_showErrorsButtonActionPerformed
276 
277  // Variables declaration - do not modify//GEN-BEGIN:variables
278  private javax.swing.JProgressBar addingHashesToDatabaseProgressBar;
279  private javax.swing.JButton okButton;
280  private javax.swing.JButton showErrorsButton;
281  private javax.swing.JLabel statusLabel;
282  // End of variables declaration//GEN-END:variables
283 }

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