Author: jcouteau Date: 2012-01-10 10:15:15 +0100 (Tue, 10 Jan 2012) New Revision: 2277 Url: http://nuiton.org/repositories/revision/nuiton-utils/2277 Log: #1891 : Introduce a utility method/class to open URIs in the user default browser Added: trunk/nuiton-utils/src/main/java/org/nuiton/util/DesktopUtil.java trunk/nuiton-utils/src/test/java/org/nuiton/util/DesktopUtilTest.java Added: trunk/nuiton-utils/src/main/java/org/nuiton/util/DesktopUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/DesktopUtil.java (rev 0) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/DesktopUtil.java 2012-01-10 09:15:15 UTC (rev 2277) @@ -0,0 +1,70 @@ +package org.nuiton.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.awt.Desktop; +import java.io.IOException; +import java.net.URI; + +/** + * Utility class for methods to interact with Desktop Environment + * + * @author jcouteau + * @since 2.4.3 + */ +public class DesktopUtil { + + /** Logger. */ + static private Log log = LogFactory.getLog(DesktopUtil.class); + + /** + * Method to open an URI in the user default web browser. It uses the Java + * Desktop API on Windows and Gnome environment and xdg-open on other + * platforms (non-gnome linux distribution for example). + * + * A Bug report have been opened in 2006 so that java.awt.Desktop can + * support all environments but it is not fixed yet : + * http://bugs.sun.com/view_bug.do?bug_id=6486393 this utility method should + * be removed when the bug is fixed. + * + * @param uri the URI to open + */ + public static void browse(URI uri) { + try { + if (Desktop.isDesktopSupported()) { + Desktop desktop = Desktop.getDesktop(); + if (desktop.isSupported(Desktop.Action.BROWSE)) { + + desktop.browse(uri); + + } else { + if (log.isDebugEnabled()) { + log.debug("Browse operation not supported"); + } + } + } else { + if (log.isDebugEnabled()) { + log.debug("Desktop not supported"); + } + } + } catch(IOException ioe) { + if (log.isDebugEnabled()) { + log.debug("Desktop API not supported, launching xdg-open"); + } + + ProcessBuilder pb = new ProcessBuilder("xdg-open", uri.toString()); + try { + pb.start(); + } catch (IOException e) { + if (log.isDebugEnabled()) { + log.debug("Could not launch browser, there is maybe no " + + "default browser configured on the system"); + } + } + + } + + } + +} Added: trunk/nuiton-utils/src/test/java/org/nuiton/util/DesktopUtilTest.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/DesktopUtilTest.java (rev 0) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/DesktopUtilTest.java 2012-01-10 09:15:15 UTC (rev 2277) @@ -0,0 +1,15 @@ +package org.nuiton.util; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +public class DesktopUtilTest { + + // Test ignored because it can fail on continuous integration server or + // headless environment + //@Test + public void testBrowse() throws IOException, URISyntaxException { + DesktopUtil.browse(new URI("http://localhost")); + } +}
participants (1)
-
jcouteau@users.nuiton.org