Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
HashDbSearchThread.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-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.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.concurrent.CancellationException;
25 import java.util.logging.Level;
26 import javax.swing.JOptionPane;
27 import javax.swing.SwingWorker;
28 import org.netbeans.api.progress.ProgressHandle;
29 import org.openide.util.Cancellable;
30 import org.openide.util.NbBundle;
31 import org.openide.windows.WindowManager;
33 import org.sleuthkit.datamodel.AbstractFile;
34 
35 class HashDbSearchThread extends SwingWorker<Object, Void> {
36 
37  private Logger logger = Logger.getLogger(HashDbSearchThread.class.getName());
38  private ProgressHandle progress;
39  private Map<String, List<AbstractFile>> map;
40  private ArrayList<String> hashes = new ArrayList<>();
41  private AbstractFile file;
42 
43  HashDbSearchThread(AbstractFile file) {
44  this.file = file;
45  this.hashes.add(this.file.getMd5Hash());
46  }
47 
48  HashDbSearchThread(ArrayList<String> hashes) {
49  this.hashes = hashes;
50  }
51 
52  @Override
53  protected Object doInBackground() throws Exception {
54  logger.log(Level.INFO, "Starting background processing for file search by MD5 hash."); //NON-NLS
55 
56  // Setup progress bar
57  final String displayName = NbBundle.getMessage(this.getClass(), "HashDbSearchThread.name.searching");
58  progress = ProgressHandle.createHandle(displayName, new Cancellable() {
59  @Override
60  public boolean cancel() {
61  if (progress != null) {
62  progress.setDisplayName(
63  NbBundle.getMessage(this.getClass(), "HashDbSearchThread.progress.cancellingSearch",
64  displayName));
65  }
66  return HashDbSearchThread.this.cancel(true);
67  }
68  });
69  // Start the progress bar as indeterminate
70  progress.start();
71  progress.switchToIndeterminate();
72 
73  // Do the querying
74  map = HashDbSearcher.findFilesBymd5(hashes, progress, this);
75  logger.log(Level.INFO, "Done background processing"); //NON-NLS
76 
77  return null;
78  }
79 
80  @Override
81  protected void done() {
82  try {
83  super.get(); //block and get all exceptions thrown while doInBackground()
84  } catch (CancellationException ex) {
85  logger.log(Level.INFO, "File search by MD5 hash was canceled."); //NON-NLS
86  } catch (InterruptedException ex) {
87  logger.log(Level.INFO, "File search by MD5 hash was interrupted."); //NON-NLS
88  } catch (Exception ex) {
89  logger.log(Level.SEVERE, "Fatal error during file search by MD5 hash.", ex); //NON-NLS
90  } finally {
91  progress.finish();
92  if (!this.isCancelled()) {
93  logger.log(Level.INFO, "File search by MD5 hash completed without cancellation."); //NON-NLS
94  // If its a right click action, we are given an FsContent which
95  // is the file right clicked, so we can remove that from the search
96  if (file != null) {
97  boolean quit = true;
98  for (List<AbstractFile> files : map.values()) {
99  files.remove(file);
100  if (!files.isEmpty()) {
101  quit = false;
102  }
103  }
104  if (quit) {
105  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
106  NbBundle.getMessage(this.getClass(),
107  "HashDbSearchThread.noMoreFilesWithMD5Msg"));
108  return;
109  }
110  }
111  HashDbSearchManager man = new HashDbSearchManager(map);
112  man.execute();
113  } else {
114  logger.log(Level.INFO, "File search by MD5 hash was canceled."); //NON-NLS
115  }
116  }
117  }
118 
119 }

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