Last active
March 21, 2022 12:10
-
-
Save boly38/31dfad5da388567e02e4 to your computer and use it in GitHub Desktop.
following this question http://stackoverflow.com/questions/10297837/set-nonproxyhosts-in-apache-httpclient-4-1-3#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.http.HttpException; | |
import org.apache.http.HttpHost; | |
import org.apache.http.HttpRequest; | |
import org.apache.http.conn.SchemePortResolver; | |
import org.apache.http.impl.conn.DefaultRoutePlanner; | |
import org.apache.http.protocol.HttpContext; | |
import org.slf4j.LoggerFactory; | |
/** | |
* ***[recommended]**** Use System default planner like this : | |
HttpClientBuilder getClientBuilder() { | |
HttpClientBuilder clientBuilder = HttpClientBuilder.create(); | |
clientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault())); | |
return clientBuilder; | |
} | |
* | |
* Or customize your own : | |
HttpClientBuilder getClientBuilder() { | |
HttpClientBuilder clientBuilder = HttpClientBuilder.create(); | |
EnvBasedProxyRoutePlanner routePlanner = new EnvBasedProxyRoutePlanner(null);// | |
clientBuilder.setRoutePlanner(routePlanner); | |
return clientBuilder; | |
} | |
* For example add extra JVM properties: | |
* -Dhttp.proxyHost=myProxy -Dhttp.myProxy=3128 -Dhttps.nonProxyHosts=localhost|127.0.* -Dhttps.proxyHost=myProxy -Dhttps.proxyPort=3128 | |
* | |
*/ | |
public class EnvBasedProxyRoutePlanner extends DefaultRoutePlanner { | |
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(EnvBasedProxyRoutePlanner.class); | |
public EnvBasedProxyRoutePlanner(SchemePortResolver schemePortResolver) { | |
super(schemePortResolver); | |
} | |
private HttpHost getProxy(String scheme) { | |
LOG.debug("get proxy for scheme {}", scheme); | |
String proto = scheme; | |
String proxyHostKey = proto + ".proxyHost"; | |
String proxyPortKey = proto + ".proxyPort"; | |
String protoProxyHost = System.getProperty(proxyHostKey); | |
if (protoProxyHost == null) { | |
protoProxyHost = System.getenv(proxyHostKey); | |
} | |
if (protoProxyHost == null) { | |
return null; | |
} | |
String proxyPortStr = System.getProperty(proxyPortKey); | |
if (proxyPortStr == null) { | |
proxyPortStr = System.getenv(proxyPortKey); | |
} | |
if (proxyPortStr == null) { | |
return null; | |
} | |
int protoProxyPort = -1; | |
if (proxyPortStr != null) { | |
try { | |
protoProxyPort = Integer.valueOf(proxyPortStr); | |
} catch (NumberFormatException nfe) { | |
LOG.warn("invalid {} : {} proxy will be ignored", proxyPortKey, proxyPortStr); | |
return null; | |
} | |
} | |
if (protoProxyPort < 1) { | |
return null; | |
} | |
LOG.debug("set {} proxy '{}:{}'", proto, protoProxyHost, protoProxyPort); | |
return new HttpHost(protoProxyHost, protoProxyPort, "http"); | |
} | |
private String[] getNonProxyHosts(String uriScheme) { | |
String nonproxyHostKey = uriScheme + ".nonProxyHosts"; | |
String nonproxyHost = System.getProperty(nonproxyHostKey); | |
if (nonproxyHost == null) { | |
nonproxyHost = System.getenv(nonproxyHostKey); | |
} | |
if (nonproxyHost == null) { | |
return null; | |
} | |
return nonproxyHost.split("\\|"); | |
} | |
private boolean doesTargetMatchNonProxy(HttpHost target) { | |
String uriHost = target.getHostName(); | |
String uriScheme = target.getSchemeName(); | |
String[] nonProxyHosts = getNonProxyHosts(uriScheme); | |
int nphLength = nonProxyHosts != null ? nonProxyHosts.length : 0; | |
if (nonProxyHosts == null || nphLength < 1) { | |
LOG.debug("sheme:'{}', host:'{}' : DEFAULT (0 non proxy host)", uriScheme, uriHost); | |
return false; | |
} | |
for (String nonProxyHost : nonProxyHosts) { | |
if (uriHost.matches(nonProxyHost)) { | |
LOG.debug("sheme:'{}', host:'{}' matches nonProxyHost '{}' : NO PROXY", uriScheme, uriHost, nonProxyHost); | |
return true; | |
} | |
} | |
LOG.debug("sheme:'{}', host:'{}' : DEFAULT (no match of {} non proxy host)", uriScheme, uriHost, nphLength); | |
return false; | |
} | |
@Override | |
protected HttpHost determineProxy( | |
final HttpHost target, | |
final HttpRequest request, | |
final HttpContext context) throws HttpException { | |
if (doesTargetMatchNonProxy(target)) { | |
return null; | |
} | |
return getProxy(target.getSchemeName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment