Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileTypeNode.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2014 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.datamodel;
20 
21 import java.util.List;
22 import java.util.Observable;
23 import java.util.Observer;
24 import java.util.logging.Level;
25 import org.openide.nodes.AbstractNode;
26 import org.openide.nodes.ChildFactory;
27 import org.openide.nodes.Children;
28 import org.openide.nodes.Node;
29 import org.openide.nodes.Sheet;
30 import org.openide.util.NbBundle;
31 import org.openide.util.lookup.Lookups;
45 
50 public class FileTypeNode extends DisplayableItemNode {
51 
52  FileTypeExtensionFilters.SearchFilterInterface filter;
53  SleuthkitCase skCase;
54 
55  // deprecated in favor of the version that takes an observable to provide refresh updates
56  @Deprecated
57  FileTypeNode(FileTypeExtensionFilters.SearchFilterInterface filter, SleuthkitCase skCase) {
58  super(Children.create(new FileTypeChildFactory(filter, skCase), true), Lookups.singleton(filter.getDisplayName()));
59  this.filter = filter;
60  this.skCase = skCase;
61  init();
62  }
63 
71  FileTypeNode(FileTypeExtensionFilters.SearchFilterInterface filter, SleuthkitCase skCase, Observable o) {
72  super(Children.create(new FileTypeChildFactory(filter, skCase, o), true), Lookups.singleton(filter.getDisplayName()));
73  this.filter = filter;
74  this.skCase = skCase;
75  init();
76  o.addObserver(new FileTypeNodeObserver());
77  }
78 
79  private void init() {
80  super.setName(filter.getName());
82  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/file-filter-icon.png"); //NON-NLS
83  }
84 
85  // update the display name when new events are fired
86  private class FileTypeNodeObserver implements Observer {
87 
88  @Override
89  public void update(Observable o, Object arg) {
91  }
92  }
93 
94  private void updateDisplayName() {
95  final long count = FileTypeChildFactory.calculateItems(skCase, filter);
96  super.setDisplayName(filter.getDisplayName() + " (" + count + ")");
97  }
98 
99  @Override
100  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
101  return v.visit(this);
102  }
103 
104  @Override
105  protected Sheet createSheet() {
106  Sheet s = super.createSheet();
107  Sheet.Set ss = s.get(Sheet.PROPERTIES);
108  if (ss == null) {
109  ss = Sheet.createPropertiesSet();
110  s.put(ss);
111  }
112 
113  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "FileTypeNode.createSheet.filterType.name"),
114  NbBundle.getMessage(this.getClass(), "FileTypeNode.createSheet.filterType.displayName"),
115  NbBundle.getMessage(this.getClass(), "FileTypeNode.createSheet.filterType.desc"),
116  filter.getDisplayName()));
117  String extensions = "";
118  for (String ext : filter.getFilter()) {
119  extensions += "'" + ext + "', ";
120  }
121  extensions = extensions.substring(0, extensions.lastIndexOf(','));
122  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "FileTypeNode.createSheet.fileExt.name"),
123  NbBundle.getMessage(this.getClass(), "FileTypeNode.createSheet.fileExt.displayName"),
124  NbBundle.getMessage(this.getClass(), "FileTypeNode.createSheet.fileExt.desc"),
125  extensions));
126 
127  return s;
128  }
129 
130  @Override
131  public boolean isLeafTypeNode() {
132  return true;
133  }
134 
138  public static class FileTypeChildFactory extends ChildFactory.Detachable<Content> {
139 
140  private final SleuthkitCase skCase;
141  private final FileTypeExtensionFilters.SearchFilterInterface filter;
142  private final static Logger logger = Logger.getLogger(FileTypeChildFactory.class.getName());
143  private Observable notifier;
144 
145  // use the constructor that gets an observable passed in for updates
146  @Deprecated
147  FileTypeChildFactory(FileTypeExtensionFilters.SearchFilterInterface filter, SleuthkitCase skCase) {
148  super();
149  this.filter = filter;
150  this.skCase = skCase;
151  notifier = null;
152  }
153 
161  FileTypeChildFactory(FileTypeExtensionFilters.SearchFilterInterface filter, SleuthkitCase skCase, Observable o) {
162  super();
163  this.filter = filter;
164  this.skCase = skCase;
165  notifier = o;
166  }
167 
168  @Override
169  protected void addNotify() {
170  if (notifier != null) {
171  notifier.addObserver(observer);
172  }
173  }
174 
175  @Override
176  protected void removeNotify() {
177  if (notifier != null) {
178  notifier.deleteObserver(observer);
179  }
180  }
181  private final Observer observer = new FileTypeChildFactoryObserver();
182 
183  // Cause refresh of children if there are changes
184  private class FileTypeChildFactoryObserver implements Observer {
185 
186  @Override
187  public void update(Observable o, Object arg) {
188  refresh(true);
189  }
190  }
191 
197  static long calculateItems(SleuthkitCase sleuthkitCase, FileTypeExtensionFilters.SearchFilterInterface filter) {
198  try {
199  return sleuthkitCase.countFilesWhere(createQuery(filter));
200  } catch (TskCoreException ex) {
201  logger.log(Level.SEVERE, "Error getting file search view count", ex); //NON-NLS
202  return 0;
203  }
204  }
205 
206  @Override
207  protected boolean createKeys(List<Content> list) {
208  try {
209  List<AbstractFile> files = skCase.findAllFilesWhere(createQuery(filter));
210  list.addAll(files);
211  } catch (TskCoreException ex) {
212  logger.log(Level.SEVERE, "Couldn't get search results", ex); //NON-NLS
213  }
214  return true;
215  }
216 
217  private static String createQuery(FileTypeExtensionFilters.SearchFilterInterface filter) {
218  StringBuilder query = new StringBuilder();
219  query.append("(dir_type = ").append(TskData.TSK_FS_NAME_TYPE_ENUM.REG.getValue()).append(")"); //NON-NLS
221  query.append(" AND (known IS NULL OR known != ").append(TskData.FileKnown.KNOWN.getFileKnownValue()).append(")"); //NON-NLS
222  }
223  query.append(" AND (0"); //NON-NLS
224  for (String s : filter.getFilter()) {
225  query.append(" OR name LIKE '%").append(s).append("'"); //NON-NLS
226  }
227  query.append(')');
228  return query.toString();
229  }
230 
231  @Override
232  protected Node createNodeForKey(Content key) {
233  return key.accept(new ContentVisitor.Default<AbstractNode>() {
234  @Override
235  public FileNode visit(File f) {
236  return new FileNode(f, false);
237  }
238 
239  @Override
240  public DirectoryNode visit(Directory d) {
241  return new DirectoryNode(d);
242  }
243 
244  @Override
245  public LayoutFileNode visit(LayoutFile lf) {
246  return new LayoutFileNode(lf);
247  }
248 
249  @Override
250  public LocalFileNode visit(DerivedFile df) {
251  return new LocalFileNode(df);
252  }
253 
254  @Override
255  public LocalFileNode visit(LocalFile lf) {
256  return new LocalFileNode(lf);
257  }
258 
259  @Override
260  protected AbstractNode defaultVisit(Content di) {
261  throw new UnsupportedOperationException(NbBundle.getMessage(this.getClass(), "FileTypeChildren.exception.notSupported.msg", di.toString()));
262  }
263  });
264  }
265  }
266 }
long countFilesWhere(String sqlWhereClause)
static String createQuery(FileTypeExtensionFilters.SearchFilterInterface filter)
List< AbstractFile > findAllFilesWhere(String sqlWhereClause)
final FileTypeExtensionFilters.SearchFilterInterface filter
public< T > T accept(ContentVisitor< T > v)
static Logger getLogger(String name)
Definition: Logger.java:131

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