Autopsy  4.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 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.BorderLayout;
28 import java.awt.Component;
29 import java.awt.Cursor;
30 import java.awt.Dimension;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.logging.Level;
38 
39 import org.openide.util.NbBundle;
41 import javax.swing.BoxLayout;
42 import javax.swing.JButton;
43 import javax.swing.JLabel;
44 import javax.swing.JPanel;
45 import javax.swing.border.EmptyBorder;
46 import org.openide.DialogDisplayer;
47 import org.openide.NotifyDescriptor;
48 import org.openide.windows.TopComponent;
54 import org.sleuthkit.datamodel.AbstractFile;
55 import org.sleuthkit.datamodel.SleuthkitCase;
56 import org.sleuthkit.datamodel.TskCoreException;
57 
61 class FileSearchPanel extends javax.swing.JPanel {
62 
63  private final List<FilterArea> filterAreas = new ArrayList<>();
64  private static int resultWindowCount = 0; //keep track of result windows so they get unique names
65  private static final String EMPTY_WHERE_CLAUSE = NbBundle.getMessage(DateSearchFilter.class, "FileSearchPanel.emptyWhereClause.text");
66 
70  public FileSearchPanel() {
71  initComponents();
72  customizeComponents();
73 
74  }
75 
79  private void customizeComponents() {
80 
81  JLabel label = new JLabel(NbBundle.getMessage(this.getClass(), "FileSearchPanel.custComp.label.text"));
82  label.setAlignmentX(Component.LEFT_ALIGNMENT);
83  label.setBorder(new EmptyBorder(0, 0, 10, 0));
84  filterPanel.add(label);
85 
86  // Create and add filter areas
87  this.filterAreas.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.name"), new NameSearchFilter()));
88 
89  List<FileSearchFilter> metadataFilters = new ArrayList<>();
90  metadataFilters.add(new SizeSearchFilter());
91  metadataFilters.add(new MimeTypeFilter());
92  metadataFilters.add(new DateSearchFilter());
93  this.filterAreas.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.metadata"), metadataFilters));
94 
95  this.filterAreas.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.knownStatus"), new KnownStatusSearchFilter()));
96 
97  for (FilterArea fa : this.filterAreas) {
98  fa.setMaximumSize(new Dimension(Integer.MAX_VALUE, fa.getMinimumSize().height));
99  fa.setAlignmentX(Component.LEFT_ALIGNMENT);
100  filterPanel.add(fa);
101  }
102 
103  addListenerToAll(new ActionListener() {
104  @Override
105  public void actionPerformed(ActionEvent e) {
106  search();
107  }
108  });
109  }
110 
114  private boolean anyFiltersEnabled() {
115  for (FileSearchFilter filter : this.getFilters()) {
116  if (filter.isEnabled()) {
117  return true;
118  }
119  }
120 
121  return false;
122  }
123 
128  private void search() {
129  // change the cursor to "waiting cursor" for this operation
130  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
131  try {
132  if (this.anyFiltersEnabled()) {
133  String title = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.title", ++resultWindowCount);
134  String pathText = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.pathText");
135 
136  // try to get the number of matches first
137  Case currentCase = Case.getCurrentCase(); // get the most updated case
138  long totalMatches = 0;
139  List<AbstractFile> contentList = null;
140  try {
141  SleuthkitCase tskDb = currentCase.getSleuthkitCase();
142  contentList = tskDb.findAllFilesWhere(this.getQuery());
143 
144  } catch (TskCoreException ex) {
145  Logger logger = Logger.getLogger(this.getClass().getName());
146  logger.log(Level.WARNING, "Error while trying to get the number of matches.", ex); //NON-NLS
147  }
148 
149  if (contentList == null) {
150  contentList = Collections.<AbstractFile>emptyList();
151  }
152 
153  final TopComponent searchResultWin = DataResultTopComponent.createInstance(title, pathText,
154  new TableFilterNode(new SearchNode(contentList), true), contentList.size());
155 
156  searchResultWin.requestActive(); // make it the active top component
157 
163  if (totalMatches > 10000) {
164  // show info
165  String msg = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.msg", totalMatches);
166  String details = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.details");
167  MessageNotifyUtil.Notify.info(msg, details);
168  }
169  } else {
170  throw new FilterValidationException(
171  NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.exception.noFilterSelected.msg"));
172  }
173  } catch (FilterValidationException ex) {
174  NotifyDescriptor d = new NotifyDescriptor.Message(
175  NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.validationErr.msg", ex.getMessage()));
176  DialogDisplayer.getDefault().notify(d);
177  } finally {
178  this.setCursor(null);
179  }
180  }
181 
200  private String getQuery() throws FilterValidationException {
201 
202  //String query = "SELECT " + tempQuery + " FROM tsk_files WHERE ";
203  String query = "";
204  int i = 0;
205  for (FileSearchFilter f : this.getEnabledFilters()) {
206  String result = f.getPredicate();
207  if (!result.isEmpty()) {
208  if (i > 0) {
209  query += " AND (" + result + ")"; //NON-NLS
210  } else {
211  query += " (" + result + ")"; //NON-NLS
212  }
213  ++i;
214  }
215  }
216 
217  if (query.isEmpty()) {
218  throw new FilterValidationException(EMPTY_WHERE_CLAUSE);
219  }
220  return query;
221  }
222 
223  private Collection<FileSearchFilter> getFilters() {
224  Collection<FileSearchFilter> filters = new ArrayList<>();
225 
226  for (FilterArea fa : this.filterAreas) {
227  filters.addAll(fa.getFilters());
228  }
229 
230  return filters;
231  }
232 
233  private Collection<FileSearchFilter> getEnabledFilters() {
234  Collection<FileSearchFilter> enabledFilters = new ArrayList<>();
235 
236  for (FileSearchFilter f : this.getFilters()) {
237  if (f.isEnabled()) {
238  enabledFilters.add(f);
239  }
240  }
241 
242  return enabledFilters;
243  }
244 
245  void addListenerToAll(ActionListener l) {
246  searchButton.addActionListener(l);
247  for (FilterArea fa : this.filterAreas) {
248  for (FileSearchFilter fsf : fa.getFilters()) {
249  fsf.addActionListener(l);
250  }
251  }
252  }
253 
259  @SuppressWarnings("unchecked")
260  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
261  private void initComponents() {
262 
263  filterPanel = new javax.swing.JPanel();
264  searchButton = new javax.swing.JButton();
265 
266  setPreferredSize(new java.awt.Dimension(300, 300));
267 
268  filterPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
269  filterPanel.setPreferredSize(new java.awt.Dimension(300, 400));
270  filterPanel.setLayout(new javax.swing.BoxLayout(filterPanel, javax.swing.BoxLayout.Y_AXIS));
271 
272  searchButton.setText(org.openide.util.NbBundle.getMessage(FileSearchPanel.class, "FileSearchPanel.searchButton.text")); // NOI18N
273 
274  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
275  this.setLayout(layout);
276  layout.setHorizontalGroup(
277  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
278  .addComponent(filterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
279  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
280  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
281  .addComponent(searchButton)
282  .addContainerGap())
283  );
284  layout.setVerticalGroup(
285  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
286  .addGroup(layout.createSequentialGroup()
287  .addComponent(filterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
288  .addGap(0, 0, 0)
289  .addComponent(searchButton)
290  .addContainerGap())
291  );
292  }// </editor-fold>//GEN-END:initComponents
293  // Variables declaration - do not modify//GEN-BEGIN:variables
294  private javax.swing.JPanel filterPanel;
295  private javax.swing.JButton searchButton;
296  // End of variables declaration//GEN-END:variables
297 }

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.