Autopsy  4.19.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileSearchPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2021 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.filesearch;
20 
21 import java.awt.Component;
22 import java.awt.Cursor;
23 import java.awt.GridLayout;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.beans.PropertyChangeEvent;
27 import java.beans.PropertyChangeListener;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.concurrent.CancellationException;
33 import java.util.concurrent.ExecutionException;
34 import java.util.logging.Level;
35 import javax.swing.JLabel;
36 import javax.swing.JPanel;
37 import javax.swing.SwingWorker;
38 import javax.swing.border.EmptyBorder;
39 import org.openide.DialogDisplayer;
40 import org.openide.NotifyDescriptor;
41 import org.openide.nodes.Node;
42 import org.openide.util.NbBundle;
51 import org.sleuthkit.datamodel.AbstractFile;
52 import org.sleuthkit.datamodel.SleuthkitCase;
53 import org.sleuthkit.datamodel.TskCoreException;
54 
58 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
59 class FileSearchPanel extends javax.swing.JPanel {
60 
61  private static final Logger logger = Logger.getLogger(FileSearchPanel.class.getName());
62  private static final long serialVersionUID = 1L;
63  private final List<FileSearchFilter> filters = new ArrayList<>();
64  private static int resultWindowCount = 0; //keep track of result windows so they get unique names
65  private static MimeTypeFilter mimeTypeFilter = new MimeTypeFilter();
66  private static DataSourceFilter dataSourceFilter = new DataSourceFilter();
67  private static final String EMPTY_WHERE_CLAUSE = NbBundle.getMessage(DateSearchFilter.class, "FileSearchPanel.emptyWhereClause.text");
68  private static SwingWorker<TableFilterNode, Void> searchWorker = null;
69 
70  enum EVENT {
71  CHECKED
72  }
73 
77  FileSearchPanel() {
78  initComponents();
79  customizeComponents();
80  }
81 
85  private void customizeComponents() {
86 
87  JLabel label = new JLabel(NbBundle.getMessage(this.getClass(), "FileSearchPanel.custComp.label.text"));
88  label.setAlignmentX(Component.LEFT_ALIGNMENT);
89  label.setBorder(new EmptyBorder(0, 0, 10, 0));
90 
91  JPanel panel1 = new JPanel();
92  panel1.setLayout(new GridLayout(1, 2));
93  panel1.add(new JLabel(""));
94  JPanel panel2 = new JPanel();
95  panel2.setLayout(new GridLayout(1, 2, 20, 0));
96  JPanel panel3 = new JPanel();
97  panel3.setLayout(new GridLayout(1, 2, 20, 0));
98  JPanel panel4 = new JPanel();
99  panel4.setLayout(new GridLayout(1, 2, 20, 0));
100  JPanel panel5 = new JPanel();
101  panel5.setLayout(new GridLayout(1, 2, 20, 0));
102 
103  // Create and add filter areas
104  NameSearchFilter nameFilter = new NameSearchFilter();
105  SizeSearchFilter sizeFilter = new SizeSearchFilter();
106  DateSearchFilter dateFilter = new DateSearchFilter();
107  KnownStatusSearchFilter knowStatusFilter = new KnownStatusSearchFilter();
108  HashSearchFilter hashFilter = new HashSearchFilter();
109 
110  panel2.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.name"), nameFilter));
111 
112  panel3.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.metadata"), sizeFilter));
113 
114  panel2.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.metadata"), dateFilter));
115  panel3.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.knownStatus"), knowStatusFilter));
116 
117  panel5.add(new FilterArea(NbBundle.getMessage(this.getClass(), "HashSearchPanel.md5CheckBox.text"), hashFilter));
118  panel5.add(new JLabel(""));
119  panel4.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.metadata"), mimeTypeFilter));
120  panel4.add(new FilterArea(NbBundle.getMessage(this.getClass(), "DataSourcePanel.dataSourceCheckBox.text"), dataSourceFilter));
121  filterPanel.add(panel1);
122  filterPanel.add(panel2);
123  filterPanel.add(panel3);
124  filterPanel.add(panel4);
125  filterPanel.add(panel5);
126 
127  filters.add(nameFilter);
128  filters.add(sizeFilter);
129  filters.add(dateFilter);
130  filters.add(knowStatusFilter);
131  filters.add(hashFilter);
132  filters.add(mimeTypeFilter);
133  filters.add(dataSourceFilter);
134 
135  for (FileSearchFilter filter : this.getFilters()) {
136  filter.addPropertyChangeListener(new PropertyChangeListener() {
137  @Override
138  public void propertyChange(PropertyChangeEvent evt) {
139  searchButton.setEnabled(isValidSearch());
140  }
141  });
142  }
143  addListenerToAll(new ActionListener() {
144  @Override
145  public void actionPerformed(ActionEvent e) {
146  search();
147  }
148  });
149  searchButton.setEnabled(isValidSearch());
150  }
151 
155  private boolean isValidSearch() {
156  boolean enabled = false;
157  for (FileSearchFilter filter : this.getFilters()) {
158  if (filter.isEnabled()) {
159  enabled = true;
160  if (!filter.isValid()) {
161  errorLabel.setText(filter.getLastError());
162  return false;
163  }
164  }
165  }
166  errorLabel.setText("");
167  return enabled;
168  }
169 
174  @NbBundle.Messages({"FileSearchPanel.emptyNode.display.text=No results found.",
175  "FileSearchPanel.searchingNode.display.text=Performing file search by attributes. Please wait.",
176  "FileSearchPanel.searchingPath.text=File Search In Progress",
177  "FileSearchPanel.cancelledSearch.text=Search Was Cancelled"})
178  private void search() {
179  if (searchWorker != null && searchWorker.isDone()) {
180  searchWorker.cancel(true);
181  }
182  try {
183  if (this.isValidSearch()) {
184  // try to get the number of matches first
185  Case currentCase = Case.getCurrentCaseThrows(); // get the most updated case
186  Node emptyNode = new TableFilterNode(new EmptyNode(Bundle.FileSearchPanel_searchingNode_display_text()), true);
187  String title = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.title", ++resultWindowCount);
188  String pathText = Bundle.FileSearchPanel_searchingPath_text();
189  final DataResultTopComponent searchResultWin = DataResultTopComponent.createInstance(title, pathText,
190  emptyNode, 0);
191  searchResultWin.requestActive(); // make it the active top component
192  searchResultWin.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
193  searchWorker = new SwingWorker<TableFilterNode, Void>() {
194  List<AbstractFile> contentList = null;
195 
196  @Override
197  protected TableFilterNode doInBackground() throws Exception {
198  try {
199  SleuthkitCase tskDb = currentCase.getSleuthkitCase();
200  contentList = tskDb.findAllFilesWhere(getQuery());
201 
202  } catch (TskCoreException ex) {
203  Logger logger = Logger.getLogger(this.getClass().getName());
204  logger.log(Level.WARNING, "Error while trying to get the number of matches.", ex); //NON-NLS
205  }
206  if (contentList == null) {
207  contentList = Collections.<AbstractFile>emptyList();
208  }
209  if (contentList.isEmpty()) {
210  return new TableFilterNode(new EmptyNode(Bundle.FileSearchPanel_emptyNode_display_text()), true);
211  }
212  SearchNode sn = new SearchNode(contentList);
213  return new TableFilterNode(sn, true, sn.getName());
214  }
215 
216  @Override
217  protected void done() {
218  String pathText = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.pathText");
219  try {
220  TableFilterNode tableFilterNode = get();
221  if (tableFilterNode == null) { //just incase this get() gets modified to return null or somehow can return null
222  tableFilterNode = new TableFilterNode(new EmptyNode(Bundle.FileSearchPanel_emptyNode_display_text()), true);
223  }
224  if (searchResultWin != null && searchResultWin.isOpened()) {
225  searchResultWin.setNode(tableFilterNode);
226  searchResultWin.requestActive(); // make it the active top component
227  }
234  if (contentList.size() > 10000) {
235  // show info
236  String msg = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.msg", contentList.size());
237  String details = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.details");
238  MessageNotifyUtil.Notify.info(msg, details);
239  }
240  } catch (InterruptedException | ExecutionException ex) {
241  logger.log(Level.SEVERE, "Error while performing file search by attributes", ex);
242  } catch (CancellationException ex) {
243  if (searchResultWin != null && searchResultWin.isOpened()) {
244  Node emptyNode = new TableFilterNode(new EmptyNode(Bundle.FileSearchPanel_cancelledSearch_text()), true);
245  searchResultWin.setNode(emptyNode);
246  pathText = Bundle.FileSearchPanel_cancelledSearch_text();
247  }
248  logger.log(Level.INFO, "File search by attributes was cancelled", ex);
249  } finally {
250  if (searchResultWin != null && searchResultWin.isOpened()) {
251  searchResultWin.setPath(pathText);
252  searchResultWin.requestActive(); // make it the active top component
253  searchResultWin.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
254  }
255  }
256  }
257  };
258  if (searchResultWin != null && searchResultWin.isOpened()) {
259  searchResultWin.addPropertyChangeListener(new PropertyChangeListener() {
260  @Override
261  public void propertyChange(PropertyChangeEvent evt) {
262  if (evt.getPropertyName().equals("tcClosed") && !searchWorker.isDone() && evt.getOldValue() == null) {
263  searchWorker.cancel(true);
264  logger.log(Level.INFO, "User has closed the results window while search was in progress, search will be cancelled");
265  }
266  }
267  });
268  }
269  searchWorker.execute();
270  } else {
271  throw new FilterValidationException(
272  NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.exception.noFilterSelected.msg"));
273  }
274  } catch (FilterValidationException | NoCurrentCaseException ex) {
275  NotifyDescriptor d = new NotifyDescriptor.Message(
276  NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.validationErr.msg", ex.getMessage()));
277  DialogDisplayer.getDefault().notify(d);
278  }
279  }
280 
299  private String getQuery() throws FilterValidationException {
300 
301  //String query = "SELECT " + tempQuery + " FROM tsk_files WHERE ";
302  String query = "";
303  int i = 0;
304  for (FileSearchFilter f : this.getEnabledFilters()) {
305  String result = f.getPredicate();
306  if (!result.isEmpty()) {
307  if (i > 0) {
308  query += " AND (" + result + ")"; //NON-NLS
309  } else {
310  query += " (" + result + ")"; //NON-NLS
311  }
312  ++i;
313  }
314  }
315 
316  if (query.isEmpty()) {
317  throw new FilterValidationException(EMPTY_WHERE_CLAUSE);
318  }
319  return query;
320  }
321 
322  private Collection<FileSearchFilter> getFilters() {
323  return filters;
324  }
325 
326  private Collection<FileSearchFilter> getEnabledFilters() {
327  Collection<FileSearchFilter> enabledFilters = new ArrayList<>();
328 
329  for (FileSearchFilter f : this.getFilters()) {
330  if (f.isEnabled()) {
331  enabledFilters.add(f);
332  }
333  }
334 
335  return enabledFilters;
336  }
337 
342  void resetCaseDependentFilters() {
343  dataSourceFilter.resetDataSourceFilter();
344  mimeTypeFilter.resetMimeTypeFilter();
345  }
346 
347  void addListenerToAll(ActionListener l) {
348  searchButton.addActionListener(l);
349  for (FileSearchFilter fsf : getFilters()) {
350  fsf.addActionListener(l);
351  }
352  }
353 
359  @SuppressWarnings("unchecked")
360  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
361  private void initComponents() {
362 
363  filterPanel = new javax.swing.JPanel();
364  searchButton = new javax.swing.JButton();
365  errorLabel = new javax.swing.JLabel();
366 
367  setPreferredSize(new java.awt.Dimension(300, 300));
368 
369  filterPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
370  filterPanel.setPreferredSize(new java.awt.Dimension(300, 400));
371  filterPanel.setLayout(new javax.swing.BoxLayout(filterPanel, javax.swing.BoxLayout.Y_AXIS));
372 
373  searchButton.setText(org.openide.util.NbBundle.getMessage(FileSearchPanel.class, "FileSearchPanel.searchButton.text")); // NOI18N
374 
375  errorLabel.setText(org.openide.util.NbBundle.getMessage(FileSearchPanel.class, "FileSearchPanel.errorLabel.text")); // NOI18N
376  errorLabel.setForeground(new java.awt.Color(255, 51, 51));
377 
378  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
379  this.setLayout(layout);
380  layout.setHorizontalGroup(
381  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
382  .addComponent(filterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
383  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
384  .addContainerGap()
385  .addComponent(errorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
386  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
387  .addComponent(searchButton)
388  .addContainerGap())
389  );
390  layout.setVerticalGroup(
391  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
392  .addGroup(layout.createSequentialGroup()
393  .addComponent(filterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
394  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
395  .addComponent(searchButton)
396  .addComponent(errorLabel))
397  .addContainerGap())
398  );
399  }// </editor-fold>//GEN-END:initComponents
400 
401  // Variables declaration - do not modify//GEN-BEGIN:variables
402  private javax.swing.JLabel errorLabel;
403  private javax.swing.JPanel filterPanel;
404  private javax.swing.JButton searchButton;
405  // End of variables declaration//GEN-END:variables
406 }

Copyright © 2012-2021 Basis Technology. Generated on: Fri Aug 6 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.