Autopsy  4.20.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;
33 import org.sleuthkit.datamodel.AbstractFile;
34 
38 @ServiceProvider(service = DataContentViewer.class, position = 3)
39 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
40 public class FileViewer extends javax.swing.JPanel implements DataContentViewer {
41 
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  new PDFViewer()
55  };
56 
57  private FileTypeViewer lastViewer;
58 
62  public FileViewer() {
63 
64  // init the mimetype to viewer map
65  for (FileTypeViewer cv : KNOWN_VIEWERS) {
66  cv.getSupportedMIMETypes().forEach((mimeType) -> {
67  if (mimeTypeToViewerMap.containsKey(mimeType) == false) {
68  mimeTypeToViewerMap.put(mimeType, cv);
69  } else {
70  LOGGER.log(Level.WARNING, "Duplicate viewer for mimtype: {0}", mimeType); //NON-NLS
71  }
72  });
73  }
74 
75  initComponents();
76 
77  LOGGER.log(Level.INFO, "Created ApplicationContentViewer instance: {0}", this); //NON-NLS
78  }
79 
88  private FileTypeViewer getSupportingViewer(AbstractFile file) {
89  FileTypeViewer viewer = mimeTypeToViewerMap.get(file.getMIMEType());
90  if (viewer == null || viewer.isSupported(file)) {
91  return viewer;
92  }
93  return null;
94  }
95 
101  @SuppressWarnings("unchecked")
102  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
103  private void initComponents() {
104 
105  setLayout(new javax.swing.OverlayLayout(this));
106  }// </editor-fold>//GEN-END:initComponents
107 
108 
109  // Variables declaration - do not modify//GEN-BEGIN:variables
110  // End of variables declaration//GEN-END:variables
111  @Override
112  public void setNode(Node selectedNode) {
113 
114  resetComponent();
115 
116  if (selectedNode == null) {
117  return;
118  }
119 
120  AbstractFile file = selectedNode.getLookup().lookup(AbstractFile.class);
121  if ((file == null) || (file.isDir())) {
122  return;
123  }
124 
125  String mimeType = file.getMIMEType();
126  if (Strings.isNullOrEmpty(mimeType)) {
127  LOGGER.log(Level.INFO, "Mimetype not known for file: {0}", file.getName()); //NON-NLS
128  try {
129  FileTypeDetector fileTypeDetector = new FileTypeDetector();
130  mimeType = fileTypeDetector.getMIMEType(file);
132  LOGGER.log(Level.SEVERE, "Failed to initialize FileTypeDetector.", ex); //NON-NLS
133  return;
134  }
135  }
136 
137  if (mimeType.equalsIgnoreCase("application/octet-stream")) {
138  return;
139  } else {
140  FileTypeViewer viewer = getSupportingViewer(file);
141  if (viewer != null) {
142  lastViewer = viewer;
143 
144  viewer.setFile(file);
145  this.removeAll();
146  this.add(viewer.getComponent());
147  this.validate();
148  }
149  }
150 
151  }
152 
153  @Override
154  @NbBundle.Messages("ApplicationContentViewer.title=Application")
155  public String getTitle() {
156  return Bundle.ApplicationContentViewer_title();
157  }
158 
159  @Override
160  @NbBundle.Messages("ApplicationContentViewer.toolTip=Displays file contents.")
161  public String getToolTip() {
162  return Bundle.ApplicationContentViewer_toolTip();
163  }
164 
165  @Override
166  public DataContentViewer createInstance() {
167  return new FileViewer();
168  }
169 
170  @Override
171  public Component getComponent() {
172  return this;
173  }
174 
175  @Override
176  public void resetComponent() {
177 
178  if (lastViewer != null) {
179  lastViewer.resetComponent();
180  }
181  this.removeAll();
182  lastViewer = null;
183  }
184 
185  @Override
186  public boolean isSupported(Node node) {
187 
188  if (node == null) {
189  return false;
190  }
191 
192  AbstractFile aFile = node.getLookup().lookup(AbstractFile.class);
193  if ((aFile == null) || (aFile.isDir())) {
194  return false;
195  }
196 
197  String mimeType = aFile.getMIMEType();
198  if (Strings.isNullOrEmpty(mimeType)) {
199  LOGGER.log(Level.INFO, "Mimetype not known for file: {0}", aFile.getName()); //NON-NLS
200  try {
201  FileTypeDetector fileTypeDetector = new FileTypeDetector();
202  mimeType = fileTypeDetector.getMIMEType(aFile);
204  LOGGER.log(Level.SEVERE, "Failed to initialize FileTypeDetector.", ex); //NON-NLS
205  return false;
206  }
207  }
208 
209  if (mimeType.equalsIgnoreCase("application/octet-stream")) {
210  return false;
211  } else {
212  return (getSupportingViewer(aFile) != null);
213  }
214 
215  }
216 
217  @Override
218  public int isPreferred(Node node) {
219  AbstractFile file = node.getLookup().lookup(AbstractFile.class);
220  String mimeType = file.getMIMEType();
221 
222  if (Strings.isNullOrEmpty(mimeType)) {
223  LOGGER.log(Level.INFO, "Mimetype not known for file: {0}", file.getName()); //NON-NLS
224  try {
225  FileTypeDetector fileTypeDetector = new FileTypeDetector();
226  mimeType = fileTypeDetector.getMIMEType(file);
228  LOGGER.log(Level.SEVERE, "Failed to initialize FileTypeDetector.", ex); //NON-NLS
229  return ViewerPriority.viewerPriority.LevelZero.getFlag();
230  }
231  }
232 
233  if (mimeType.equalsIgnoreCase("application/octet-stream")) {
234  return ViewerPriority.viewerPriority.LevelZero.getFlag();
235  } else {
236  if (null != getSupportingViewer(file)) {
237  return ViewerPriority.viewerPriority.LevelFive.getFlag();
238  }
239  }
240 
241  return ViewerPriority.viewerPriority.LevelZero.getFlag();
242  }
243 }
FileTypeViewer getSupportingViewer(AbstractFile file)
Definition: FileViewer.java:88
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.