[AMQ-9102] Add http.nonProxyHost support in HTTP transport

(cherry picked from commit 44a5539afc)
This commit is contained in:
Jean-Baptiste Onofré 2022-11-21 16:58:29 +01:00
parent b5126f6e7b
commit 60e79ed3d5
2 changed files with 33 additions and 2 deletions

View File

@ -19,8 +19,11 @@ package org.apache.activemq.transport.http;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InterruptedIOException; import java.io.InterruptedIOException;
import java.net.URI; import java.net.*;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
@ -56,6 +59,7 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
import org.apache.http.message.AbstractHttpMessage; import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
@ -320,7 +324,7 @@ public class HttpClientTransport extends HttpTransportSupport {
clientBuilder.addInterceptorLast(new HttpRequestInterceptor() { clientBuilder.addInterceptorLast(new HttpRequestInterceptor() {
@Override @Override
public void process(HttpRequest request, HttpContext context) { public void process(HttpRequest request, HttpContext context) {
// We expect to received a compression response that we un-gzip // We expect to receive a compression response that we un-gzip
request.addHeader("Accept-Encoding", "gzip"); request.addHeader("Accept-Encoding", "gzip");
} }
}); });
@ -328,6 +332,23 @@ public class HttpClientTransport extends HttpTransportSupport {
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
if (getProxyHost() != null) { if (getProxyHost() != null) {
if (getNonProxyHosts() != null) {
List<String> nonProxyHosts = Arrays.asList(getNonProxyHosts().split("\\|"));
ProxySelector proxySelector = new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
return Collections.singletonList(nonProxyHosts.contains(uri.getHost()) ? Proxy.NO_PROXY : new Proxy(Proxy.Type.HTTP, new InetSocketAddress(getProxyHost(), getProxyPort())));
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
LOG.warn("Connect to {} failed", uri, ioe);
}
};
clientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(proxySelector));
}
clientBuilder.useSystemProperties();
HttpHost proxy = new HttpHost(getProxyHost(), getProxyPort()); HttpHost proxy = new HttpHost(getProxyHost(), getProxyPort());
requestConfigBuilder.setProxy(proxy); requestConfigBuilder.setProxy(proxy);

View File

@ -29,6 +29,7 @@ import org.apache.activemq.transport.util.TextWireFormat;
public abstract class HttpTransportSupport extends TransportThreadSupport { public abstract class HttpTransportSupport extends TransportThreadSupport {
private static final int DEFAULT_PROXY_PORT = 8080; private static final int DEFAULT_PROXY_PORT = 8080;
private static final String PROPERTY_PROXY_HOST = "proxyHost"; private static final String PROPERTY_PROXY_HOST = "proxyHost";
private static final String PROPERTY_NON_PROXY_HOST = "nonProxyHosts";
private static final String PROPERTY_PROXY_PORT = "proxyPort"; private static final String PROPERTY_PROXY_PORT = "proxyPort";
private static final String PROPERTY_PROXY_USER = "proxyUser"; private static final String PROPERTY_PROXY_USER = "proxyUser";
private static final String PROPERTY_PROXY_PASSWORD = "proxyPassword"; private static final String PROPERTY_PROXY_PASSWORD = "proxyPassword";
@ -36,6 +37,7 @@ public abstract class HttpTransportSupport extends TransportThreadSupport {
private TextWireFormat textWireFormat; private TextWireFormat textWireFormat;
private URI remoteUrl; private URI remoteUrl;
private String proxyHost; private String proxyHost;
private String nonProxyHosts;
private Integer proxyPort; private Integer proxyPort;
private String proxyUser; private String proxyUser;
private String proxyPassword; private String proxyPassword;
@ -75,6 +77,14 @@ public abstract class HttpTransportSupport extends TransportThreadSupport {
this.proxyHost = proxyHost; this.proxyHost = proxyHost;
} }
public String getNonProxyHosts() {
return nonProxyHosts != null ? nonProxyHosts : getSystemProperty(PROPERTY_NON_PROXY_HOST);
}
public void setNonProxyHosts(String nonProxyHosts) {
this.nonProxyHosts = nonProxyHosts;
}
public int getProxyPort() { public int getProxyPort() {
return proxyPort != null ? proxyPort return proxyPort != null ? proxyPort
: (getSystemProperty(PROPERTY_PROXY_PORT) != null : (getSystemProperty(PROPERTY_PROXY_PORT) != null