Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ExternalViewerAction.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2016 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.directorytree;
20 
21 import java.awt.Desktop;
22 import java.awt.event.ActionEvent;
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.logging.Level;
26 import javax.swing.AbstractAction;
27 import javax.swing.JOptionPane;
28 import org.openide.nodes.Node;
29 import org.openide.util.NbBundle.Messages;
33 
39 public class ExternalViewerAction extends AbstractAction {
40 
41  private final static Logger logger = Logger.getLogger(ExternalViewerAction.class.getName());
42  private org.sleuthkit.datamodel.AbstractFile fileObject;
43  private String fileObjectExt;
44  final static String[] EXECUTABLE_EXT = {".exe", ".dll", ".com", ".bat", ".msi", ".reg", ".scr", ".cmd"}; //NON-NLS
45 
46  public ExternalViewerAction(String title, Node fileNode) {
47  super(title);
48  this.fileObject = fileNode.getLookup().lookup(org.sleuthkit.datamodel.AbstractFile.class);
49 
50  long size = fileObject.getSize();
51  String fileName = fileObject.getName();
52  int extPos = fileName.lastIndexOf('.');
53 
54  boolean isExecutable = false;
55  if (extPos != -1) {
56  String extension = fileName.substring(extPos, fileName.length()).toLowerCase();
57  fileObjectExt = extension;
58  for (int i = 0; i < EXECUTABLE_EXT.length; ++i) {
59  if (EXECUTABLE_EXT[i].equals(extension)) {
60  isExecutable = true;
61  break;
62  }
63  }
64  } else {
65  fileObjectExt = "";
66  }
67 
68  // no point opening a file if it's empty, and java doesn't know how to
69  // find an application for files without an extension
70  // or if file is executable (for security reasons)
71  if (!(size > 0) || extPos == -1 || isExecutable) {
72  this.setEnabled(false);
73  }
74  }
75 
76  @Messages({"ExternalViewerAction.actionPerformed.failure.message=Could not find a viewer for the given file.",
77  "ExternalViewerAction.actionPerformed.failure.title=Open Failure"})
78  @Override
79  public void actionPerformed(ActionEvent e) {
80  // Get the temp folder path of the case
81  String tempPath = Case.getCurrentCase().getTempDirectory();
82  tempPath = tempPath + File.separator + this.fileObject.getName();
83 
84  // create the temporary file
85  File tempFile = new File(tempPath);
86  if (tempFile.exists()) {
87  tempFile.delete();
88  }
89  try {
90  tempFile.createNewFile();
92  } catch (IOException ex) {
93  logger.log(Level.WARNING, "Can't save to temporary file.", ex); //NON-NLS
94  }
95 
100  String exePath = ExternalViewerRulesManager.getInstance().getExePathForName(fileObject.getMIMEType());
101  if (exePath.equals("")) {
102  exePath = ExternalViewerRulesManager.getInstance().getExePathForName(fileObjectExt);
103  }
104  if (!exePath.equals("")) {
105  Runtime runtime = Runtime.getRuntime();
106  String[] s = new String[]{exePath, tempFile.getAbsolutePath()};
107  try {
108  runtime.exec(s);
109  } catch (IOException ex) {
110  logger.log(Level.WARNING, "Could not open the specified viewer for the given file: " + tempFile.getName(), ex); //NON-NLS
111  JOptionPane.showMessageDialog(null, Bundle.ExternalViewerAction_actionPerformed_failure_message(), Bundle.ExternalViewerAction_actionPerformed_failure_title(), JOptionPane.ERROR_MESSAGE);
112  }
113  } else {
114  try {
115  Desktop.getDesktop().open(tempFile);
116  } catch (IOException ex) {
117  logger.log(Level.WARNING, "Could not find a viewer for the given file: " + tempFile.getName(), ex); //NON-NLS
118  JOptionPane.showMessageDialog(null, Bundle.ExternalViewerAction_actionPerformed_failure_message(), Bundle.ExternalViewerAction_actionPerformed_failure_title(), JOptionPane.ERROR_MESSAGE);
119  }
120  }
121  // delete the file on exit
122  tempFile.deleteOnExit();
123  }
124 }
static< T > long writeToFile(Content content, java.io.File outputFile, ProgressHandle progress, Future< T > worker, boolean source)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161

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.