package com.facebook.presto.jdbc;

import com.facebook.presto.client.ClientSession;
import com.facebook.presto.client.QueryResults;
import com.facebook.presto.client.StatementClient;
import com.google.common.collect.ImmutableSet;
import io.airlift.http.client.AsyncHttpClient;
import io.airlift.http.client.HttpClientConfig;
import io.airlift.http.client.netty.NettyAsyncHttpClientConfig;
import io.airlift.http.client.netty.NettyIoPoolConfig;
import io.airlift.http.client.netty.StandaloneNettyAsyncHttpClient;
import io.airlift.json.JsonCodec;
import io.airlift.units.Duration;

import java.io.Closeable;
import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.airlift.json.JsonCodec.jsonCodec;

public class QueryExecutor
        implements Closeable
{
    private final JsonCodec<QueryResults> queryInfoCodec;
    private final AsyncHttpClient httpClient;

    private QueryExecutor(String userAgent, JsonCodec<QueryResults> queryResultsCodec)
    {
        checkNotNull(userAgent, "userAgent is null");
        checkNotNull(queryResultsCodec, "queryResultsCodec is null");

        this.queryInfoCodec = queryResultsCodec;
        this.httpClient = new StandaloneNettyAsyncHttpClient("jdbc",
                new HttpClientConfig()
                        .setConnectTimeout(new Duration(10, TimeUnit.SECONDS)),
                new NettyAsyncHttpClientConfig(),
                new NettyIoPoolConfig(),
                ImmutableSet.of(new UserAgentRequestFilter(userAgent)));
    }

    public StatementClient startQuery(ClientSession session, String query)
    {
        return new StatementClient(httpClient, queryInfoCodec, session, query);
    }

    @Override
    public void close()
    {
        httpClient.close();
    }

    // TODO: replace this with a phantom reference
    @SuppressWarnings("FinalizeDeclaration")
    @Override
    protected void finalize()
    {
        close();
    }

    public static QueryExecutor create(String userAgent)
    {
        return new QueryExecutor(userAgent, jsonCodec(QueryResults.class));
    }
}

@@ -223,16 +223,58 @@
 bleSet;%0A
+import com.google.common.net.HostAndPort;%0A
 import i
@@ -631,21 +631,166 @@
 java
-.io.Closeable
+x.annotation.Nullable;%0A%0Aimport java.io.Closeable;%0Aimport java.net.InetSocketAddress;%0Aimport java.net.Proxy;%0Aimport java.net.ProxySelector;%0Aimport java.net.URI
 ;%0Aim
@@ -1191,16 +1191,40 @@
 ltsCodec
+, HostAndPort socksProxy
 )%0A    %7B%0A
@@ -1583,16 +1583,67 @@
 ECONDS))
+%0A                        .setSocksProxy(socksProxy)
 ,%0A      
@@ -2360,17 +2360,604 @@
 s.class)
-)
+, getSystemSocksProxy());%0A    %7D%0A%0A    @Nullable%0A    private static HostAndPort getSystemSocksProxy()%0A    %7B%0A        URI uri = URI.create(%22socket://0.0.0.0:80%22);%0A        for (Proxy proxy : ProxySelector.getDefault().select(uri)) %7B%0A            if (proxy.type() == Proxy.Type.SOCKS) %7B%0A                if (proxy.address() instanceof InetSocketAddress) %7B%0A                    InetSocketAddress address = (InetSocketAddress) proxy.address();%0A                    return HostAndPort.fromParts(address.getHostString(), address.getPort());%0A                %7D%0A            %7D%0A        %7D%0A        return null
 ;%0A    %7D%0A

Use system proxy settings for JDBC driver