Autopsy  4.7.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-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.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.logging.Level;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.border.EmptyBorder;
36 import org.openide.DialogDisplayer;
37 import org.openide.NotifyDescriptor;
38 import org.openide.util.NbBundle;
39 import org.openide.windows.TopComponent;
47 import org.sleuthkit.datamodel.AbstractFile;
48 import org.sleuthkit.datamodel.SleuthkitCase;
49 import org.sleuthkit.datamodel.TskCoreException;
50 
54 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
55 class FileSearchPanel extends javax.swing.JPanel {
56 
57  private final List<FileSearchFilter> filters = new ArrayList<>();
58  private static int resultWindowCount = 0; //keep track of result windows so they get unique names
59  private static final String EMPTY_WHERE_CLAUSE = NbBundle.getMessage(DateSearchFilter.class, "FileSearchPanel.emptyWhereClause.text");
60 
61  enum EVENT {
62  CHECKED
63  }
64 
68  public FileSearchPanel() {
69  initComponents();
70  customizeComponents();
71 
72  }
73 
77  private void customizeComponents() {
78 
79  JLabel label = new JLabel(NbBundle.getMessage(this.getClass(), "FileSearchPanel.custComp.label.text"));
80  label.setAlignmentX(Component.LEFT_ALIGNMENT);
81  label.setBorder(new EmptyBorder(0, 0, 10, 0));
82 
83  JPanel panel1 = new JPanel();
84  panel1.setLayout(new GridLayout(1,2));
85  panel1.add(new JLabel(""));
86  JPanel panel2 = new JPanel();
87  panel2.setLayout(new GridLayout(1,2, 20, 0));
88  JPanel panel3 = new JPanel();
89  panel3.setLayout(new GridLayout(1,2, 20, 0));
90  JPanel panel4 = new JPanel();
91  panel4.setLayout(new GridLayout(1,2, 20, 0));
92  JPanel panel5 = new JPanel();
93  panel5.setLayout(new GridLayout(1,2, 20, 0));
94 
95  // Create and add filter areas
96  NameSearchFilter nameFilter = new NameSearchFilter();
97  SizeSearchFilter sizeFilter = new SizeSearchFilter();
98  DateSearchFilter dateFilter = new DateSearchFilter();
99  KnownStatusSearchFilter knowStatusFilter = new KnownStatusSearchFilter();
100  HashSearchFilter hashFilter = new HashSearchFilter();
101  MimeTypeFilter mimeTypeFilter = new MimeTypeFilter();
102  DataSourceFilter dataSourceFilter = new DataSourceFilter();
103 
104  panel2.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.name"),nameFilter));
105 
106  panel3.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.metadata"),sizeFilter));
107 
108  panel2.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.metadata"), dateFilter));
109  panel3.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.knownStatus"), knowStatusFilter));
110 
111  panel5.add(new FilterArea(NbBundle.getMessage(this.getClass(), "HashSearchPanel.md5CheckBox.text"), hashFilter));
112  panel5.add(new JLabel(""));
113  panel4.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.metadata"), mimeTypeFilter));
114  panel4.add(new FilterArea(NbBundle.getMessage(this.getClass(), "DataSourcePanel.dataSourceCheckBox.text"), dataSourceFilter));
115  filterPanel.add(panel1);
116  filterPanel.add(panel2);
117  filterPanel.add(panel3);
118  filterPanel.add(panel4);
119  filterPanel.add(panel5);
120 
121  filters.add(nameFilter);
122  filters.add(sizeFilter);
123  filters.add(dateFilter);
124  filters.add(knowStatusFilter);
125  filters.add(hashFilter);
126  filters.add(mimeTypeFilter);
127  filters.add(dataSourceFilter);
128 
129  for (FileSearchFilter filter : this.getFilters()) {
130  filter.addPropertyChangeListener(new PropertyChangeListener() {
131  @Override
132  public void propertyChange(PropertyChangeEvent evt) {
133  searchButton.setEnabled(isValidSearch());
134  }
135  });
136  }
137  addListenerToAll(new ActionListener() {
138  @Override
139  public void actionPerformed(ActionEvent e) {
140  search();
141  }
142  });
143  searchButton.setEnabled(isValidSearch());
144  }
145 
149  private boolean isValidSearch() {
150  boolean enabled = false;
151  for (FileSearchFilter filter : this.getFilters()) {
152  if (filter.isEnabled()) {
153  enabled = true;
154  if (!filter.isValid()) {
155  errorLabel.setText(filter.getLastError());
156  return false;
157  }
158  }
159  }
160 
161  errorLabel.setText("");
162  return enabled;
163  }
164 
169  private void search() {
170  // change the cursor to "waiting cursor" for this operation
171  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
172  try {
173  if (this.isValidSearch()) {
174  String title = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.title", ++resultWindowCount);
175  String pathText = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.pathText");
176 
177  // try to get the number of matches first
178  Case currentCase = Case.getCurrentCaseThrows(); // get the most updated case
179  long totalMatches = 0;
180  List<AbstractFile> contentList = null;
181  try {
182  SleuthkitCase tskDb = currentCase.getSleuthkitCase();
183  contentList = tskDb.findAllFilesWhere(this.getQuery());
184 
185  } catch (TskCoreException ex) {
186  Logger logger = Logger.getLogger(this.getClass().getName());
187  logger.log(Level.WARNING, "Error while trying to get the number of matches.", ex); //NON-NLS
188  }
189 
190  if (contentList == null) {
191  contentList = Collections.<AbstractFile>emptyList();
192  }
193 
194  SearchNode sn = new SearchNode(contentList);
195  final TopComponent searchResultWin = DataResultTopComponent.createInstance(title, pathText,
196  new TableFilterNode(sn, true, sn.getName()), contentList.size());
197 
198  searchResultWin.requestActive(); // make it the active top component
199 
205  if (totalMatches > 10000) {
206  // show info
207  String msg = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.msg", totalMatches);
208  String details = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.details");
209  MessageNotifyUtil.Notify.info(msg, details);
210  }
211  } else {
212  throw new FilterValidationException(
213  NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.exception.noFilterSelected.msg"));
214  }
215  } catch (FilterValidationException | NoCurrentCaseException ex) {
216  NotifyDescriptor d = new NotifyDescriptor.Message(
217  NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.validationErr.msg", ex.getMessage()));
218  DialogDisplayer.getDefault().notify(d);
219  } finally {
220  this.setCursor(null);
221  }
222  }
223 
242  private String getQuery() throws FilterValidationException {
243 
244  //String query = "SELECT " + tempQuery + " FROM tsk_files WHERE ";
245  String query = "";
246  int i = 0;
247  for (FileSearchFilter f : this.getEnabledFilters()) {
248  String result = f.getPredicate();
249  if (!result.isEmpty()) {
250  if (i > 0) {
251  query += " AND (" + result + ")"; //NON-NLS
252  } else {
253  query += " (" + result + ")"; //NON-NLS
254  }
255  ++i;
256  }
257  }
258 
259  if (query.isEmpty()) {
260  throw new FilterValidationException(EMPTY_WHERE_CLAUSE);
261  }
262  return query;
263  }
264 
265  private Collection<FileSearchFilter> getFilters() {
266  return filters;
267  }
268 
269  private Collection<FileSearchFilter> getEnabledFilters() {
270  Collection<FileSearchFilter> enabledFilters = new ArrayList<>();
271 
272  for (FileSearchFilter f : this.getFilters()) {
273  if (f.isEnabled()) {
274  enabledFilters.add(f);
275  }
276  }
277 
278  return enabledFilters;
279  }
280 
281  void addListenerToAll(ActionListener l) {
282  searchButton.addActionListener(l);
283  for (FileSearchFilter fsf : getFilters()) {
284  fsf.addActionListener(l);
285  }
286  }
287 
293  @SuppressWarnings("unchecked")
294  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
295  private void initComponents() {
296 
297  filterPanel = new javax.swing.JPanel();
298  searchButton = new javax.swing.JButton();
299  errorLabel = new javax.swing.JLabel();
300 
301  setPreferredSize(new java.awt.Dimension(300, 300));
302 
303  filterPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
304  filterPanel.setPreferredSize(new java.awt.Dimension(300, 400));
305  filterPanel.setLayout(new javax.swing.BoxLayout(filterPanel, javax.swing.BoxLayout.Y_AXIS));
306 
307  searchButton.setText(org.openide.util.NbBundle.getMessage(FileSearchPanel.class, "FileSearchPanel.searchButton.text")); // NOI18N
308 
309  errorLabel.setText(org.openide.util.NbBundle.getMessage(FileSearchPanel.class, "FileSearchPanel.errorLabel.text")); // NOI18N
310  errorLabel.setForeground(new java.awt.Color(255, 51, 51));
311 
312  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
313  this.setLayout(layout);
314  layout.setHorizontalGroup(
315  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
316  .addComponent(filterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
317  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
318  .addContainerGap()
319  .addComponent(errorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
320  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
321  .addComponent(searchButton)
322  .addContainerGap())
323  );
324  layout.setVerticalGroup(
325  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
326  .addGroup(layout.createSequentialGroup()
327  .addComponent(filterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
328  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
329  .addComponent(searchButton)
330  .addComponent(errorLabel))
331  .addContainerGap())
332  );
333  }// </editor-fold>//GEN-END:initComponents
334 
335  // Variables declaration - do not modify//GEN-BEGIN:variables
336  private javax.swing.JLabel errorLabel;
337  private javax.swing.JPanel filterPanel;
338  private javax.swing.JButton searchButton;
339  // End of variables declaration//GEN-END:variables
340 }

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