Skip to content

Commit

Permalink
Fix Desktop#browse in 1.13+
Browse files Browse the repository at this point in the history
  • Loading branch information
roccodev committed Aug 3, 2019
1 parent ed69a4e commit 6185e5d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 9 deletions.
10 changes: 7 additions & 3 deletions mod/src/main/java/eu/the5zig/mod/SocialUrls.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package eu.the5zig.mod;

import eu.the5zig.util.BrowseUrl;

import java.awt.*;
import java.net.URI;

Expand All @@ -12,11 +14,13 @@ public class SocialUrls {
public static final String GITHUB = "https://github.com/5zig-reborn/The-5zig-Mod";

public static void open(String urlString) {
if(!Desktop.isDesktopSupported()) return;

try {
URI url = new URI(urlString);
Desktop.getDesktop().browse(url);
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(url);
} else {
BrowseUrl.get().openURL(url.toURL());
}
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package eu.the5zig.mod.chat.network.packets;

import eu.the5zig.mod.The5zigMod;
import eu.the5zig.util.BrowseUrl;
import io.netty.buffer.ByteBuf;

import java.awt.*;
Expand Down Expand Up @@ -49,13 +50,16 @@ public void write(ByteBuf buffer) throws IOException {

@Override
public void handle() {
if(Desktop.isDesktopSupported()) {
try {
String baseUrl = The5zigMod.DEBUG ? "http://localhost:8080" : "https://secure.5zigreborn.eu";
Desktop.getDesktop().browse(new URI(baseUrl + "/login?token=" + token + "&remember=" + remember));
} catch (Exception e) {
e.printStackTrace();
String baseUrl = The5zigMod.DEBUG ? "http://localhost:8080" : "https://secure.5zigreborn.eu";
try {
URI uri = new URI(baseUrl + "/login?token=" + token + "&remember=" + remember);
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(uri);
} else {
BrowseUrl.get().openURL(uri.toURL());
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
88 changes: 88 additions & 0 deletions mod/src/main/java/eu/the5zig/util/BrowseUrl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2019 5zig Reborn
*
* This file is part of The 5zig Mod
* The 5zig Mod is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The 5zig Mod is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with The 5zig Mod. If not, see <http://www.gnu.org/licenses/>.
*/

package eu.the5zig.util;

import eu.the5zig.mod.The5zigMod;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Locale;

public enum BrowseUrl {
LINUX,
SOLARIS,
WINDOWS {
protected String[] getOpenCommand(URL url) {
return new String[]{"rundll32", "url.dll,FileProtocolHandler", url.toString()};
}
},
OSX {
protected String[] getOpenCommand(URL url) {
return new String[]{"open", url.toString()};
}
},
UNKNOWN;

public void openURL(URL url) {
try {
Process process = AccessController.doPrivileged((PrivilegedExceptionAction<Process>)
(() -> Runtime.getRuntime().exec(this.getOpenCommand(url))));

for (String s : IOUtils.readLines(process.getErrorStream())) {
The5zigMod.logger.error(s);
}

process.getInputStream().close();
process.getErrorStream().close();
process.getOutputStream().close();
} catch (IOException | PrivilegedActionException ex) {
The5zigMod.logger.error("Couldn't open url '{}'", url, ex);
}

}

protected String[] getOpenCommand(URL url) {
String s = url.toString();
if ("file".equals(url.getProtocol())) {
s = s.replace("file:", "file://");
}
return new String[]{"xdg-open", s};
}

public static BrowseUrl get() {
String s = System.getProperty("os.name").toLowerCase(Locale.ROOT);
if (s.contains("win")) {
return WINDOWS;
} else if (s.contains("mac")) {
return OSX;
} else if (s.contains("solaris")) {
return SOLARIS;
} else if (s.contains("sunos")) {
return SOLARIS;
} else if (s.contains("linux")) {
return LINUX;
} else {
return s.contains("unix") ? LINUX : UNKNOWN;
}
}
}

0 comments on commit 6185e5d

Please sign in to comment.