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;
34 
40 public class ExternalViewerAction extends AbstractAction {
41 
42  private final static Logger logger = Logger.getLogger(ExternalViewerAction.class.getName());
44  private String fileObjectExt;
45  final static String[] EXECUTABLE_EXT = {".exe", ".dll", ".com", ".bat", ".msi", ".reg", ".scr", ".cmd"}; //NON-NLS
46 
47  public ExternalViewerAction(String title, Node fileNode) {
48  super(title);
49  this.fileObject = fileNode.getLookup().lookup(org.sleuthkit.datamodel.AbstractFile.class);
50 
51  long size = fileObject.getSize();
52  String fileName = fileObject.getName();
53  int extPos = fileName.lastIndexOf('.');
54 
55  boolean isExecutable = false;
56  if (extPos != -1) {
57  String extension = fileName.substring(extPos, fileName.length()).toLowerCase();
58  fileObjectExt = extension;
59  for (int i = 0; i < EXECUTABLE_EXT.length; ++i) {
60  if (EXECUTABLE_EXT[i].equals(extension)) {
61  isExecutable = true;
62  break;
63  }
64  }
65  } else {
66  fileObjectExt = "";
67  }
68 
69  // no point opening a file if it's empty, and java doesn't know how to
70  // find an application for files without an extension
71  // or if file is executable (for security reasons)
72  // Also skip slack files since their extension is the original extension + "-slack"
73  if (!(size > 0) || extPos == -1 || isExecutable || (fileNode instanceof SlackFileNode)) {
74  this.setEnabled(false);
75  }
76  }
77 
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 
97 
98  // delete the temporary file on exit
99  tempFile.deleteOnExit();
100  }
101 
110  @Messages({
111  "ExternalViewerAction.actionPerformed.failure.title=Open File Failure",
112  "ExternalViewerAction.actionPerformed.failure.IO.message=There is no associated editor for files of this type or the associated application failed to launch.",
113  "ExternalViewerAction.actionPerformed.failure.support.message=This platform (operating system) does not support opening a file in an editor this way.",
114  "ExternalViewerAction.actionPerformed.failure.missingFile.message=The file no longer exists.",
115  "ExternalViewerAction.actionPerformed.failure.permission.message=Permission to open the file was denied."})
116  public static void openFile(String mimeType, String ext, File file) {
121  String exePath = ExternalViewerRulesManager.getInstance().getExePathForName(mimeType);
122  if (exePath.equals("")) {
123  exePath = ExternalViewerRulesManager.getInstance().getExePathForName(ext);
124  }
125  if (!exePath.equals("")) {
126  Runtime runtime = Runtime.getRuntime();
127  String[] s = new String[]{exePath, file.getAbsolutePath()};
128  try {
129  runtime.exec(s);
130  } catch (IOException ex) {
131  logger.log(Level.WARNING, "Could not open the specified viewer for the given file: " + file.getName(), ex); //NON-NLS
132  JOptionPane.showMessageDialog(null, Bundle.ExternalViewerAction_actionPerformed_failure_IO_message(), Bundle.ExternalViewerAction_actionPerformed_failure_title(), JOptionPane.ERROR_MESSAGE);
133  }
134  } else {
135  try {
136  Desktop.getDesktop().open(file);
137  } catch (IOException ex) {
138  logger.log(Level.WARNING, "Could not find a viewer for the given file: " + file.getName(), ex); //NON-NLS
139  JOptionPane.showMessageDialog(null,
140  Bundle.ExternalViewerAction_actionPerformed_failure_IO_message(),
141  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
142  JOptionPane.ERROR_MESSAGE);
143  } catch (UnsupportedOperationException ex) {
144  logger.log(Level.WARNING, "Platform cannot open " + file.getName() + " in the defined editor.", ex); //NON-NLS
145  JOptionPane.showMessageDialog(null,
146  Bundle.ExternalViewerAction_actionPerformed_failure_support_message(),
147  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
148  JOptionPane.ERROR_MESSAGE);
149  } catch (IllegalArgumentException ex) {
150  logger.log(Level.WARNING, "Could not find the given file: " + file.getName(), ex); //NON-NLS
151  JOptionPane.showMessageDialog(null,
152  Bundle.ExternalViewerAction_actionPerformed_failure_missingFile_message(),
153  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
154  JOptionPane.ERROR_MESSAGE);
155  } catch (SecurityException ex) {
156  logger.log(Level.WARNING, "Could not get permission to open the given file: " + file.getName(), ex); //NON-NLS
157  JOptionPane.showMessageDialog(null,
158  Bundle.ExternalViewerAction_actionPerformed_failure_permission_message(),
159  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
160  JOptionPane.ERROR_MESSAGE);
161  }
162  }
163  }
164 
165 }
static< T > long writeToFile(Content content, java.io.File outputFile, ProgressHandle progress, Future< T > worker, boolean source)
static void openFile(String mimeType, String ext, File file)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161

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