SchemeRegistry instance set in the execution context takes precedence over the default one (related to HTTPASYNC-25)

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1382350 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2012-09-08 19:56:35 +00:00
parent 90b8646309
commit e42006ea27
4 changed files with 41 additions and 7 deletions

View File

@ -156,8 +156,9 @@ class InternalHttpClient extends AbstractBasicHttpClient {
HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request); HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request);
wrapper.setParams(params); wrapper.setParams(params);
wrapper.setVirtualHost(virtualHost); wrapper.setVirtualHost(virtualHost);
HttpRoute route = determineRoute(target, wrapper, context); HttpContext localcontext = setupContext(context);
return this.execChain.execute(route, wrapper, setupContext(context), execListner); HttpRoute route = determineRoute(target, wrapper, localcontext);
return this.execChain.execute(route, wrapper, localcontext, execListner);
} catch (HttpException httpException) { } catch (HttpException httpException) {
throw new ClientProtocolException(httpException); throw new ClientProtocolException(httpException);
} }

View File

@ -62,6 +62,7 @@ import org.apache.http.conn.routing.BasicRouteDirector;
import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.routing.HttpRouteDirector; import org.apache.http.conn.routing.HttpRouteDirector;
import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.HttpAuthenticator; import org.apache.http.impl.client.HttpAuthenticator;
import org.apache.http.impl.client.RequestAbortedException; import org.apache.http.impl.client.RequestAbortedException;
@ -453,6 +454,15 @@ public class MainClientExec implements ClientExecChain {
} while (step > HttpRouteDirector.COMPLETE); } while (step > HttpRouteDirector.COMPLETE);
} }
private SchemeRegistry getSchemeRegistry(final HttpContext context) {
SchemeRegistry reg = (SchemeRegistry) context.getAttribute(
ClientContext.SCHEME_REGISTRY);
if (reg == null) {
reg = this.connManager.getSchemeRegistry();
}
return reg;
}
/** /**
* Creates a tunnel to the target server. * Creates a tunnel to the target server.
* The connection must be established to the (last) proxy. * The connection must be established to the (last) proxy.
@ -476,8 +486,8 @@ public class MainClientExec implements ClientExecChain {
String host = target.getHostName(); String host = target.getHostName();
int port = target.getPort(); int port = target.getPort();
if (port < 0) { if (port < 0) {
Scheme scheme = connManager.getSchemeRegistry(). SchemeRegistry registry = getSchemeRegistry(context);
getScheme(target.getSchemeName()); Scheme scheme = registry.getScheme(target.getSchemeName());
port = scheme.getDefaultPort(); port = scheme.getDefaultPort();
} }

View File

@ -43,6 +43,7 @@ import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException; import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.conn.HttpInetSocketAddress; import org.apache.http.conn.HttpInetSocketAddress;
@ -137,6 +138,15 @@ public class DefaultClientConnectionOperator implements ClientConnectionOperator
return new DefaultClientConnection(); return new DefaultClientConnection();
} }
private SchemeRegistry getSchemeRegistry(final HttpContext context) {
SchemeRegistry reg = (SchemeRegistry) context.getAttribute(
ClientContext.SCHEME_REGISTRY);
if (reg == null) {
reg = this.schemeRegistry;
}
return reg;
}
public void openConnection( public void openConnection(
final OperatedClientConnection conn, final OperatedClientConnection conn,
final HttpHost target, final HttpHost target,
@ -156,7 +166,8 @@ public class DefaultClientConnectionOperator implements ClientConnectionOperator
throw new IllegalStateException("Connection must not be open"); throw new IllegalStateException("Connection must not be open");
} }
Scheme schm = schemeRegistry.getScheme(target.getSchemeName()); SchemeRegistry registry = getSchemeRegistry(context);
Scheme schm = registry.getScheme(target.getSchemeName());
SchemeSocketFactory sf = schm.getSchemeSocketFactory(); SchemeSocketFactory sf = schm.getSchemeSocketFactory();
InetAddress[] addresses = resolveHostname(target.getHostName()); InetAddress[] addresses = resolveHostname(target.getHostName());
@ -219,7 +230,8 @@ public class DefaultClientConnectionOperator implements ClientConnectionOperator
throw new IllegalStateException("Connection must be open"); throw new IllegalStateException("Connection must be open");
} }
final Scheme schm = schemeRegistry.getScheme(target.getSchemeName()); SchemeRegistry registry = getSchemeRegistry(context);
Scheme schm = registry.getScheme(target.getSchemeName());
if (!(schm.getSchemeSocketFactory() instanceof SchemeLayeredSocketFactory)) { if (!(schm.getSchemeSocketFactory() instanceof SchemeLayeredSocketFactory)) {
throw new IllegalArgumentException throw new IllegalArgumentException
("Target scheme (" + schm.getName() + ("Target scheme (" + schm.getName() +

View File

@ -37,6 +37,7 @@ import org.apache.http.HttpHost;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.routing.HttpRoutePlanner; import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.Scheme;
@ -79,6 +80,15 @@ public class DefaultHttpRoutePlanner implements HttpRoutePlanner {
schemeRegistry = schreg; schemeRegistry = schreg;
} }
private SchemeRegistry getSchemeRegistry(final HttpContext context) {
SchemeRegistry reg = (SchemeRegistry) context.getAttribute(
ClientContext.SCHEME_REGISTRY);
if (reg == null) {
reg = this.schemeRegistry;
}
return reg;
}
public HttpRoute determineRoute(HttpHost target, public HttpRoute determineRoute(HttpHost target,
HttpRequest request, HttpRequest request,
HttpContext context) HttpContext context)
@ -110,7 +120,8 @@ public class DefaultHttpRoutePlanner implements HttpRoutePlanner {
final Scheme schm; final Scheme schm;
try { try {
schm = schemeRegistry.getScheme(target.getSchemeName()); SchemeRegistry registry = getSchemeRegistry(context);
schm = registry.getScheme(target.getSchemeName());
} catch (IllegalStateException ex) { } catch (IllegalStateException ex) {
throw new HttpException(ex.getMessage()); throw new HttpException(ex.getMessage());
} }