Skip to content

Commit

Permalink
Update error handling for connection through proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
udda1996 committed Sep 26, 2023
1 parent 3b3e7b3 commit cdbef6f
Showing 1 changed file with 69 additions and 31 deletions.
100 changes: 69 additions & 31 deletions src/main/java/org/ballerinalang/command/util/ToolUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -204,42 +206,50 @@ public static boolean checkDependencyAvailable(String dependency) {
}

public static HttpsURLConnection getServerUrlWithProxyAuthentication(URL serverURL) throws IOException {
Map<String, Object> 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<String, Object> 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<String, Object> getProxyConfigs () {
Map<String, Object> 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<Channel> getDistributions(PrintStream printStream) {
Expand Down Expand Up @@ -302,7 +312,11 @@ public static List<Channel> 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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit cdbef6f

Please sign in to comment.