From 7ca3a883c91119660931b9e12ab2f81c15e61594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Mon, 5 Feb 2024 10:46:10 +0000 Subject: [PATCH] remove dependency on com.amazonaws.Protocol from HttpClientSettingsKey --- .../client/core/HttpClientSettingsKey.java | 5 ++--- .../snowflake/client/core/HttpProtocol.java | 19 +++++++++++++++++++ .../net/snowflake/client/core/HttpUtil.java | 15 +++++++++++++-- .../client/jdbc/CustomProxyLatestIT.java | 8 ++++---- 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 src/main/java/net/snowflake/client/core/HttpProtocol.java diff --git a/src/main/java/net/snowflake/client/core/HttpClientSettingsKey.java b/src/main/java/net/snowflake/client/core/HttpClientSettingsKey.java index 183655be7..33f6b3ef8 100644 --- a/src/main/java/net/snowflake/client/core/HttpClientSettingsKey.java +++ b/src/main/java/net/snowflake/client/core/HttpClientSettingsKey.java @@ -4,7 +4,6 @@ package net.snowflake.client.core; -import com.amazonaws.Protocol; import com.google.common.base.Strings; import java.io.Serializable; @@ -132,8 +131,8 @@ public String getNonProxyHosts() { return this.nonProxyHosts; } - public Protocol getProxyProtocol() { - return this.proxyProtocol.equalsIgnoreCase("https") ? Protocol.HTTPS : Protocol.HTTP; + public HttpProtocol getProxyProtocol() { + return this.proxyProtocol.equalsIgnoreCase("https") ? HttpProtocol.HTTPS : HttpProtocol.HTTP; } public Boolean getGzipDisabled() { diff --git a/src/main/java/net/snowflake/client/core/HttpProtocol.java b/src/main/java/net/snowflake/client/core/HttpProtocol.java new file mode 100644 index 000000000..3975e4167 --- /dev/null +++ b/src/main/java/net/snowflake/client/core/HttpProtocol.java @@ -0,0 +1,19 @@ +package net.snowflake.client.core; + +public enum HttpProtocol { + + HTTP("http"), + + HTTPS("https"); + + private final String protocol; + + HttpProtocol(String protocol) { + this.protocol = protocol; + } + + @Override + public String toString() { + return protocol; + } +} diff --git a/src/main/java/net/snowflake/client/core/HttpUtil.java b/src/main/java/net/snowflake/client/core/HttpUtil.java index 628d0e28d..3338169cf 100644 --- a/src/main/java/net/snowflake/client/core/HttpUtil.java +++ b/src/main/java/net/snowflake/client/core/HttpUtil.java @@ -123,7 +123,7 @@ public static void closeExpiredAndIdleConnections() { */ public static void setProxyForS3(HttpClientSettingsKey key, ClientConfiguration clientConfig) { if (key != null && key.usesProxy()) { - clientConfig.setProxyProtocol(key.getProxyProtocol()); + clientConfig.setProxyProtocol(toAwsProtocol(key.getProxyProtocol())); clientConfig.setProxyHost(key.getProxyHost()); clientConfig.setProxyPort(key.getProxyPort()); clientConfig.setNonProxyHosts(key.getNonProxyHosts()); @@ -135,6 +135,17 @@ public static void setProxyForS3(HttpClientSettingsKey key, ClientConfiguration } } + private static Protocol toAwsProtocol(HttpProtocol protocol) { + switch (protocol) { + case HTTP: + return Protocol.HTTP; + case HTTPS: + return Protocol.HTTPS; + default: + throw new IllegalArgumentException("Unknown protocol: " + protocol); + } + } + /** * A static function to set S3 proxy params for sessionless connections using the proxy params * from the StageInfo @@ -379,7 +390,7 @@ public static CloseableHttpClient buildHttpClient( new SnowflakeMutableProxyRoutePlanner( key.getProxyHost(), key.getProxyPort(), - key.getProxyProtocol(), + toAwsProtocol(key.getProxyProtocol()), key.getNonProxyHosts())); httpClientBuilder = httpClientBuilder.setProxy(proxy).setRoutePlanner(sdkProxyRoutePlanner); if (!Strings.isNullOrEmpty(key.getProxyUser()) diff --git a/src/test/java/net/snowflake/client/jdbc/CustomProxyLatestIT.java b/src/test/java/net/snowflake/client/jdbc/CustomProxyLatestIT.java index b8379f985..54320beb1 100644 --- a/src/test/java/net/snowflake/client/jdbc/CustomProxyLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/CustomProxyLatestIT.java @@ -9,7 +9,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertTrue; -import com.amazonaws.Protocol; import java.io.File; import java.net.Authenticator; import java.net.PasswordAuthentication; @@ -17,6 +16,7 @@ import java.util.Properties; import net.snowflake.client.category.TestCategoryOthers; import net.snowflake.client.core.HttpClientSettingsKey; +import net.snowflake.client.core.HttpProtocol; import net.snowflake.client.core.HttpUtil; import net.snowflake.client.core.SFSession; import net.snowflake.common.core.SqlState; @@ -596,7 +596,7 @@ public void testSetJVMProxyHttp() throws SQLException { "jdbc:snowflake://s3testaccount.us-east-1.snowflakecomputing.com", props); SFSession sfSession = con.unwrap(SnowflakeConnectionV1.class).getSfSession(); HttpClientSettingsKey clientSettingsKey = sfSession.getHttpClientKey(); - assertEquals(Protocol.HTTP, clientSettingsKey.getProxyProtocol()); + assertEquals(HttpProtocol.HTTP, clientSettingsKey.getProxyProtocol()); con.close(); } @@ -622,7 +622,7 @@ public void testSetJVMProxyHttps() throws SQLException { "jdbc:snowflake://s3testaccount.us-east-1.snowflakecomputing.com", props); SFSession sfSession = con.unwrap(SnowflakeConnectionV1.class).getSfSession(); HttpClientSettingsKey clientSettingsKey = sfSession.getHttpClientKey(); - assertEquals(Protocol.HTTPS, clientSettingsKey.getProxyProtocol()); + assertEquals(HttpProtocol.HTTPS, clientSettingsKey.getProxyProtocol()); con.close(); } @@ -647,7 +647,7 @@ public void testSetJVMProxyDefaultHttps() throws SQLException { "jdbc:snowflake://s3testaccount.us-east-1.snowflakecomputing.com", props); SFSession sfSession = con.unwrap(SnowflakeConnectionV1.class).getSfSession(); HttpClientSettingsKey clientSettingsKey = sfSession.getHttpClientKey(); - assertEquals(Protocol.HTTPS, clientSettingsKey.getProxyProtocol()); + assertEquals(HttpProtocol.HTTPS, clientSettingsKey.getProxyProtocol()); con.close(); }