Autopsy  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  for (FilterArea fa : this.filterAreas) {
99  fa.setMaximumSize(new Dimension(Integer.MAX_VALUE, fa.getMinimumSize().height));
100  fa.setAlignmentX(Component.LEFT_ALIGNMENT);
101  filterPanel.add(fa);
102  }
103 
104  for (FileSearchFilter filter : this.getFilters()) {
105  filter.addPropertyChangeListener(new PropertyChangeListener() {
106  @Override
107  public void propertyChange(PropertyChangeEvent evt) {
108  searchButton.setEnabled(isValidSearch());
109  }
110  });
111  }
112 
113  addListenerToAll(new ActionListener() {
114  @Override
115  public void actionPerformed(ActionEvent e) {
116  search();
117  }
118  });
119  searchButton.setEnabled(isValidSearch());
120  }
121 
125  private boolean isValidSearch() {
126  boolean enabled = false;
127  for (FileSearchFilter filter : this.getFilters()) {
128  if (filter.isEnabled()) {
129  enabled = true;
130  if (!filter.isValid()) {
131  return false;
132  }
133  }
134  }
135 
136  return enabled;
137  }
138 
143  private void search() {
144  // change the cursor to "waiting cursor" for this operation
145  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
146  try {
147  if (this.isValidSearch()) {
148  String title = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.title", ++resultWindowCount);
149  String pathText = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.pathText");
150 
151  // try to get the number of matches first
152  Case currentCase = Case.getCurrentCase(); // get the most updated case
153  long totalMatches = 0;
154  List<AbstractFile> contentList = null;
155  try {
156  SleuthkitCase tskDb = currentCase.getSleuthkitCase();
157  contentList = tskDb.findAllFilesWhere(this.getQuery());
158 
159  } catch (TskCoreException ex) {
160  Logger logger = Logger.getLogger(this.getClass().getName());
161  logger.log(Level.WARNING, "Error while trying to get the number of matches.", ex); //NON-NLS
162  }
163 
164  if (contentList == null) {
165  contentList = Collections.<AbstractFile>emptyList();
166  }
167 
168  final TopComponent searchResultWin = DataResultTopComponent.createInstance(title, pathText,
169  new TableFilterNode(new SearchNode(contentList), true), contentList.size());
170 
171  searchResultWin.requestActive(); // make it the active top component
172 
178  if (totalMatches > 10000) {
179  // show info
180  String msg = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.msg", totalMatches);
181  String details = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.details");
182  MessageNotifyUtil.Notify.info(msg, details);
183  }
184  } else {
185  throw new FilterValidationException(
186  NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.exception.noFilterSelected.msg"));
187  }
188  } catch (FilterValidationException ex) {
189  NotifyDescriptor d = new NotifyDescriptor.Message(
190  NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.validationErr.msg", ex.getMessage()));
191  DialogDisplayer.getDefault().notify(d);
192  } finally {
193  this.setCursor(null);
194  }
195  }
196 
215  private String getQuery() throws FilterValidationException {
216 
217  //String query = "SELECT " + tempQuery + " FROM tsk_files WHERE ";
218  String query = "";
219  int i = 0;
220  for (FileSearchFilter f : this.getEnabledFilters()) {
221  String result = f.getPredicate();
222  if (!result.isEmpty()) {
223  if (i > 0) {
224  query += " AND (" + result + ")"; //NON-NLS
225  } else {
226  query += " (" + result + ")"; //NON-NLS
227  }
228  ++i;
229  }
230  }
231 
232  if (query.isEmpty()) {
233  throw new FilterValidationException(EMPTY_WHERE_CLAUSE);
234  }
235  return query;
236  }
237 
238  private Collection<FileSearchFilter> getFilters() {
239  Collection<FileSearchFilter> filters = new ArrayList<>();
240 
241  for (FilterArea fa : this.filterAreas) {
242  filters.addAll(fa.getFilters());
243  }
244 
245  return filters;
246  }
247 
248  private Collection<FileSearchFilter> getEnabledFilters() {
249  Collection<FileSearchFilter> enabledFilters = new ArrayList<>();
250 
251  for (FileSearchFilter f : this.getFilters()) {
252  if (f.isEnabled()) {
253  enabledFilters.add(f);
254  }
255  }
256 
257  return enabledFilters;
258  }
259 
260  void addListenerToAll(ActionListener l) {
261  searchButton.addActionListener(l);
262  for (FilterArea fa : this.filterAreas) {
263  for (FileSearchFilter fsf : fa.getFilters()) {
264  fsf.addActionListener(l);
265  }
266  }
267  }
268 
274  @SuppressWarnings("unchecked")
275  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
276  private void initComponents() {
277 
278  filterPanel = new javax.swing.JPanel();
279  searchButton = new javax.swing.JButton();
280 
281  setPreferredSize(new java.awt.Dimension(300, 300));
282 
283  filterPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
284  filterPanel.setPreferredSize(new java.awt.Dimension(300, 400));
285  filterPanel.setLayout(new javax.swing.BoxLayout(filterPanel, javax.swing.BoxLayout.Y_AXIS));
286 
287  searchButton.setText(org.openide.util.NbBundle.getMessage(FileSearchPanel.class, "FileSearchPanel.searchButton.text")); // NOI18N
288 
289  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
290  this.setLayout(layout);
291  layout.setHorizontalGroup(
292  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
293  .addComponent(filterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
294  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
295  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
296  .addComponent(searchButton)
297  .addContainerGap())
298  );
299  layout.setVerticalGroup(
300  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
301  .addGroup(layout.createSequentialGroup()
302  .addComponent(filterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
303  .addGap(0, 0, 0)
304  .addComponent(searchButton)
305  .addContainerGap())
306  );
307  }// </editor-fold>//GEN-END:initComponents
308 
309  // Variables declaration - do not modify//GEN-BEGIN:variables
310  private javax.swing.JPanel filterPanel;
311  private javax.swing.JButton searchButton;
312  // End of variables declaration//GEN-END:variables
313 }

Copyright © 2012-2016 Basis Technology. Generated on: Tue Oct 25 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.