Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileViewer.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2018-2019 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.contentviewers;
20 
21 import com.google.common.base.Strings;
22 import java.awt.Component;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.logging.Level;
26 import org.openide.nodes.Node;
27 import org.openide.util.NbBundle;
28 import org.openide.util.lookup.ServiceProvider;
32 import org.sleuthkit.datamodel.AbstractFile;
33 
37 @ServiceProvider(service = DataContentViewer.class, position = 3)
38 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
39 public class FileViewer extends javax.swing.JPanel implements DataContentViewer {
40 
41  private static final int CONFIDENCE_LEVEL = 5;
42  private static final long serialVersionUID = 1L;
43  private static final Logger LOGGER = Logger.getLogger(FileViewer.class.getName());
44 
45  private final Map<String, FileTypeViewer> mimeTypeToViewerMap = new HashMap<>();
46 
47  // TBD: This hardcoded list of viewers should be replaced with a dynamic lookup
48  private final FileTypeViewer[] KNOWN_VIEWERS = new FileTypeViewer[]{
49  new SQLiteViewer(),
50  new PListViewer(),
51  new MediaFileViewer(),
52  new HtmlViewer(),
53  new WindowsRegistryViewer()
54  };
55 
56  private FileTypeViewer lastViewer;
57 
61  public FileViewer() {
62 
63  // init the mimetype to viewer map
64  for (FileTypeViewer cv : KNOWN_VIEWERS) {
65  cv.getSupportedMIMETypes().forEach((mimeType) -> {
66  if (mimeTypeToViewerMap.containsKey(mimeType) == false) {
67  mimeTypeToViewerMap.put(mimeType, cv);
68  } else {
69  LOGGER.log(Level.WARNING, "Duplicate viewer for mimtype: {0}", mimeType); //NON-NLS
70  }
71  });
72  }
73 
74  initComponents();
75 
76  LOGGER.log(Level.INFO, "Created ApplicationContentViewer instance: {0}", this); //NON-NLS
77  }
78 
87  private FileTypeViewer getSupportingViewer(AbstractFile file) {
88  FileTypeViewer viewer = mimeTypeToViewerMap.get(file.getMIMEType());
89  if (viewer == null || viewer.isSupported(file)) {
90  return viewer;
91  }
92  return null;
93  }
94 
100  @SuppressWarnings("unchecked")
101  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
102  private void initComponents() {
103 
104  setLayout(new javax.swing.OverlayLayout(this));
105  }// </editor-fold>//GEN-END:initComponents
106 
107 
108  // Variables declaration - do not modify//GEN-BEGIN:variables
109  // End of variables declaration//GEN-END:variables
110  @Override
111  public void setNode(Node selectedNode) {
112 
113  resetComponent();
114 
115  if (selectedNode == null) {
116  return;
117  }
118 
119  AbstractFile file = selectedNode.getLookup().lookup(AbstractFile.class);
120  if ((file == null) || (file.isDir())) {
121  return;
122  }
123 
124  String mimeType = file.getMIMEType();
125  if (Strings.isNullOrEmpty(mimeType)) {
126  LOGGER.log(Level.INFO, "Mimetype not known for file: {0}", file.getName()); //NON-NLS
127  try {
128  FileTypeDetector fileTypeDetector = new FileTypeDetector();
129  mimeType = fileTypeDetector.getMIMEType(file);
131  LOGGER.log(Level.SEVERE, "Failed to initialize FileTypeDetector.", ex); //NON-NLS
132  return;
133  }
134  }
135 
136  if (mimeType.equalsIgnoreCase("application/octet-stream")) {
137  return;
138  } else {
139  FileTypeViewer viewer = getSupportingViewer(file);
140  if (viewer != null) {
141  lastViewer = viewer;
142 
143  viewer.setFile(file);
144  this.removeAll();
145  this.add(viewer.getComponent());
146  this.validate();
147  }
148  }
149 
150  }
151 
152  @Override
153  @NbBundle.Messages("ApplicationContentViewer.title=Application")
154  public String getTitle() {
155  return Bundle.ApplicationContentViewer_title();
156  }
157 
158  @Override
159  @NbBundle.Messages("ApplicationContentViewer.toolTip=Displays file contents.")
160  public String getToolTip() {
161  return Bundle.ApplicationContentViewer_toolTip();
162  }
163 
164  @Override
165  public DataContentViewer createInstance() {
166  return new FileViewer();
167  }
168 
169  @Override
170  public Component getComponent() {
171  return this;
172  }
173 
174  @Override
175  public void resetComponent() {
176 
177  if (lastViewer != null) {
178  lastViewer.resetComponent();
179  }
180  this.removeAll();
181  lastViewer = null;
182  }
183 
184  @Override
185  public boolean isSupported(Node node) {
186 
187  if (node == null) {
188  return false;
189  }
190 
191  AbstractFile aFile = node.getLookup().lookup(AbstractFile.class);
192  if ((aFile == null) || (aFile.isDir())) {
193  return false;
194  }
195 
196  String mimeType = aFile.getMIMEType();
197  if (Strings.isNullOrEmpty(mimeType)) {
198  LOGGER.log(Level.INFO, "Mimetype not known for file: {0}", aFile.getName()); //NON-NLS
199  try {
200  FileTypeDetector fileTypeDetector = new FileTypeDetector();
201  mimeType = fileTypeDetector.getMIMEType(aFile);
203  LOGGER.log(Level.SEVERE, "Failed to initialize FileTypeDetector.", ex); //NON-NLS
204  return false;
205  }
206  }
207 
208  if (mimeType.equalsIgnoreCase("application/octet-stream")) {
209  return false;
210  } else {
211  return (getSupportingViewer(aFile) != null);
212  }
213 
214  }
215 
216  @Override
217  public int isPreferred(Node node) {
218  AbstractFile file = node.getLookup().lookup(AbstractFile.class);
219  String mimeType = file.getMIMEType();
220 
221  if (Strings.isNullOrEmpty(mimeType)) {
222  LOGGER.log(Level.INFO, "Mimetype not known for file: {0}", file.getName()); //NON-NLS
223  try {
224  FileTypeDetector fileTypeDetector = new FileTypeDetector();
225  mimeType = fileTypeDetector.getMIMEType(file);
227  LOGGER.log(Level.SEVERE, "Failed to initialize FileTypeDetector.", ex); //NON-NLS
228  return 0;
229  }
230  }
231 
232  if (mimeType.equalsIgnoreCase("application/octet-stream")) {
233  return 0;
234  } else {
235  if (null != getSupportingViewer(file)) {
236  return CONFIDENCE_LEVEL;
237  }
238  }
239 
240  return 0;
241  }
242 }
FileTypeViewer getSupportingViewer(AbstractFile file)
Definition: FileViewer.java:87
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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