Skip to content

Commit

Permalink
Disable socket factory for pre JDK11
Browse files Browse the repository at this point in the history
  • Loading branch information
Lewuathe committed Feb 2, 2020
1 parent 1d79fe1 commit 9927836
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
5 changes: 2 additions & 3 deletions presto-cli/src/main/java/io/prestosql/cli/QueryRunner.java
Expand Up @@ -17,7 +17,6 @@
import io.airlift.log.Logger;
import io.prestosql.client.ClientSession;
import io.prestosql.client.OkHttpUtil;
import io.prestosql.client.SocketChannelSocketFactory;
import io.prestosql.client.StatementClient;
import okhttp3.OkHttpClient;

Expand All @@ -30,6 +29,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static io.prestosql.client.ClientSession.stripTransactionId;
import static io.prestosql.client.OkHttpUtil.basicAuth;
import static io.prestosql.client.OkHttpUtil.setupChannelSocket;
import static io.prestosql.client.OkHttpUtil.setupCookieJar;
import static io.prestosql.client.OkHttpUtil.setupHttpProxy;
import static io.prestosql.client.OkHttpUtil.setupKerberos;
Expand Down Expand Up @@ -84,8 +84,7 @@ public QueryRunner(

OkHttpClient.Builder builder = new OkHttpClient.Builder();

builder.socketFactory(new SocketChannelSocketFactory());

setupChannelSocket(builder);
setupTimeouts(builder, 30, SECONDS);
setupCookieJar(builder);
setupSocksProxy(builder, socksProxy);
Expand Down
21 changes: 21 additions & 0 deletions presto-client/src/main/java/io/prestosql/client/OkHttpUtil.java
Expand Up @@ -14,6 +14,8 @@
package io.prestosql.client;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.base.StandardSystemProperty;
import com.google.common.net.HostAndPort;
import io.airlift.security.pem.PemReader;
import okhttp3.Credentials;
Expand Down Expand Up @@ -238,6 +240,25 @@ public static void setupSsl(
}
}

public static void setupChannelSocket(OkHttpClient.Builder clientBuilder)
{
// Enable socket factory only for pre JDK 11
if (!isAtLeastJava11()) {
clientBuilder.socketFactory(new SocketChannelSocketFactory());
}
}

private static boolean isAtLeastJava11()
{
String feature = Splitter.on(".").split(StandardSystemProperty.JAVA_VERSION.value()).iterator().next();
try {
return Integer.parseInt(feature) >= 11;
}
catch (NumberFormatException e) {
return false;
}
}

private static void validateCertificates(KeyStore keyStore)
throws GeneralSecurityException
{
Expand Down
17 changes: 12 additions & 5 deletions presto-jdbc/src/main/java/io/prestosql/jdbc/PrestoDriver.java
Expand Up @@ -13,7 +13,6 @@
*/
package io.prestosql.jdbc;

import io.prestosql.client.SocketChannelSocketFactory;
import okhttp3.OkHttpClient;

import java.io.Closeable;
Expand All @@ -30,6 +29,7 @@

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Strings.nullToEmpty;
import static io.prestosql.client.OkHttpUtil.setupChannelSocket;
import static io.prestosql.client.OkHttpUtil.userAgent;
import static java.lang.Integer.parseInt;

Expand All @@ -43,10 +43,7 @@ public class PrestoDriver

private static final String DRIVER_URL_START = "jdbc:presto:";

private final OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(userAgent(DRIVER_NAME + "/" + DRIVER_VERSION))
.socketFactory(new SocketChannelSocketFactory())
.build();
private final OkHttpClient httpClient = newHttpClient();

static {
String version = nullToEmpty(PrestoDriver.class.getPackage().getImplementationVersion());
Expand Down Expand Up @@ -138,4 +135,14 @@ public Logger getParentLogger()
// TODO: support java.util.Logging
throw new SQLFeatureNotSupportedException();
}

private OkHttpClient newHttpClient()
{
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.addInterceptor(userAgent(DRIVER_NAME + "/" + DRIVER_VERSION));

// Enable socket factory only for pre JDK 11
setupChannelSocket(builder);
return builder.build();
}
}

0 comments on commit 9927836

Please sign in to comment.