From 2900b5c687f7a2e11bf1b56ddc17aa271e557ff2 Mon Sep 17 00:00:00 2001 From: ey1984 Date: Mon, 30 Apr 2018 23:31:39 +0200 Subject: [PATCH 1/2] Get proxy system --- .../org/apache/hive/jdbc/HiveConnection.java | 71 ++++++++++++++++--- 1 file changed, 62 insertions(+), 9 deletions(-) diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index 8fb1d126ce96..6ba2ddb0bd42 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -18,6 +18,8 @@ package org.apache.hive.jdbc; +import org.apache.commons.lang.math.NumberUtils; +import org.apache.http.HttpHost; import java.io.FileInputStream; import java.io.IOException; import java.security.KeyStore; @@ -39,12 +41,8 @@ import java.sql.Savepoint; import java.sql.Statement; import java.sql.Struct; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.Properties; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; @@ -54,6 +52,7 @@ import javax.security.sasl.Sasl; import javax.security.sasl.SaslException; +import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.protocol.HttpContext; import org.apache.commons.logging.Log; @@ -104,6 +103,8 @@ public class HiveConnection implements java.sql.Connection { private static final String HIVE_VAR_PREFIX = "hivevar:"; private static final String HIVE_CONF_PREFIX = "hiveconf:"; + private static final String HTTP = "http"; + private static final String HTTP_PROXY = "HTTP_PROXY"; private String jdbcUriString; private String host; private int port; @@ -392,10 +393,18 @@ public long getRetryInterval() { } socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); - final Registry registry = - RegistryBuilder.create() - .register("https", socketFactory) - .build(); + HttpHost proxy = getProxySettings(); + httpClientBuilder.setProxy(proxy); + + RegistryBuilder registryBuilder = RegistryBuilder. create() + .register("https", socketFactory); + + if (proxy != null && HTTP.equalsIgnoreCase(proxy.getSchemeName())) { + registryBuilder.register("http", PlainConnectionSocketFactory.INSTANCE); + } + + final Registry registry = registryBuilder.build(); + httpClientBuilder.setConnectionManager(new BasicHttpClientConnectionManager(registry)); } @@ -408,6 +417,50 @@ public long getRetryInterval() { return httpClientBuilder.build(); } + private HttpHost getProxySettings() { + + String httpProxyFromEnv = System.getenv(HTTP_PROXY); + + if (httpProxyFromEnv != null) { + return HttpHost.create(httpProxyFromEnv); + } + + String http = System.getProperty(HTTP + ".proxyHost"); + + if (http != null) { + return getProxySettings(HTTP); + } + + LOG.warn("Proxy not settings"); + + return null; + } + + private HttpHost getProxySettings(String protocol) { + if (HTTP.equalsIgnoreCase(protocol)) { + String host = System.getProperty(protocol + ".proxyHost", ""); + Integer port = getPortFromProxy(protocol); + return new HttpHost(host, port, protocol ); + } + + LOG.warn("Proxy not settings"); + + return null; + + } + + private Integer getPortFromProxy(String protocol) { + String port = System.getProperty(protocol + ".proxyPort"); + if (port != null && NumberUtils.isNumber(port)) { + return Integer.valueOf(port); + } + + LOG.warn("Proxy Port not defined or not a number"); + + return -1; + } + + /** * Create transport per the connection options * Supported transport options are: From 3962b9b3b54f5e07854331d989911b645ef344bd Mon Sep 17 00:00:00 2001 From: Ekrem YILMAZ Date: Mon, 14 May 2018 14:06:21 +0200 Subject: [PATCH 2/2] Get Proxy Settings By UseSystemProperties Get Proxy Settings by UseSystemProperties and keep getEnv --- .../org/apache/hive/jdbc/HiveConnection.java | 33 +------------------ 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index 6ba2ddb0bd42..9ff9168e5e8e 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -405,7 +405,6 @@ public long getRetryInterval() { final Registry registry = registryBuilder.build(); - httpClientBuilder.setConnectionManager(new BasicHttpClientConnectionManager(registry)); } catch (Exception e) { @@ -414,7 +413,7 @@ public long getRetryInterval() { throw new SQLException(msg, " 08S01", e); } } - return httpClientBuilder.build(); + return httpClientBuilder.useSystemProperties().build(); } private HttpHost getProxySettings() { @@ -425,41 +424,11 @@ private HttpHost getProxySettings() { return HttpHost.create(httpProxyFromEnv); } - String http = System.getProperty(HTTP + ".proxyHost"); - - if (http != null) { - return getProxySettings(HTTP); - } - LOG.warn("Proxy not settings"); return null; } - private HttpHost getProxySettings(String protocol) { - if (HTTP.equalsIgnoreCase(protocol)) { - String host = System.getProperty(protocol + ".proxyHost", ""); - Integer port = getPortFromProxy(protocol); - return new HttpHost(host, port, protocol ); - } - - LOG.warn("Proxy not settings"); - - return null; - - } - - private Integer getPortFromProxy(String protocol) { - String port = System.getProperty(protocol + ".proxyPort"); - if (port != null && NumberUtils.isNumber(port)) { - return Integer.valueOf(port); - } - - LOG.warn("Proxy Port not defined or not a number"); - - return -1; - } - /** * Create transport per the connection options