/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cli_fmw.utils;
import cli_fmw.main.ClipsException;
import java.awt.Desktop;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
/**
*
* @author petr
*/
public class DefaultBrowser {
// Used to identify the windows platform.
private static final String WIN_ID = "Windows";
// The default system browser under windows.
private static final String WIN_PATH = "rundll32";
// The flag to display a url.
private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
// The default browser under unix.
private static final String UNIX_PATH = "xdg-open";
// The flag to display a url.
private static final String UNIX_FLAG = "-remote openURL";
/**
* Опеределяет ОС виндовс
* @return истина если винда
*/
private static boolean isWindowsPlatform() {
String os = System.getProperty("os.name");
if (os != null && os.startsWith(WIN_ID)) {
return true;
} else {
return false;
}
}
public static void open(URI uri) {
ClipsException exception = null;
try {
openByDesktopApi(uri);
} catch (ClipsException ex) {
exception = new ClipsException("Не удалось открыть ссылку используя Desktop API", ex);
Log.printlnAnsPos("Не удалось открыть ссылку используя Desktop API, используем xdg-utils");
try {
openByXDG(uri.toURL());
} catch (MalformedURLException ex1) {
Log.printlnAnsPos("Неверный URI");
exception = new ClipsException("Неверный URI", ex1);
MessageBox.showException(exception);
} catch (ClipsException ex1) {
Log.printlnAnsPos("Не удалось открыть ссылку используя xdg-utils");
exception = new ClipsException("Не удалось открыть ссылку используя xdg-utils", ex1);
MessageBox.showException(exception);
}
}
}
/**
* Открывает ссылку используя Desktop API
* @param uri
* @throws cli_fmw.main.ClipsException
*/
private static void openByDesktopApi(URI uri) throws ClipsException{
try {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(uri);
}else{
throw new ClipsException("Desktop API не поддерживается Вашей системой");
}
} catch (Exception ex) {
throw new ClipsException("Не удалось открыть \"" + uri + "\" в стандартном браузере", ex);
}
}
/**
* Открывает ссылку используя пакет xdg-utils
* @param url
* @throws cli_fmw.main.ClipsException
*/
private static void openByXDG(URL url) throws ClipsException {
String cmd = null;
try {
if (isWindowsPlatform()) {
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
Process p = Runtime.getRuntime().exec(cmd);
} else {
cmd = UNIX_PATH + " " + url;// " " + UNIX_FLAG + "(" + url + ")";
Process p = Runtime.getRuntime().exec(cmd);
try {
int exitCode = p.waitFor();
if (exitCode != 0) {
cmd = UNIX_PATH + " " + url;
p = Runtime.getRuntime().exec(cmd);
}
} catch (InterruptedException ex) {
throw new ClipsException("Не удалось открыть ссылку \"" + cmd + "\" в стандартном браузере", ex);
}
}
} catch (IOException ex) {
// couldn't exec browser
throw new ClipsException("Не удалось открыть ссылку \"" + cmd + "\" в стандартном браузере", ex);
}
}
}