Autopsy  4.6.0
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-2018 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.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.logging.Level;
28 import javax.swing.AbstractAction;
29 import javax.swing.JOptionPane;
30 import org.openide.nodes.Node;
31 import org.openide.util.NbBundle.Messages;
32 import org.openide.windows.WindowManager;
38 
44 public class ExternalViewerAction extends AbstractAction {
45 
46  private final static Logger logger = Logger.getLogger(ExternalViewerAction.class.getName());
47  private final org.sleuthkit.datamodel.AbstractFile fileObject;
48  private String fileObjectExt;
49  final static String[] EXECUTABLE_EXT = {".exe", ".dll", ".com", ".bat", ".msi", ".reg", ".scr", ".cmd"}; //NON-NLS
50 
56  public ExternalViewerAction(String title, Node fileNode) {
57  super(title);
58  this.fileObject = fileNode.getLookup().lookup(org.sleuthkit.datamodel.AbstractFile.class);
59 
60  long size = fileObject.getSize();
61  String fileName = fileObject.getName();
62  int extPos = fileName.lastIndexOf('.');
63 
64  boolean isExecutable = false;
65  if (extPos != -1) {
66  String extension = fileName.substring(extPos, fileName.length()).toLowerCase();
67  fileObjectExt = extension;
68  for (int i = 0; i < EXECUTABLE_EXT.length; ++i) {
69  if (EXECUTABLE_EXT[i].equals(extension)) {
70  isExecutable = true;
71  break;
72  }
73  }
74  } else {
75  fileObjectExt = "";
76  }
77 
78  // no point opening a file if it's empty, and java doesn't know how to
79  // find an application for files without an extension
80  // or if file is executable (for security reasons)
81  // Also skip slack files since their extension is the original extension + "-slack"
82  if (!(size > 0) || extPos == -1 || isExecutable || (fileNode instanceof SlackFileNode)) {
83  this.setEnabled(false);
84  }
85  }
86 
87  @Override
88  public void actionPerformed(ActionEvent e) {
89  // Get the temp folder path of the case
90  Case openCase;
91  try {
92  openCase = Case.getOpenCase();
93  } catch (NoCurrentCaseException ex) {
94  logger.log(Level.WARNING, "Exception while getting open case.", ex); //NON-NLS
95  return;
96  }
97  String tempPath = openCase.getTempDirectory();
98  tempPath = tempPath + File.separator + this.fileObject.getName();
99 
100  // create the temporary file
101  File tempFile = new File(tempPath);
102  if (tempFile.exists()) {
103  tempFile.delete();
104  }
105  try {
106  tempFile.createNewFile();
108  } catch (IOException ex) {
109  logger.log(Level.WARNING, "Can't save to temporary file.", ex); //NON-NLS
110  }
111 
112  ExternalViewerAction.openFile(fileObject.getMIMEType(), fileObjectExt, tempFile);
113 
114  // delete the temporary file on exit
115  tempFile.deleteOnExit();
116  }
117 
126  @Messages({
127  "ExternalViewerAction.actionPerformed.failure.title=Open File Failure",
128  "ExternalViewerAction.actionPerformed.failure.IO.message=There is no associated editor for files of this type or the associated application failed to launch.",
129  "ExternalViewerAction.actionPerformed.failure.support.message=This platform (operating system) does not support opening a file in an editor this way.",
130  "ExternalViewerAction.actionPerformed.failure.missingFile.message=The file no longer exists.",
131  "ExternalViewerAction.actionPerformed.failure.permission.message=Permission to open the file was denied.",
132  "ExternalViewerAction.actionPerformed.failure.open.url=Cannot open URL"})
133  public static void openFile(String mimeType, String ext, File file) {
138  String exePath = ExternalViewerRulesManager.getInstance().getExePathForName(mimeType);
139  if (exePath.equals("")) {
140  exePath = ExternalViewerRulesManager.getInstance().getExePathForName(ext);
141  }
142  if (!exePath.equals("")) {
143  Runtime runtime = Runtime.getRuntime();
144  String[] execArray = new String[]{exePath, file.getAbsolutePath()};
145  try {
146  runtime.exec(execArray);
147  } catch (IOException ex) {
148  logger.log(Level.WARNING, "Could not open the specified viewer for the given file: " + file.getName(), ex); //NON-NLS
149  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(), Bundle.ExternalViewerAction_actionPerformed_failure_IO_message(), Bundle.ExternalViewerAction_actionPerformed_failure_title(), JOptionPane.ERROR_MESSAGE);
150  }
151  } else {
152  try {
153  String localpath = file.getPath();
154  if (localpath.toLowerCase().contains("http")) {
155  String url_path = file.getPath().replaceAll("\\\\","/");
156  Desktop.getDesktop().browse(new URI(url_path.replaceFirst("/","//")));
157  } else {
158  Desktop.getDesktop().open(file);
159  }
160 
161  } catch (IOException ex) {
162  logger.log(Level.WARNING, "Could not find a viewer for the given file: " + file.getName(), ex); //NON-NLS
163  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
164  Bundle.ExternalViewerAction_actionPerformed_failure_IO_message(),
165  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
166  JOptionPane.ERROR_MESSAGE);
167  } catch (UnsupportedOperationException ex) {
168  logger.log(Level.WARNING, "Platform cannot open " + file.getName() + " in the defined editor.", ex); //NON-NLS
169  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
170  Bundle.ExternalViewerAction_actionPerformed_failure_support_message(),
171  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
172  JOptionPane.ERROR_MESSAGE);
173  } catch (IllegalArgumentException ex) {
174  logger.log(Level.WARNING, "Could not find the given file: " + file.getName(), ex); //NON-NLS
175  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
176  Bundle.ExternalViewerAction_actionPerformed_failure_missingFile_message(),
177  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
178  JOptionPane.ERROR_MESSAGE);
179  } catch (SecurityException ex) {
180  logger.log(Level.WARNING, "Could not get permission to open the given file: " + file.getName(), ex); //NON-NLS
181  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
182  Bundle.ExternalViewerAction_actionPerformed_failure_permission_message(),
183  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
184  JOptionPane.ERROR_MESSAGE);
185  } catch (URISyntaxException ex) {
186  logger.log(Level.WARNING, "Could not open URL provided: " + file.getPath(), ex);
187  JOptionPane.showMessageDialog(null,
188  Bundle.ExternalViewerAction_actionPerformed_failure_open_url(),
189  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
190  JOptionPane.ERROR_MESSAGE);
191  }
192  }
193  }
194 
200  public static void openURL(String path) {
201  String url_path = path.replaceAll("\\\\","/");
202  try {
203  Desktop.getDesktop().browse(new URI(url_path.replaceFirst("/","//")));
204  } catch (IOException ex) {
205  logger.log(Level.WARNING, "Could not find a viewer for the given URL: " + url_path, ex); //NON-NLS
206  JOptionPane.showMessageDialog(null,
207  Bundle.ExternalViewerAction_actionPerformed_failure_IO_message(),
208  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
209  JOptionPane.ERROR_MESSAGE);
210  } catch (UnsupportedOperationException ex) {
211  logger.log(Level.WARNING, "Platform cannot open " + url_path + " in the defined editor.", ex); //NON-NLS
212  JOptionPane.showMessageDialog(null,
213  Bundle.ExternalViewerAction_actionPerformed_failure_support_message(),
214  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
215  JOptionPane.ERROR_MESSAGE);
216  } catch (IllegalArgumentException ex) {
217  logger.log(Level.WARNING, "Could not find the given URL: " + url_path, ex); //NON-NLS
218  JOptionPane.showMessageDialog(null,
219  Bundle.ExternalViewerAction_actionPerformed_failure_missingFile_message(),
220  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
221  JOptionPane.ERROR_MESSAGE);
222  } catch (SecurityException ex) {
223  logger.log(Level.WARNING, "Could not get permission to open the given URL: " + url_path, ex); //NON-NLS
224  JOptionPane.showMessageDialog(null,
225  Bundle.ExternalViewerAction_actionPerformed_failure_permission_message(),
226  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
227  JOptionPane.ERROR_MESSAGE);
228  } catch (URISyntaxException ex) {
229  logger.log(Level.WARNING, "Could not open URL provided: " + url_path, ex);
230  JOptionPane.showMessageDialog(null,
231  Bundle.ExternalViewerAction_actionPerformed_failure_open_url(),
232  Bundle.ExternalViewerAction_actionPerformed_failure_title(),
233  JOptionPane.ERROR_MESSAGE);
234  }
235  }
236 }
final org.sleuthkit.datamodel.AbstractFile fileObject
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:124

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