Autopsy  4.1
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 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.Dimension;
23 import java.awt.Toolkit;
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;
37 
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);
64  display();
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() {
74  Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
75  setLocation((screenDimension.width - getSize().width) / 2, (screenDimension.height - getSize().height) / 2);
76  setVisible(true);
77  }
78 
83  final void addHashValuesToDatabase() {
84  parentRef.enableAddHashValuesToDatabaseDialog(false);
85  new SwingWorker<Object, Void>() {
86 
87  @Override
88  protected Object doInBackground() throws Exception {
89  // parse the text for md5 hashes.
90  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.parsing"));
92 
93  // Perform checks for invalid input. Then perform insertion
94  // of hashes in the database.
95  if (!invalidHashes.isEmpty()) {
96  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.invalidHash"));
97  finish(false);
98  errorTitle = NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.invaliHash.msg");
99  errorMessage = "";
100  for (String invalidHash : invalidHashes) {
101  errorMessage = errorMessage + invalidHash + "\n"; // NON-NLS
102  }
103  showErrorsButton.setVisible(true);
104  showErrorsButton.requestFocus();
105  } else if (hashes.isEmpty()) {
106  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.noHashesToAdd"));
107  finish(false);
108  } else {
109  try {
110  hashDb.addHashes(hashes);
111  okButton.requestFocus();
112  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.success", hashes.size()));
113  finish(true);
114  disposeParent = true;
115  } catch (TskCoreException ex) {
116  statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.errorAddingValidHash"));
117  finish(false);
118  errorTitle = NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.errorAddingValidHash.msg");
119  errorMessage = ex.toString();
120  showErrorsButton.setVisible(true);
121  showErrorsButton.requestFocus();
122  }
123  }
124  return null;
125  }
126  }.execute();
127  }
128 
135  private void finish(boolean success) {
136  okButton.setEnabled(true);
137  addingHashesToDatabaseProgressBar.setIndeterminate(false);
139  if (success) {
140  // a hack to set progressbar color.
141  addingHashesToDatabaseProgressBar.setStringPainted(true);
142  addingHashesToDatabaseProgressBar.setForeground(new Color(50, 205, 50));
143  addingHashesToDatabaseProgressBar.setString("");
144  } else {
145  // a hack to set progressbar color.
146  addingHashesToDatabaseProgressBar.setStringPainted(true);
147  addingHashesToDatabaseProgressBar.setForeground(new Color(178, 34, 34));
148  addingHashesToDatabaseProgressBar.setString("");
149  }
150 
151  }
152 
160  private void getHashesFromTextArea(String text) {
161  String[] linesInTextArea = text.split("\\r?\\n"); // NON-NLS
162  // These entries may be of <MD5> or <MD5, comment> format
163  for (String hashEntry : linesInTextArea) {
164  hashEntry = hashEntry.trim();
165  Matcher m = md5Pattern.matcher(hashEntry);
166  if (m.find()) {
167  // more information can be added to the HashEntry - sha-1, sha-512, comment
168  hashes.add(new HashEntry(null, m.group(0), null, null, null));
169  } else {
170  if (!hashEntry.isEmpty()) {
171  invalidHashes.add(hashEntry);
172  }
173  }
174  }
175  }
176 
182  @SuppressWarnings("unchecked")
183  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
184  private void initComponents() {
185 
186  addingHashesToDatabaseProgressBar = new javax.swing.JProgressBar();
187  okButton = new javax.swing.JButton();
188  statusLabel = new javax.swing.JLabel();
189  showErrorsButton = new javax.swing.JButton();
190 
191  setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
192  setTitle(org.openide.util.NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.title")); // NOI18N
193 
194  addingHashesToDatabaseProgressBar.setIndeterminate(true);
195 
196  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.okButton.text")); // NOI18N
197  okButton.setEnabled(false);
198  okButton.addActionListener(new java.awt.event.ActionListener() {
199  public void actionPerformed(java.awt.event.ActionEvent evt) {
201  }
202  });
203 
204  org.openide.awt.Mnemonics.setLocalizedText(statusLabel, org.openide.util.NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.statusLabel.text")); // NOI18N
205 
206  org.openide.awt.Mnemonics.setLocalizedText(showErrorsButton, org.openide.util.NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class, "AddHashValuesToDatabaseProgressDialog.showErrorsButton.text")); // NOI18N
207  showErrorsButton.setVisible(false);
208  showErrorsButton.addActionListener(new java.awt.event.ActionListener() {
209  public void actionPerformed(java.awt.event.ActionEvent evt) {
211  }
212  });
213 
214  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
215  getContentPane().setLayout(layout);
216  layout.setHorizontalGroup(
217  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
218  .addGroup(layout.createSequentialGroup()
219  .addContainerGap()
220  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
221  .addGroup(layout.createSequentialGroup()
222  .addComponent(statusLabel)
223  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
224  .addComponent(showErrorsButton))
225  .addGroup(layout.createSequentialGroup()
226  .addComponent(addingHashesToDatabaseProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
227  .addGap(18, 18, 18)
228  .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
229  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
230  );
231  layout.setVerticalGroup(
232  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
233  .addGroup(layout.createSequentialGroup()
234  .addContainerGap()
235  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
236  .addComponent(addingHashesToDatabaseProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
237  .addComponent(okButton))
238  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
239  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
240  .addComponent(showErrorsButton)
241  .addComponent(statusLabel))
242  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
243  );
244 
245  pack();
246  }// </editor-fold>//GEN-END:initComponents
247 
248  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
249  parentRef.enableAddHashValuesToDatabaseDialog(true);
250  if (disposeParent) {
251  parentRef.dispose();
252  }
253  this.dispose();
254  }//GEN-LAST:event_okButtonActionPerformed
255 
256  private void showErrorsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showErrorsButtonActionPerformed
257  JLabel textLabel = new JLabel(errorTitle);
258  JTextArea textArea = new JTextArea(errorMessage);
259  textArea.setEditable(false);
260  JScrollPane scrollPane = new JScrollPane(textArea);
261  scrollPane.setPreferredSize(new Dimension(250, 100));
262  Object[] jOptionPaneComponents = {textLabel, scrollPane};
263  JOptionPane.showMessageDialog(this, jOptionPaneComponents, "Error:\n", JOptionPane.OK_OPTION); // NON-NLS
264  }//GEN-LAST:event_showErrorsButtonActionPerformed
265 
266  // Variables declaration - do not modify//GEN-BEGIN:variables
267  private javax.swing.JProgressBar addingHashesToDatabaseProgressBar;
268  private javax.swing.JButton okButton;
269  private javax.swing.JButton showErrorsButton;
270  private javax.swing.JLabel statusLabel;
271  // End of variables declaration//GEN-END:variables
272 }

Copyright © 2012-2016 Basis Technology. Generated on: Mon Apr 24 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.