diff --git a/src/main/java/org/ballerinalang/command/util/ToolUtil.java b/src/main/java/org/ballerinalang/command/util/ToolUtil.java index 115a7d9a..0fa6a29c 100644 --- a/src/main/java/org/ballerinalang/command/util/ToolUtil.java +++ b/src/main/java/org/ballerinalang/command/util/ToolUtil.java @@ -66,7 +66,9 @@ public class ToolUtil { public static final String CLI_HELP_FILE_PREFIX = "dist-"; private static final String BALLERINA_1_X_VERSIONS = "1.0."; private static final String CONNECTION_ERROR_MESSAGE = "connection to the remote server failed"; + private static final String PROXY_ERROR_MESSAGE = "connection to the remote server through proxy server failed"; private static final String BALLERINA_SETTINGS_FILE = "Settings.toml"; + private static final String PROXY = "proxy"; public static final boolean BALLERINA_STAGING_UPDATE = Boolean.parseBoolean( System.getenv("BALLERINA_STAGING_UPDATE")); public static final boolean BALLERINA_DEV_UPDATE = Boolean.parseBoolean( @@ -204,42 +206,50 @@ public static boolean checkDependencyAvailable(String dependency) { } public static HttpsURLConnection getServerUrlWithProxyAuthentication(URL serverURL) throws IOException { - Map proxyConfigs = getProxyConfigs(); - String proxyHost = proxyConfigs.containsKey("host") ? proxyConfigs.get("host").toString() : null; - String proxyPort = proxyConfigs.containsKey("port") ? proxyConfigs.get("port").toString() : null; - String proxyUser = proxyConfigs.containsKey("user") ? proxyConfigs.get("user").toString() : null; - String proxyPassword = proxyConfigs.containsKey("password") ? proxyConfigs.get("password").toString() : null; - - if (proxyHost != null && proxyPort != null && !"".equals(proxyHost) && Integer.parseInt(proxyPort) > 0 && - Integer.parseInt(proxyPort) < 65536) { - if (proxyUser != null && proxyPassword != null && !"".equals(proxyUser) && !"".equals(proxyPassword)) { - Authenticator authenticator = new Authenticator() { - @Override - public PasswordAuthentication getPasswordAuthentication() { - return (new PasswordAuthentication(proxyUser, - proxyPassword.toCharArray())); - } - }; - Authenticator.setDefault(authenticator); + if (checkProxyConfigsDefinition()) { + Map proxyConfigs = getProxyConfigs(); + String proxyHost = proxyConfigs.containsKey("host") ? proxyConfigs.get("host").toString() : null; + String proxyPort = proxyConfigs.containsKey("port") ? proxyConfigs.get("port").toString() : null; + String proxyUser = proxyConfigs.containsKey("user") ? proxyConfigs.get("user").toString() : null; + String proxyPassword = proxyConfigs.containsKey("password") ? proxyConfigs.get("password").toString() : null; + + if (proxyHost != null && proxyPort != null && !"".equals(proxyHost) && Integer.parseInt(proxyPort) > 0 && + Integer.parseInt(proxyPort) < 65536) { + if (proxyUser != null && proxyPassword != null && !"".equals(proxyUser) && !"".equals(proxyPassword)) { + Authenticator authenticator = new Authenticator() { + @Override + public PasswordAuthentication getPasswordAuthentication() { + return (new PasswordAuthentication(proxyUser, + proxyPassword.toCharArray())); + } + }; + Authenticator.setDefault(authenticator); + } + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort))); + return (HttpsURLConnection) serverURL.openConnection(proxy); + } else { + return (HttpsURLConnection) serverURL.openConnection(); } - Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort))); - return (HttpsURLConnection) serverURL.openConnection(proxy); } else { return (HttpsURLConnection) serverURL.openConnection(); } } public static Map getProxyConfigs () { - Map proxyConfigs = new HashMap<>(); + File settingsFile = new File(OSUtils.getBallerinaHomePath() + File.separator + + BALLERINA_SETTINGS_FILE ); + Toml toml = new Toml().read(settingsFile); + return toml.getTable(PROXY).toMap(); + } + + private static boolean checkProxyConfigsDefinition() { File settingsFile = new File(OSUtils.getBallerinaHomePath() + File.separator + BALLERINA_SETTINGS_FILE ); if (settingsFile.exists()) { Toml toml = new Toml().read(settingsFile); - if (toml.contains("proxy")) { - proxyConfigs = toml.getTable("proxy").toMap(); - } + return toml.contains(PROXY); } - return proxyConfigs; + return false; } public static List getDistributions(PrintStream printStream) { @@ -302,7 +312,11 @@ public static List getDistributions(PrintStream printStream) { } } } catch (IOException e) { - throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + if (checkProxyConfigsDefinition()) { + throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE); + } else { + throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + } } finally { if (conn != null) { conn.disconnect(); @@ -330,7 +344,11 @@ public static String getLatest(String currentVersion, String type) { } throw ErrorUtil.createCommandException(getServerRequestFailedErrorMessage(conn)); } catch (IOException e) { - throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + if (checkProxyConfigsDefinition()) { + throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE); + } else { + throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + } } finally { if (conn != null) { conn.disconnect(); @@ -376,7 +394,11 @@ public static Tool getLatestToolVersion() { } throw ErrorUtil.createCommandException(getServerRequestFailedErrorMessage(conn)); } catch (IOException e) { - throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + if (checkProxyConfigsDefinition()) { + throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE); + } else { + throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + } } finally { if (conn != null) { conn.disconnect(); @@ -517,7 +539,11 @@ public static boolean downloadDistribution(PrintStream printStream, String distr return true; } } catch (IOException e) { - throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + if (checkProxyConfigsDefinition()) { + throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE); + } else { + throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + } } finally { if (conn != null) { conn.disconnect(); @@ -596,7 +622,11 @@ public static void getDependency(PrintStream printStream, String distribution, S } } } catch (IOException e) { - throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + if (checkProxyConfigsDefinition()) { + throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE); + } else { + throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + } } finally { if (conn != null) { conn.disconnect(); @@ -626,7 +656,11 @@ private static void downloadDependency(PrintStream printStream, String dependenc throw ErrorUtil.createDependencyNotFoundException(dependency); } } catch (IOException e) { - throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + if (checkProxyConfigsDefinition()) { + throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE); + } else { + throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + } } } @@ -668,7 +702,11 @@ public static void downloadTool(PrintStream printStream, String toolVersion) { throw ErrorUtil.createCommandException("tool version '" + toolVersion + "' not found "); } } catch (IOException e) { - throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + if (checkProxyConfigsDefinition()) { + throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE); + } else { + throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE); + } } finally { if (conn != null) { conn.disconnect();