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