Modified ExternalViewerAction to accomodate opening reports

This commit is contained in:
Sophie Mori 2016-10-17 16:36:12 -04:00
parent 8d639a29e4
commit 6d86bf48c8
2 changed files with 60 additions and 34 deletions

View File

@ -44,6 +44,7 @@ import org.openide.util.lookup.Lookups;
import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.coreutils.MessageNotifyUtil;
import org.sleuthkit.autopsy.directorytree.ExternalViewerAction;
import org.sleuthkit.datamodel.Report;
import org.sleuthkit.datamodel.TskCoreException;
@ -292,30 +293,16 @@ public final class Reports implements AutopsyVisitableItem {
@Override
public void actionPerformed(ActionEvent e) {
File file = new File(ReportNode.this.report.getPath());
try {
Desktop.getDesktop().open(file);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null,
NbBundle.getMessage(OpenReportAction.class, "OpenReportAction.actionPerformed.NoAssociatedEditorMessage"),
NbBundle.getMessage(OpenReportAction.class, "OpenReportAction.actionPerformed.MessageBoxTitle"),
JOptionPane.ERROR_MESSAGE);
} catch (UnsupportedOperationException ex) {
JOptionPane.showMessageDialog(null,
NbBundle.getMessage(OpenReportAction.class, "OpenReportAction.actionPerformed.NoOpenInEditorSupportMessage"),
NbBundle.getMessage(OpenReportAction.class, "OpenReportAction.actionPerformed.MessageBoxTitle"),
JOptionPane.ERROR_MESSAGE);
} catch (IllegalArgumentException ex) {
JOptionPane.showMessageDialog(null,
NbBundle.getMessage(OpenReportAction.class, "OpenReportAction.actionPerformed.MissingReportFileMessage"),
NbBundle.getMessage(OpenReportAction.class, "OpenReportAction.actionPerformed.MessageBoxTitle"),
JOptionPane.ERROR_MESSAGE);
} catch (SecurityException ex) {
JOptionPane.showMessageDialog(null,
NbBundle.getMessage(OpenReportAction.class, "OpenReportAction.actionPerformed.ReportFileOpenPermissionDeniedMessage"),
NbBundle.getMessage(OpenReportAction.class, "OpenReportAction.actionPerformed.MessageBoxTitle"),
JOptionPane.ERROR_MESSAGE);
String reportPath = ReportNode.this.report.getPath();
String extension = "";
int extPosition = reportPath.lastIndexOf('.');
if (extPosition != -1) {
extension = reportPath.substring(extPosition, reportPath.length()).toLowerCase();
}
File file = new File(reportPath);
ExternalViewerAction.openFile("", extension, file);
}
}
}

View File

@ -73,8 +73,6 @@ public class ExternalViewerAction extends AbstractAction {
}
}
@Messages({"ExternalViewerAction.actionPerformed.failure.message=Could not find a viewer for the given file.",
"ExternalViewerAction.actionPerformed.failure.title=Open Failure"})
@Override
public void actionPerformed(ActionEvent e) {
// Get the temp folder path of the case
@ -93,32 +91,73 @@ public class ExternalViewerAction extends AbstractAction {
logger.log(Level.WARNING, "Can't save to temporary file.", ex); //NON-NLS
}
ExternalViewerAction.openFile(fileObject.getMIMEType(), fileObjectExt, tempFile);
// delete the temporary file on exit
tempFile.deleteOnExit();
}
/**
* Opens a file, taking into account user preferences and then the default
* associated application.
*
* @param mimeType MIME type of the file
* @param ext extension of the file
* @param file the file object
*/
@Messages({
"ExternalViewerAction.actionPerformed.failure.title=Open File Failure",
"ExternalViewerAction.actionPerformed.failure.IO.message=There is no associated editor for files of this type or the associated application failed to launch.",
"ExternalViewerAction.actionPerformed.failure.support.message=This platform (operating system) does not support opening a file in an editor this way.",
"ExternalViewerAction.actionPerformed.failure.missingFile.message=The file no longer exists.",
"ExternalViewerAction.actionPerformed.failure.permission.message=Permission to open the file was denied."})
public static void openFile(String mimeType, String ext, File file) {
/**
* Check if the file MIME type or extension exists in the user defined
* settings. Otherwise open with the default associated application.
*/
String exePath = ExternalViewerRulesManager.getInstance().getExePathForName(fileObject.getMIMEType());
String exePath = ExternalViewerRulesManager.getInstance().getExePathForName(mimeType);
if (exePath.equals("")) {
exePath = ExternalViewerRulesManager.getInstance().getExePathForName(fileObjectExt);
exePath = ExternalViewerRulesManager.getInstance().getExePathForName(ext);
}
if (!exePath.equals("")) {
Runtime runtime = Runtime.getRuntime();
String[] s = new String[]{exePath, tempFile.getAbsolutePath()};
String[] s = new String[]{exePath, file.getAbsolutePath()};
try {
runtime.exec(s);
} catch (IOException ex) {
logger.log(Level.WARNING, "Could not open the specified viewer for the given file: " + tempFile.getName(), ex); //NON-NLS
logger.log(Level.WARNING, "Could not open the specified viewer for the given file: " + file.getName(), ex); //NON-NLS
JOptionPane.showMessageDialog(null, Bundle.ExternalViewerAction_actionPerformed_failure_message(), Bundle.ExternalViewerAction_actionPerformed_failure_title(), JOptionPane.ERROR_MESSAGE);
}
} else {
try {
Desktop.getDesktop().open(tempFile);
Desktop.getDesktop().open(file);
} catch (IOException ex) {
logger.log(Level.WARNING, "Could not find a viewer for the given file: " + tempFile.getName(), ex); //NON-NLS
JOptionPane.showMessageDialog(null, Bundle.ExternalViewerAction_actionPerformed_failure_message(), Bundle.ExternalViewerAction_actionPerformed_failure_title(), JOptionPane.ERROR_MESSAGE);
logger.log(Level.WARNING, "Could not find a viewer for the given file: " + file.getName(), ex); //NON-NLS
JOptionPane.showMessageDialog(null,
Bundle.ExternalViewerAction_actionPerformed_failure_IO_message(),
Bundle.ExternalViewerAction_actionPerformed_failure_title(),
JOptionPane.ERROR_MESSAGE);
} catch (UnsupportedOperationException ex) {
logger.log(Level.WARNING, "Platform cannot open " + file.getName() + " in the defined editor.", ex); //NON-NLS
JOptionPane.showMessageDialog(null,
Bundle.ExternalViewerAction_actionPerformed_failure_support_message(),
Bundle.ExternalViewerAction_actionPerformed_failure_title(),
JOptionPane.ERROR_MESSAGE);
} catch (IllegalArgumentException ex) {
logger.log(Level.WARNING, "Could not find the given file: " + file.getName(), ex); //NON-NLS
JOptionPane.showMessageDialog(null,
Bundle.ExternalViewerAction_actionPerformed_failure_missingFile_message(),
Bundle.ExternalViewerAction_actionPerformed_failure_title(),
JOptionPane.ERROR_MESSAGE);
} catch (SecurityException ex) {
logger.log(Level.WARNING, "Could not get permission to open the given file: " + file.getName(), ex); //NON-NLS
JOptionPane.showMessageDialog(null,
Bundle.ExternalViewerAction_actionPerformed_failure_permission_message(),
Bundle.ExternalViewerAction_actionPerformed_failure_title(),
JOptionPane.ERROR_MESSAGE);
}
}
// delete the file on exit
tempFile.deleteOnExit();
}
}