Autopsy  4.4.1
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 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 
20  /*
21  * FileSearchPanel.java
22  *
23  * Created on Mar 5, 2012, 1:51:50 PM
24  */
25 package org.sleuthkit.autopsy.filesearch;
26 
27 import java.awt.Component;
28 import java.awt.Cursor;
29 import java.awt.Dimension;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.beans.PropertyChangeEvent;
33 import java.beans.PropertyChangeListener;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.logging.Level;
39 import javax.swing.JLabel;
40 import javax.swing.border.EmptyBorder;
41 import org.openide.DialogDisplayer;
42 import org.openide.NotifyDescriptor;
43 import org.openide.util.NbBundle;
44 import org.openide.windows.TopComponent;
51 import org.sleuthkit.datamodel.AbstractFile;
52 import org.sleuthkit.datamodel.SleuthkitCase;
53 import org.sleuthkit.datamodel.TskCoreException;
54 
58 class FileSearchPanel extends javax.swing.JPanel {
59 
60  private final List<FilterArea> filterAreas = new ArrayList<>();
61  private static int resultWindowCount = 0; //keep track of result windows so they get unique names
62  private static final String EMPTY_WHERE_CLAUSE = NbBundle.getMessage(DateSearchFilter.class, "FileSearchPanel.emptyWhereClause.text");
63 
64  enum EVENT {
65  CHECKED
66  }
67 
71  public FileSearchPanel() {
72  initComponents();
73  customizeComponents();
74 
75  }
76 
80  private void customizeComponents() {
81 
82  JLabel label = new JLabel(NbBundle.getMessage(this.getClass(), "FileSearchPanel.custComp.label.text"));
83  label.setAlignmentX(Component.LEFT_ALIGNMENT);
84  label.setBorder(new EmptyBorder(0, 0, 10, 0));
85  filterPanel.add(label);
86 
87  // Create and add filter areas
88  this.filterAreas.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.name"), new NameSearchFilter()));
89 
90  List<FileSearchFilter> metadataFilters = new ArrayList<>();
91  metadataFilters.add(new SizeSearchFilter());
92  metadataFilters.add(new MimeTypeFilter());
93  metadataFilters.add(new DateSearchFilter());
94  this.filterAreas.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.metadata"), metadataFilters));
95 
96  this.filterAreas.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.knownStatus"), new KnownStatusSearchFilter()));
97 
98  this.filterAreas.add(new FilterArea(NbBundle.getMessage(this.getClass(), "HashSearchPanel.md5CheckBox.text"), new HashSearchFilter()));
99 
100  for (FilterArea fa : this.filterAreas) {
101  fa.setMaximumSize(new Dimension(Integer.MAX_VALUE, fa.getMinimumSize().height));
102  fa.setAlignmentX(Component.LEFT_ALIGNMENT);
103  filterPanel.add(fa);
104  }
105 
106  for (FileSearchFilter filter : this.getFilters()) {
107  filter.addPropertyChangeListener(new PropertyChangeListener() {
108  @Override
109  public void propertyChange(PropertyChangeEvent evt) {
110  searchButton.setEnabled(isValidSearch());
111  }
112  });
113  }
114 
115  addListenerToAll(new ActionListener() {
116  @Override
117  public void actionPerformed(ActionEvent e) {
118  search();
119  }
120  });
121  searchButton.setEnabled(isValidSearch());
122  }
123 
127  private boolean isValidSearch() {
128  boolean enabled = false;
129  for (FileSearchFilter filter : this.getFilters()) {
130  if (filter.isEnabled()) {
131  enabled = true;
132  if (!filter.isValid()) {
133  return false;
134  }
135  }
136  }
137 
138  return enabled;
139  }
140 
145  private void search() {
146  // change the cursor to "waiting cursor" for this operation
147  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
148  try {
149  if (this.isValidSearch()) {
150  String title = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.title", ++resultWindowCount);
151  String pathText = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.pathText");
152 
153  // try to get the number of matches first
154  Case currentCase = Case.getCurrentCase(); // get the most updated case
155  long totalMatches = 0;
156  List<AbstractFile> contentList = null;
157  try {
158  SleuthkitCase tskDb = currentCase.getSleuthkitCase();
159  contentList = tskDb.findAllFilesWhere(this.getQuery());
160 
161  } catch (TskCoreException ex) {
162  Logger logger = Logger.getLogger(this.getClass().getName());
163  logger.log(Level.WARNING, "Error while trying to get the number of matches.", ex); //NON-NLS
164  }
165 
166  if (contentList == null) {
167  contentList = Collections.<AbstractFile>emptyList();
168  }
169 
170  SearchNode sn = new SearchNode(contentList);
171  final TopComponent searchResultWin = DataResultTopComponent.createInstance(title, pathText,
172  new TableFilterNode(sn, true, sn.getName()), contentList.size());
173 
174  searchResultWin.requestActive(); // make it the active top component
175 
181  if (totalMatches > 10000) {
182  // show info
183  String msg = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.msg", totalMatches);
184  String details = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.details");
185  MessageNotifyUtil.Notify.info(msg, details);
186  }
187  } else {
188  throw new FilterValidationException(
189  NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.exception.noFilterSelected.msg"));
190  }
191  } catch (FilterValidationException ex) {
192  NotifyDescriptor d = new NotifyDescriptor.Message(
193  NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.validationErr.msg", ex.getMessage()));
194  DialogDisplayer.getDefault().notify(d);
195  } finally {
196  this.setCursor(null);
197  }
198  }
199 
218  private String getQuery() throws FilterValidationException {
219 
220  //String query = "SELECT " + tempQuery + " FROM tsk_files WHERE ";
221  String query = "";
222  int i = 0;
223  for (FileSearchFilter f : this.getEnabledFilters()) {
224  String result = f.getPredicate();
225  if (!result.isEmpty()) {
226  if (i > 0) {
227  query += " AND (" + result + ")"; //NON-NLS
228  } else {
229  query += " (" + result + ")"; //NON-NLS
230  }
231  ++i;
232  }
233  }
234 
235  if (query.isEmpty()) {
236  throw new FilterValidationException(EMPTY_WHERE_CLAUSE);
237  }
238  return query;
239  }
240 
241  private Collection<FileSearchFilter> getFilters() {
242  Collection<FileSearchFilter> filters = new ArrayList<>();
243 
244  for (FilterArea fa : this.filterAreas) {
245  filters.addAll(fa.getFilters());
246  }
247 
248  return filters;
249  }
250 
251  private Collection<FileSearchFilter> getEnabledFilters() {
252  Collection<FileSearchFilter> enabledFilters = new ArrayList<>();
253 
254  for (FileSearchFilter f : this.getFilters()) {
255  if (f.isEnabled()) {
256  enabledFilters.add(f);
257  }
258  }
259 
260  return enabledFilters;
261  }
262 
263  void addListenerToAll(ActionListener l) {
264  searchButton.addActionListener(l);
265  for (FilterArea fa : this.filterAreas) {
266  for (FileSearchFilter fsf : fa.getFilters()) {
267  fsf.addActionListener(l);
268  }
269  }
270  }
271 
277  @SuppressWarnings("unchecked")
278  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
279  private void initComponents() {
280 
281  filterPanel = new javax.swing.JPanel();
282  searchButton = new javax.swing.JButton();
283 
284  setPreferredSize(new java.awt.Dimension(300, 300));
285 
286  filterPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
287  filterPanel.setPreferredSize(new java.awt.Dimension(300, 400));
288  filterPanel.setLayout(new javax.swing.BoxLayout(filterPanel, javax.swing.BoxLayout.Y_AXIS));
289 
290  searchButton.setText(org.openide.util.NbBundle.getMessage(FileSearchPanel.class, "FileSearchPanel.searchButton.text")); // NOI18N
291 
292  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
293  this.setLayout(layout);
294  layout.setHorizontalGroup(
295  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
296  .addComponent(filterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
297  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
298  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
299  .addComponent(searchButton)
300  .addContainerGap())
301  );
302  layout.setVerticalGroup(
303  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
304  .addGroup(layout.createSequentialGroup()
305  .addComponent(filterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
306  .addGap(0, 0, 0)
307  .addComponent(searchButton)
308  .addContainerGap())
309  );
310  }// </editor-fold>//GEN-END:initComponents
311 
312  // Variables declaration - do not modify//GEN-BEGIN:variables
313  private javax.swing.JPanel filterPanel;
314  private javax.swing.JButton searchButton;
315  // End of variables declaration//GEN-END:variables
316 }

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