summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java
diff options
context:
space:
mode:
authorJonathan Bauer2018-04-26 18:19:48 +0200
committerJonathan Bauer2018-04-26 18:19:48 +0200
commit1bfa51971154f6f851c33746aac3d9a37d00b5cf (patch)
treea18abe2381323e5e69c22cf82f5ae7fec0db4687 /dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java
parent[client] Better warning for streamOptimized Images (diff)
downloadtutor-module-1bfa51971154f6f851c33746aac3d9a37d00b5cf.tar.gz
tutor-module-1bfa51971154f6f851c33746aac3d9a37d00b5cf.tar.xz
tutor-module-1bfa51971154f6f851c33746aac3d9a37d00b5cf.zip
fix broken links on some desktop
Introduce OSHelper class that should be used when trying to detect the type of OS we are on. This is now used when deciding the path to save the config to and when trying to execute the fallback command if opening the link via java's desktop API fails.
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java67
1 files changed, 43 insertions, 24 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java
index 2a181a34..dc34922b 100644
--- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/OpenLinks.java
@@ -2,6 +2,7 @@ package org.openslx.dozmod.util;
import java.awt.Desktop;
import java.io.File;
+import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
@@ -41,7 +42,7 @@ public class OpenLinks {
public static boolean openWebpage(Link location) {
return openWebpageUri(location.uri);
}
-
+
public static boolean openWebpageUri(URI uri) {
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
@@ -51,7 +52,7 @@ public class OpenLinks {
LOGGER.error("Got exception in openWebpage: ", e);
}
}
- return false;
+ return fallbackExec(uri.toString());
}
public static boolean openLocal(File path) {
@@ -61,17 +62,7 @@ public class OpenLinks {
} catch (Exception e) {
LOGGER.error("Got exception in openLocal: ", e);
}
- try {
- int exitCode = Runtime.getRuntime()
- .exec(new String[] { "xdg-open", path.getAbsolutePath() })
- .waitFor();
- if (exitCode == 0)
- return true;
- LOGGER.error("xdg-open returned " + exitCode);
- } catch (Exception e) {
- LOGGER.error("xdg-open fallback failed", e);
- }
- return false;
+ return fallbackExec(path.getAbsolutePath());
}
/**
@@ -83,20 +74,48 @@ public class OpenLinks {
* @return false on not-success
*/
public static boolean sendMail(String address, String subject) {
- if (desktop != null && desktop.isSupported(Desktop.Action.MAIL)) {
- try {
- if (subject != null) {
- desktop.mail(new URI("mailto:" + address + "?subject="
- + URLEncoder.encode(subject, "UTF-8")));
- } else {
- desktop.mail(new URI("mailto:" + address));
- }
+ URI mailToUri = null;
+ try {
+ if (subject != null) {
+ mailToUri = new URI("mailto:" + address + "?subject=" + URLEncoder.encode(subject, "UTF-8"));
+ } else {
+ mailToUri = new URI("mailto:" + address);
+ }
+ if (desktop != null && desktop.isSupported(Desktop.Action.MAIL)) {
+ desktop.mail(mailToUri);
return true;
- } catch (Exception e) {
- LOGGER.error("Got exception in sendMail: ", e);
}
+ } catch (Exception e) {
+ LOGGER.error("Got exception in sendMail: ", e);
}
- return false;
+ return mailToUri != null && fallbackExec(mailToUri.toString());
}
+ /**
+ * Opens the given link using the system's preferred application. This makes
+ * use of OSHelper to create a OS-dependent command to execute.
+ *
+ * @param link to open
+ * @return true if the external command executed successfully, false
+ * otherwise.
+ */
+ public static boolean fallbackExec(String link) {
+ String[] execCommand;
+ if (OSHelper.isLinux()) {
+ execCommand = new String[] { "xdg-open", link };
+ } else if (OSHelper.isWindows()) {
+ execCommand = new String[] { "cmd.exe", "/c", "start", link };
+ } else {
+ LOGGER.error("Could not set command for unknown operating system: " + OSHelper.osName);
+ return false;
+ }
+ int exitCode = -1;
+ try {
+ exitCode = Runtime.getRuntime().exec(execCommand).waitFor();
+ } catch (InterruptedException | IOException e) {
+ LOGGER.debug("Failed to execute: " + execCommand + "\nTrace: ", e);
+ return false;
+ }
+ return exitCode == 0;
+ }
}