Moved route local address configuration from RequestConfig to DefaultRoutePlanner

This commit is contained in:
Oleg Kalnichevski 2017-11-17 18:23:31 +01:00
parent f182b73e48
commit 47dfbfa221
4 changed files with 17 additions and 39 deletions

View File

@ -27,7 +27,6 @@
package org.apache.hc.client5.http.config; package org.apache.hc.client5.http.config;
import java.net.InetAddress;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -50,7 +49,6 @@ public class RequestConfig implements Cloneable {
private final boolean expectContinueEnabled; private final boolean expectContinueEnabled;
private final HttpHost proxy; private final HttpHost proxy;
private final InetAddress localAddress;
private final String cookieSpec; private final String cookieSpec;
private final boolean redirectsEnabled; private final boolean redirectsEnabled;
private final boolean circularRedirectsAllowed; private final boolean circularRedirectsAllowed;
@ -67,14 +65,13 @@ public class RequestConfig implements Cloneable {
* Intended for CDI compatibility * Intended for CDI compatibility
*/ */
protected RequestConfig() { protected RequestConfig() {
this(false, null, null, null, false, false, 0, false, null, null, this(false, null, null, false, false, 0, false, null, null,
DEFAULT_CONNECTION_REQUEST_TIMEOUT, DEFAULT_CONNECT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT, false); DEFAULT_CONNECTION_REQUEST_TIMEOUT, DEFAULT_CONNECT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT, false);
} }
RequestConfig( RequestConfig(
final boolean expectContinueEnabled, final boolean expectContinueEnabled,
final HttpHost proxy, final HttpHost proxy,
final InetAddress localAddress,
final String cookieSpec, final String cookieSpec,
final boolean redirectsEnabled, final boolean redirectsEnabled,
final boolean circularRedirectsAllowed, final boolean circularRedirectsAllowed,
@ -89,7 +86,6 @@ public class RequestConfig implements Cloneable {
super(); super();
this.expectContinueEnabled = expectContinueEnabled; this.expectContinueEnabled = expectContinueEnabled;
this.proxy = proxy; this.proxy = proxy;
this.localAddress = localAddress;
this.cookieSpec = cookieSpec; this.cookieSpec = cookieSpec;
this.redirectsEnabled = redirectsEnabled; this.redirectsEnabled = redirectsEnabled;
this.circularRedirectsAllowed = circularRedirectsAllowed; this.circularRedirectsAllowed = circularRedirectsAllowed;
@ -139,21 +135,6 @@ public class RequestConfig implements Cloneable {
return proxy; return proxy;
} }
/**
* Returns local address to be used for request execution.
* <p>
* On machines with multiple network interfaces, this parameter
* can be used to select the network interface from which the
* connection originates.
* </p>
* <p>
* Default: {@code null}
* </p>
*/
public InetAddress getLocalAddress() {
return localAddress;
}
/** /**
* Determines the name of the cookie specification to be used for HTTP state * Determines the name of the cookie specification to be used for HTTP state
* management. * management.
@ -300,7 +281,6 @@ public class RequestConfig implements Cloneable {
builder.append("["); builder.append("[");
builder.append("expectContinueEnabled=").append(expectContinueEnabled); builder.append("expectContinueEnabled=").append(expectContinueEnabled);
builder.append(", proxy=").append(proxy); builder.append(", proxy=").append(proxy);
builder.append(", localAddress=").append(localAddress);
builder.append(", cookieSpec=").append(cookieSpec); builder.append(", cookieSpec=").append(cookieSpec);
builder.append(", redirectsEnabled=").append(redirectsEnabled); builder.append(", redirectsEnabled=").append(redirectsEnabled);
builder.append(", maxRedirects=").append(maxRedirects); builder.append(", maxRedirects=").append(maxRedirects);
@ -324,7 +304,6 @@ public class RequestConfig implements Cloneable {
return new Builder() return new Builder()
.setExpectContinueEnabled(config.isExpectContinueEnabled()) .setExpectContinueEnabled(config.isExpectContinueEnabled())
.setProxy(config.getProxy()) .setProxy(config.getProxy())
.setLocalAddress(config.getLocalAddress())
.setCookieSpec(config.getCookieSpec()) .setCookieSpec(config.getCookieSpec())
.setRedirectsEnabled(config.isRedirectsEnabled()) .setRedirectsEnabled(config.isRedirectsEnabled())
.setCircularRedirectsAllowed(config.isCircularRedirectsAllowed()) .setCircularRedirectsAllowed(config.isCircularRedirectsAllowed())
@ -342,7 +321,6 @@ public class RequestConfig implements Cloneable {
private boolean expectContinueEnabled; private boolean expectContinueEnabled;
private HttpHost proxy; private HttpHost proxy;
private InetAddress localAddress;
private String cookieSpec; private String cookieSpec;
private boolean redirectsEnabled; private boolean redirectsEnabled;
private boolean circularRedirectsAllowed; private boolean circularRedirectsAllowed;
@ -376,11 +354,6 @@ public class RequestConfig implements Cloneable {
return this; return this;
} }
public Builder setLocalAddress(final InetAddress localAddress) {
this.localAddress = localAddress;
return this;
}
public Builder setCookieSpec(final String cookieSpec) { public Builder setCookieSpec(final String cookieSpec) {
this.cookieSpec = cookieSpec; this.cookieSpec = cookieSpec;
return this; return this;
@ -455,7 +428,6 @@ public class RequestConfig implements Cloneable {
return new RequestConfig( return new RequestConfig(
expectContinueEnabled, expectContinueEnabled,
proxy, proxy,
localAddress,
cookieSpec, cookieSpec,
redirectsEnabled, redirectsEnabled,
circularRedirectsAllowed, circularRedirectsAllowed,

View File

@ -148,7 +148,7 @@ public class ProxyClient {
} }
final HttpRoute route = new HttpRoute( final HttpRoute route = new HttpRoute(
host, host,
this.requestConfig.getLocalAddress(), null,
proxy, false, TunnelType.TUNNELLED, LayerType.PLAIN); proxy, false, TunnelType.TUNNELLED, LayerType.PLAIN);
final ManagedHttpClientConnection conn = this.connFactory.createConnection(null); final ManagedHttpClientConnection conn = this.connFactory.createConnection(null);

View File

@ -63,13 +63,12 @@ public class DefaultRoutePlanner implements HttpRoutePlanner {
} }
@Override @Override
public HttpRoute determineRoute(final HttpHost host, final HttpContext context) throws HttpException { public final HttpRoute determineRoute(final HttpHost host, final HttpContext context) throws HttpException {
if (host == null) { if (host == null) {
throw new ProtocolException("Target host is not specified"); throw new ProtocolException("Target host is not specified");
} }
final HttpClientContext clientContext = HttpClientContext.adapt(context); final HttpClientContext clientContext = HttpClientContext.adapt(context);
final RequestConfig config = clientContext.getRequestConfig(); final RequestConfig config = clientContext.getRequestConfig();
final InetAddress local = config.getLocalAddress();
HttpHost proxy = config.getProxy(); HttpHost proxy = config.getProxy();
if (proxy == null) { if (proxy == null) {
proxy = determineProxy(host, context); proxy = determineProxy(host, context);
@ -90,14 +89,14 @@ public class DefaultRoutePlanner implements HttpRoutePlanner {
} }
final boolean secure = target.getSchemeName().equalsIgnoreCase("https"); final boolean secure = target.getSchemeName().equalsIgnoreCase("https");
if (proxy == null) { if (proxy == null) {
return new HttpRoute(target, local, secure); return new HttpRoute(target, determineLocalAddress(target, context), secure);
} else { } else {
return new HttpRoute(target, local, proxy, secure); return new HttpRoute(target, determineLocalAddress(proxy, context), proxy, secure);
} }
} }
@Override @Override
public HttpHost determineTargetHost(final HttpRequest request, final HttpContext context) throws HttpException { public final HttpHost determineTargetHost(final HttpRequest request, final HttpContext context) throws HttpException {
final URIAuthority authority = request.getAuthority(); final URIAuthority authority = request.getAuthority();
if (authority != null) { if (authority != null) {
final String scheme = request.getScheme(); final String scheme = request.getScheme();
@ -121,4 +120,15 @@ public class DefaultRoutePlanner implements HttpRoutePlanner {
return null; return null;
} }
/**
* This implementation returns null.
*
* @throws HttpException may be thrown if overridden
*/
protected InetAddress determineLocalAddress(
final HttpHost firstHop,
final HttpContext context) throws HttpException {
return null;
}
} }

View File

@ -27,7 +27,6 @@
package org.apache.hc.client5.http.config; package org.apache.hc.client5.http.config;
import java.net.InetAddress;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -57,7 +56,6 @@ public class TestRequestConfig {
Assert.assertEquals(false, config.isCircularRedirectsAllowed()); Assert.assertEquals(false, config.isCircularRedirectsAllowed());
Assert.assertEquals(50, config.getMaxRedirects()); Assert.assertEquals(50, config.getMaxRedirects());
Assert.assertEquals(null, config.getCookieSpec()); Assert.assertEquals(null, config.getCookieSpec());
Assert.assertEquals(null, config.getLocalAddress());
Assert.assertEquals(null, config.getProxy()); Assert.assertEquals(null, config.getProxy());
Assert.assertEquals(null, config.getTargetPreferredAuthSchemes()); Assert.assertEquals(null, config.getTargetPreferredAuthSchemes());
Assert.assertEquals(null, config.getProxyPreferredAuthSchemes()); Assert.assertEquals(null, config.getProxyPreferredAuthSchemes());
@ -76,7 +74,6 @@ public class TestRequestConfig {
.setCircularRedirectsAllowed(true) .setCircularRedirectsAllowed(true)
.setMaxRedirects(100) .setMaxRedirects(100)
.setCookieSpec(CookieSpecs.STANDARD) .setCookieSpec(CookieSpecs.STANDARD)
.setLocalAddress(InetAddress.getLocalHost())
.setProxy(new HttpHost("someproxy")) .setProxy(new HttpHost("someproxy"))
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM)) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM))
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.DIGEST))
@ -92,7 +89,6 @@ public class TestRequestConfig {
Assert.assertEquals(true, config.isCircularRedirectsAllowed()); Assert.assertEquals(true, config.isCircularRedirectsAllowed());
Assert.assertEquals(100, config.getMaxRedirects()); Assert.assertEquals(100, config.getMaxRedirects());
Assert.assertEquals(CookieSpecs.STANDARD, config.getCookieSpec()); Assert.assertEquals(CookieSpecs.STANDARD, config.getCookieSpec());
Assert.assertEquals(InetAddress.getLocalHost(), config.getLocalAddress());
Assert.assertEquals(new HttpHost("someproxy"), config.getProxy()); Assert.assertEquals(new HttpHost("someproxy"), config.getProxy());
Assert.assertEquals(Arrays.asList(AuthSchemes.NTLM), config.getTargetPreferredAuthSchemes()); Assert.assertEquals(Arrays.asList(AuthSchemes.NTLM), config.getTargetPreferredAuthSchemes());
Assert.assertEquals(Arrays.asList(AuthSchemes.DIGEST), config.getProxyPreferredAuthSchemes()); Assert.assertEquals(Arrays.asList(AuthSchemes.DIGEST), config.getProxyPreferredAuthSchemes());