From 4f01bb8f9b708c71e7a2111c87371dbfc1d53dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hofman?= Date: Wed, 29 Jan 2025 12:33:24 +0100 Subject: [PATCH] SNOW-1708383: Fix opening external browser on win (#2053) --- .pre-commit-config.yaml | 2 +- .../core/SessionUtilExternalBrowser.java | 29 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e83ee91826..e7689ba266 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,5 @@ repos: - repo: git@github.com:snowflakedb/casec_precommit.git - rev: v1.11 + rev: v1.35.5 hooks: - id: secret-scanner diff --git a/src/main/java/net/snowflake/client/core/SessionUtilExternalBrowser.java b/src/main/java/net/snowflake/client/core/SessionUtilExternalBrowser.java index 0f83a96421..ada33ee2bc 100644 --- a/src/main/java/net/snowflake/client/core/SessionUtilExternalBrowser.java +++ b/src/main/java/net/snowflake/client/core/SessionUtilExternalBrowser.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Strings; +import java.awt.Desktop; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -69,24 +70,22 @@ public HttpPost build(URI uri) { @Override public void openBrowser(String ssoUrl) throws SFException { + if (!URLUtil.isValidURL(ssoUrl)) { + throw new SFException(ErrorCode.INVALID_CONNECTION_URL, "Invalid SSOUrl found - " + ssoUrl); + } try { // start web browser - if (!URLUtil.isValidURL(ssoUrl)) { - throw new SFException( - ErrorCode.INVALID_CONNECTION_URL, "Invalid SSOUrl found - " + ssoUrl); - } - if (java.awt.Desktop.isDesktopSupported()) { - URI uri = new URI(ssoUrl); - java.awt.Desktop.getDesktop().browse(uri); + Runtime runtime = Runtime.getRuntime(); + Constants.OS os = Constants.getOS(); + if (Desktop.isDesktopSupported() + && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { + Desktop.getDesktop().browse(new URI(ssoUrl)); + } else if (os == Constants.OS.MAC) { + runtime.exec("open " + ssoUrl); + } else if (os == Constants.OS.WINDOWS) { + runtime.exec(new String[] {"rundll32", "url.dll,FileProtocolHandler", ssoUrl}); } else { - Runtime runtime = Runtime.getRuntime(); - Constants.OS os = Constants.getOS(); - if (os == Constants.OS.MAC) { - runtime.exec("open " + ssoUrl); - } else { - // linux? - runtime.exec("xdg-open " + ssoUrl); - } + runtime.exec("xdg-open " + ssoUrl); } } catch (URISyntaxException | IOException ex) { throw new SFException(ex, ErrorCode.NETWORK_ERROR, ex.getMessage());