Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
Desktop.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2023 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.coreutils;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.URI;
24 import java.util.concurrent.TimeUnit;
25 import java.util.logging.Level;
26 import org.apache.commons.lang3.SystemUtils;
27 
32 public class Desktop {
33 
34  private static final Logger LOGGER = Logger.getLogger(Desktop.class.getName());
35  private static final long XDG_TIMEOUT_SECS = 30;
36 
37  private static Boolean xdgSupported = null;
38 
39  private static boolean isXdgSupported() {
40  if (xdgSupported == null) {
41  xdgSupported = false;
42  if (SystemUtils.IS_OS_LINUX) {
43  try {
44  xdgSupported = Runtime.getRuntime().exec(new String[]{"which", "xdg-open"}).getInputStream().read() != -1;
45  } catch (IOException ex) {
46  LOGGER.log(Level.WARNING, "There was an error running 'which xdg-open' ", ex);
47  }
48  }
49  }
50 
51  return xdgSupported;
52  }
53 
58  public static boolean isDesktopSupported() {
59  return java.awt.Desktop.isDesktopSupported() || isXdgSupported();
60  }
61 
62  private static Desktop instance = null;
63 
67  public static Desktop getDesktop() {
68  if (instance == null) {
69  instance = new Desktop(java.awt.Desktop.getDesktop());
70  }
71 
72  return instance;
73  }
74 
75  private final java.awt.Desktop awtDesktop;
76 
82  private Desktop(java.awt.Desktop awtDesktop) {
83  this.awtDesktop = awtDesktop;
84  }
85 
92  private void xdgOpen(String path) throws IOException {
93  Process process = Runtime.getRuntime().exec(new String[]{"xdg-open", path});
94  try {
95  process.waitFor(XDG_TIMEOUT_SECS, TimeUnit.SECONDS);
96  } catch (InterruptedException ex) {
97  throw new IOException("xdg-open timed out", ex);
98  }
99  int exitCode = process.exitValue();
100  if (exitCode != 0) {
101  throw new IOException("Received non-zero exit code from xdg-open: " + exitCode);
102  }
103  }
104 
111  public void browse(URI uri) throws IOException {
112  if (!awtDesktop.isSupported(java.awt.Desktop.Action.BROWSE) && isXdgSupported()) {
113  xdgOpen(uri.toString());
114  } else {
115  awtDesktop.browse(uri);
116  }
117  }
118 
125  public void open(File file) throws IOException {
126  if (!awtDesktop.isSupported(java.awt.Desktop.Action.OPEN) && isXdgSupported()) {
127  xdgOpen(file.getAbsolutePath());
128  } else {
129  awtDesktop.open(file);
130  }
131  }
132 
139  public void edit(File file) throws IOException {
140  if (!awtDesktop.isSupported(java.awt.Desktop.Action.EDIT) && isXdgSupported()) {
141  xdgOpen(file.getAbsolutePath());
142  } else {
143  awtDesktop.edit(file);
144  }
145  }
146 }
final java.awt.Desktop awtDesktop
Definition: Desktop.java:75
Desktop(java.awt.Desktop awtDesktop)
Definition: Desktop.java:82
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2024 Sleuth Kit Labs. Generated on: Mon Feb 17 2025
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.