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 Oct 3, 2023
1 parent 3b3e7b3 commit 6be283e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 35 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew clean build jacocoTestReport
run: ./gradlew clean build jacocoTestReport --info --stacktrace
env:
TEST_MODE_ACTIVE: true
- name: Generate Codecov Report
Expand All @@ -33,7 +33,7 @@ jobs:
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew clean build
run: ./gradlew clean build jacocoTestReport --info --stacktrace
env:
TEST_MODE_ACTIVE: true

Expand All @@ -49,6 +49,6 @@ jobs:
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew clean build
run: ./gradlew clean build jacocoTestReport --info --stacktrace
env:
TEST_MODE_ACTIVE: true
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
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public static void cleanDistribution() {
*
* @param file file path
*/
private static void addExecutablePermissionToFile(File file) {
static void addExecutablePermissionToFile(File file) {
file.setReadable(true, false);
file.setExecutable(true, false);
file.setWritable(true, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ public void projectTestWithLatestSpec() throws IOException, InterruptedException
Assert.assertTrue(output.contains("Generating executable"));

Assert.assertTrue(Files.exists(projectPath.resolve("target/bin/project2.jar")));
TestUtils.addExecutablePermissionToFile(TestUtils.TEST_DIR.resolve("project2").resolve("target").resolve("bin").resolve("project2.jar").toFile());

args.remove(args.size() - 1);
args.add("run");
Expand Down

0 comments on commit 6be283e

Please sign in to comment.