Deprecated HttpParams and related classes in favor of new configuration API (fixed remaining deprecation warnings)
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1414682 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
73dc5eebfd
commit
f7cf9a22e1
|
@ -33,9 +33,10 @@ import java.security.NoSuchAlgorithmException;
|
|||
import javax.net.ssl.SSLContext;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AUTH;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.ChallengeState;
|
||||
import org.apache.http.auth.Credentials;
|
||||
import org.apache.http.auth.MalformedChallengeException;
|
||||
import org.apache.http.auth.NTCredentials;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.AuthCache;
|
||||
|
@ -57,6 +58,7 @@ import org.apache.http.impl.client.BasicAuthCache;
|
|||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.builder.HttpClientBuilder;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
|
||||
/**
|
||||
|
@ -133,12 +135,22 @@ public class Executor {
|
|||
}
|
||||
|
||||
public Executor authPreemptive(final HttpHost host) {
|
||||
this.authCache.put(host, new BasicScheme(ChallengeState.TARGET));
|
||||
BasicScheme basicScheme = new BasicScheme();
|
||||
try {
|
||||
basicScheme.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC "));
|
||||
} catch (MalformedChallengeException ingnore) {
|
||||
}
|
||||
this.authCache.put(host, basicScheme);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Executor authPreemptiveProxy(final HttpHost host) {
|
||||
this.authCache.put(host, new BasicScheme(ChallengeState.PROXY));
|
||||
BasicScheme basicScheme = new BasicScheme();
|
||||
try {
|
||||
basicScheme.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC "));
|
||||
} catch (MalformedChallengeException ingnore) {
|
||||
}
|
||||
this.authCache.put(host, basicScheme);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -53,6 +53,7 @@ import org.apache.http.client.methods.HttpPost;
|
|||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.client.methods.HttpTrace;
|
||||
import org.apache.http.client.params.HttpClientParamConfig;
|
||||
import org.apache.http.conn.params.ConnRoutePNames;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.entity.ContentType;
|
||||
|
@ -64,6 +65,7 @@ import org.apache.http.params.CoreProtocolPNames;
|
|||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class Request {
|
||||
|
||||
public static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
|
||||
|
@ -142,6 +144,7 @@ public class Request {
|
|||
}
|
||||
|
||||
public Response execute() throws ClientProtocolException, IOException {
|
||||
this.request.setConfig(HttpClientParamConfig.getRequestConfig(this.localParams));
|
||||
return new Response(Executor.CLIENT.execute(this.request));
|
||||
}
|
||||
|
||||
|
@ -204,13 +207,19 @@ public class Request {
|
|||
return this;
|
||||
}
|
||||
|
||||
//// HTTP config parameter operations
|
||||
|
||||
/**
|
||||
* @deprecated (4.3)
|
||||
*/
|
||||
@Deprecated
|
||||
public Request config(final String param, final Object object) {
|
||||
this.localParams.setParameter(param, object);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated (4.3)
|
||||
*/
|
||||
@Deprecated
|
||||
public Request removeConfig(final String param) {
|
||||
this.localParams.removeParameter(param);
|
||||
return this;
|
||||
|
@ -219,7 +228,8 @@ public class Request {
|
|||
//// HTTP protocol parameter operations
|
||||
|
||||
public Request version(final HttpVersion version) {
|
||||
return config(CoreProtocolPNames.PROTOCOL_VERSION, version);
|
||||
this.request.setProtocolVersion(version);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Request elementCharset(final String charset) {
|
||||
|
@ -231,7 +241,8 @@ public class Request {
|
|||
}
|
||||
|
||||
public Request userAgent(final String agent) {
|
||||
return config(CoreProtocolPNames.USER_AGENT, agent);
|
||||
this.request.setHeader(HTTP.USER_AGENT, agent);
|
||||
return this;
|
||||
}
|
||||
|
||||
//// HTTP connection parameter operations
|
||||
|
|
|
@ -32,7 +32,6 @@ import java.net.InetSocketAddress;
|
|||
import java.net.Proxy;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
|
@ -40,16 +39,15 @@ import org.apache.http.HttpHost;
|
|||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
import org.apache.http.conn.ConnectTimeoutException;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||
import org.apache.http.conn.scheme.SchemeSocketFactory;
|
||||
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.impl.conn.SchemeRegistryFactory;
|
||||
import org.apache.http.params.HttpConnectionParams;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
/**
|
||||
|
@ -60,18 +58,20 @@ import org.apache.http.util.EntityUtils;
|
|||
public class ClientExecuteSOCKS {
|
||||
|
||||
public static void main(String[] args)throws Exception {
|
||||
SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();
|
||||
schemeRegistry.register(new Scheme("http", 80, new MySchemeSocketFactory()));
|
||||
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
|
||||
Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", new MyConnectionSocketFactory())
|
||||
.build();
|
||||
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg);
|
||||
CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();
|
||||
try {
|
||||
httpclient.getParams().setParameter("socks.host", "mysockshost");
|
||||
httpclient.getParams().setParameter("socks.port", 1234);
|
||||
InetSocketAddress socksaddr = new InetSocketAddress("mysockshost", 1234);
|
||||
HttpClientContext context = HttpClientContext.create();
|
||||
context.setAttribute("socks.address", socksaddr);
|
||||
|
||||
HttpHost target = new HttpHost("www.apache.org", 80, "http");
|
||||
HttpGet request = new HttpGet("/");
|
||||
|
||||
System.out.println("executing request to " + target + " via SOCKS proxy");
|
||||
System.out.println("executing request to " + target + " via SOCKS proxy " + socksaddr);
|
||||
CloseableHttpResponse response = httpclient.execute(target, request);
|
||||
try {
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
@ -95,45 +95,32 @@ public class ClientExecuteSOCKS {
|
|||
}
|
||||
}
|
||||
|
||||
static class MySchemeSocketFactory implements SchemeSocketFactory {
|
||||
static class MyConnectionSocketFactory implements ConnectionSocketFactory {
|
||||
|
||||
public Socket createSocket(final HttpParams params) throws IOException {
|
||||
if (params == null) {
|
||||
throw new IllegalArgumentException("HTTP parameters may not be null");
|
||||
}
|
||||
String proxyHost = (String) params.getParameter("socks.host");
|
||||
Integer proxyPort = (Integer) params.getParameter("socks.port");
|
||||
|
||||
InetSocketAddress socksaddr = new InetSocketAddress(proxyHost, proxyPort.intValue());
|
||||
public Socket createSocket(final HttpContext context) throws IOException {
|
||||
InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socks.address");
|
||||
Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
|
||||
return new Socket(proxy);
|
||||
}
|
||||
|
||||
public Socket connectSocket(
|
||||
final int connectTimeout,
|
||||
final Socket socket,
|
||||
final HttpHost host,
|
||||
final InetSocketAddress remoteAddress,
|
||||
final InetSocketAddress localAddress,
|
||||
final HttpParams params)
|
||||
throws IOException, UnknownHostException, ConnectTimeoutException {
|
||||
if (remoteAddress == null) {
|
||||
throw new IllegalArgumentException("Remote address may not be null");
|
||||
}
|
||||
if (params == null) {
|
||||
throw new IllegalArgumentException("HTTP parameters may not be null");
|
||||
}
|
||||
final HttpContext context) throws IOException, ConnectTimeoutException {
|
||||
Socket sock;
|
||||
if (socket != null) {
|
||||
sock = socket;
|
||||
} else {
|
||||
sock = createSocket(params);
|
||||
sock = createSocket(context);
|
||||
}
|
||||
if (localAddress != null) {
|
||||
sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
|
||||
sock.bind(localAddress);
|
||||
}
|
||||
int timeout = HttpConnectionParams.getConnectionTimeout(params);
|
||||
try {
|
||||
sock.connect(remoteAddress, timeout);
|
||||
sock.connect(remoteAddress, connectTimeout);
|
||||
} catch (SocketTimeoutException ex) {
|
||||
throw new ConnectTimeoutException("Connect to " + remoteAddress.getHostName() + "/"
|
||||
+ remoteAddress.getAddress() + " timed out");
|
||||
|
@ -141,10 +128,6 @@ public class ClientExecuteSOCKS {
|
|||
return sock;
|
||||
}
|
||||
|
||||
public boolean isSecure(final Socket sock) throws IllegalArgumentException {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -42,9 +42,9 @@ import org.apache.http.params.HttpProtocolParams;
|
|||
* @since 4.3
|
||||
*/
|
||||
@Deprecated
|
||||
public final class HttpParamConfig {
|
||||
public final class HttpClientParamConfig {
|
||||
|
||||
private HttpParamConfig() {
|
||||
private HttpClientParamConfig() {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
|
@ -30,8 +30,6 @@ package org.apache.http.impl.client;
|
|||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
|
||||
import javax.net.ssl.SSLSession;
|
||||
|
||||
import org.apache.http.ConnectionReuseStrategy;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpException;
|
||||
|
@ -39,29 +37,35 @@ import org.apache.http.HttpHost;
|
|||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpRequestInterceptor;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.ProtocolVersion;
|
||||
import org.apache.http.HttpVersion;
|
||||
import org.apache.http.auth.AUTH;
|
||||
import org.apache.http.auth.AuthSchemeRegistry;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.AuthState;
|
||||
import org.apache.http.auth.Credentials;
|
||||
import org.apache.http.client.params.AuthPolicy;
|
||||
import org.apache.http.client.params.HttpClientParams;
|
||||
import org.apache.http.client.config.AuthSchemes;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.params.HttpClientParamConfig;
|
||||
import org.apache.http.client.protocol.ClientContext;
|
||||
import org.apache.http.client.protocol.RequestClientConnControl;
|
||||
import org.apache.http.config.ConnectionConfig;
|
||||
import org.apache.http.conn.HttpConnectionFactory;
|
||||
import org.apache.http.conn.SocketClientConnection;
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.conn.routing.RouteInfo.LayerType;
|
||||
import org.apache.http.conn.routing.RouteInfo.TunnelType;
|
||||
import org.apache.http.entity.BufferedHttpEntity;
|
||||
import org.apache.http.impl.DefaultConnectionReuseStrategy;
|
||||
import org.apache.http.impl.DefaultHttpClientConnection;
|
||||
import org.apache.http.impl.auth.BasicSchemeFactory;
|
||||
import org.apache.http.impl.auth.DigestSchemeFactory;
|
||||
import org.apache.http.impl.auth.KerberosSchemeFactory;
|
||||
import org.apache.http.impl.auth.NTLMSchemeFactory;
|
||||
import org.apache.http.impl.auth.SPNegoSchemeFactory;
|
||||
import org.apache.http.impl.conn.DefaultClientConnectionFactory;
|
||||
import org.apache.http.message.BasicHttpRequest;
|
||||
import org.apache.http.params.BasicHttpParams;
|
||||
import org.apache.http.params.HttpParamConfig;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.params.HttpProtocolParams;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
import org.apache.http.protocol.ExecutionContext;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
|
@ -69,10 +73,15 @@ import org.apache.http.protocol.HttpProcessor;
|
|||
import org.apache.http.protocol.HttpRequestExecutor;
|
||||
import org.apache.http.protocol.ImmutableHttpProcessor;
|
||||
import org.apache.http.protocol.RequestUserAgent;
|
||||
import org.apache.http.util.Args;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ProxyClient {
|
||||
|
||||
private final HttpConnectionFactory<SocketClientConnection> connFactory;
|
||||
private final ConnectionConfig connectionConfig;
|
||||
private final RequestConfig requestConfig;
|
||||
private final HttpProcessor httpProcessor;
|
||||
private final HttpRequestExecutor requestExec;
|
||||
private final ProxyAuthenticationStrategy proxyAuthStrategy;
|
||||
|
@ -80,13 +89,18 @@ public class ProxyClient {
|
|||
private final AuthState proxyAuthState;
|
||||
private final AuthSchemeRegistry authSchemeRegistry;
|
||||
private final ConnectionReuseStrategy reuseStrategy;
|
||||
private final HttpParams params;
|
||||
|
||||
public ProxyClient(final HttpParams params) {
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
public ProxyClient(
|
||||
final HttpConnectionFactory<SocketClientConnection> connFactory,
|
||||
final ConnectionConfig connectionConfig,
|
||||
final RequestConfig requestConfig) {
|
||||
super();
|
||||
if (params == null) {
|
||||
throw new IllegalArgumentException("HTTP parameters may not be null");
|
||||
}
|
||||
this.connFactory = connFactory != null ? connFactory : DefaultClientConnectionFactory.INSTANCE;
|
||||
this.connectionConfig = connectionConfig != null ? connectionConfig : ConnectionConfig.DEFAULT;
|
||||
this.requestConfig = requestConfig != null ? requestConfig : RequestConfig.DEFAULT;
|
||||
this.httpProcessor = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
|
||||
new RequestClientConnControl(),
|
||||
new RequestUserAgent()
|
||||
|
@ -96,23 +110,41 @@ public class ProxyClient {
|
|||
this.authenticator = new HttpAuthenticator();
|
||||
this.proxyAuthState = new AuthState();
|
||||
this.authSchemeRegistry = new AuthSchemeRegistry();
|
||||
this.authSchemeRegistry.register(AuthPolicy.BASIC, new BasicSchemeFactory());
|
||||
this.authSchemeRegistry.register(AuthPolicy.DIGEST, new DigestSchemeFactory());
|
||||
this.authSchemeRegistry.register(AuthPolicy.NTLM, new NTLMSchemeFactory());
|
||||
this.authSchemeRegistry.register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory());
|
||||
this.authSchemeRegistry.register(AuthPolicy.KERBEROS, new KerberosSchemeFactory());
|
||||
this.authSchemeRegistry.register(AuthSchemes.BASIC, new BasicSchemeFactory());
|
||||
this.authSchemeRegistry.register(AuthSchemes.DIGEST, new DigestSchemeFactory());
|
||||
this.authSchemeRegistry.register(AuthSchemes.NTLM, new NTLMSchemeFactory());
|
||||
this.authSchemeRegistry.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory());
|
||||
this.authSchemeRegistry.register(AuthSchemes.KERBEROS, new KerberosSchemeFactory());
|
||||
this.reuseStrategy = new DefaultConnectionReuseStrategy();
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated (4.3) use {@link ProxyClient#ProxyClient(RequestConfig)}
|
||||
* @param params
|
||||
*/
|
||||
@Deprecated
|
||||
public ProxyClient(final HttpParams params) {
|
||||
this(null,
|
||||
HttpParamConfig.getConnectionConfig(params),
|
||||
HttpClientParamConfig.getRequestConfig(params));
|
||||
}
|
||||
|
||||
public ProxyClient() {
|
||||
this(new BasicHttpParams());
|
||||
this(null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated (4.3) do not use.
|
||||
*/
|
||||
@Deprecated
|
||||
public HttpParams getParams() {
|
||||
return this.params;
|
||||
return new BasicHttpParams();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated (4.3) do not use.
|
||||
*/
|
||||
@Deprecated
|
||||
public AuthSchemeRegistry getAuthSchemeRegistry() {
|
||||
return this.authSchemeRegistry;
|
||||
}
|
||||
|
@ -121,78 +153,74 @@ public class ProxyClient {
|
|||
final HttpHost proxy,
|
||||
final HttpHost target,
|
||||
final Credentials credentials) throws IOException, HttpException {
|
||||
ProxyConnection conn = new ProxyConnection(new HttpRoute(proxy));
|
||||
Args.notNull(proxy, "Proxy host");
|
||||
Args.notNull(target, "Target host");
|
||||
Args.notNull(credentials, "Credentials");
|
||||
HttpHost host = target;
|
||||
if (host.getPort() <= 0) {
|
||||
host = new HttpHost(host.getHostName(), 80, host.getSchemeName());
|
||||
}
|
||||
HttpRoute route = new HttpRoute(
|
||||
host,
|
||||
this.requestConfig.getLocalAddress(),
|
||||
proxy, false, TunnelType.TUNNELLED, LayerType.PLAIN);
|
||||
|
||||
SocketClientConnection conn = this.connFactory.create(this.connectionConfig);
|
||||
HttpContext context = new BasicHttpContext();
|
||||
HttpResponse response = null;
|
||||
|
||||
String host = target.getHostName();
|
||||
int port = target.getPort();
|
||||
if (port < 0) {
|
||||
port = 80;
|
||||
}
|
||||
|
||||
StringBuilder buffer = new StringBuilder(host.length() + 6);
|
||||
buffer.append(host);
|
||||
buffer.append(':');
|
||||
buffer.append(Integer.toString(port));
|
||||
|
||||
String authority = buffer.toString();
|
||||
ProtocolVersion ver = HttpProtocolParams.getVersion(this.params);
|
||||
HttpRequest connect = new BasicHttpRequest("CONNECT", authority, ver);
|
||||
connect.setParams(this.params);
|
||||
HttpRequest connect = new BasicHttpRequest(
|
||||
"CONNECT", host.toHostString(), HttpVersion.HTTP_1_1);
|
||||
|
||||
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||
credsProvider.setCredentials(new AuthScope(proxy), credentials);
|
||||
|
||||
// Populate the execution context
|
||||
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target);
|
||||
context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, proxy);
|
||||
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
|
||||
context.setAttribute(ExecutionContext.HTTP_REQUEST, connect);
|
||||
context.setAttribute(ClientContext.ROUTE, route);
|
||||
context.setAttribute(ClientContext.PROXY_AUTH_STATE, this.proxyAuthState);
|
||||
context.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);
|
||||
context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry);
|
||||
context.setAttribute(ClientContext.REQUEST_CONFIG, this.requestConfig);
|
||||
|
||||
this.requestExec.preProcess(connect, this.httpProcessor, context);
|
||||
|
||||
for (;;) {
|
||||
if (!conn.isOpen()) {
|
||||
Socket socket = new Socket(proxy.getHostName(), proxy.getPort());
|
||||
conn.bind(socket, this.params);
|
||||
conn.bind(socket);
|
||||
}
|
||||
|
||||
this.authenticator.generateAuthResponse(connect, this.proxyAuthState, context);
|
||||
|
||||
response = this.requestExec.execute(connect, conn, context);
|
||||
response.setParams(this.params);
|
||||
|
||||
int status = response.getStatusLine().getStatusCode();
|
||||
if (status < 200) {
|
||||
throw new HttpException("Unexpected response to CONNECT request: " +
|
||||
response.getStatusLine());
|
||||
}
|
||||
|
||||
if (HttpClientParams.isAuthenticating(this.params)) {
|
||||
if (this.authenticator.isAuthenticationRequested(proxy, response,
|
||||
if (this.authenticator.isAuthenticationRequested(proxy, response,
|
||||
this.proxyAuthStrategy, this.proxyAuthState, context)) {
|
||||
if (this.authenticator.handleAuthChallenge(proxy, response,
|
||||
this.proxyAuthStrategy, this.proxyAuthState, context)) {
|
||||
if (this.authenticator.handleAuthChallenge(proxy, response,
|
||||
this.proxyAuthStrategy, this.proxyAuthState, context)) {
|
||||
// Retry request
|
||||
if (this.reuseStrategy.keepAlive(response, context)) {
|
||||
// Consume response content
|
||||
HttpEntity entity = response.getEntity();
|
||||
EntityUtils.consume(entity);
|
||||
} else {
|
||||
conn.close();
|
||||
}
|
||||
// discard previous auth header
|
||||
connect.removeHeaders(AUTH.PROXY_AUTH_RESP);
|
||||
// Retry request
|
||||
if (this.reuseStrategy.keepAlive(response, context)) {
|
||||
// Consume response content
|
||||
HttpEntity entity = response.getEntity();
|
||||
EntityUtils.consume(entity);
|
||||
} else {
|
||||
break;
|
||||
conn.close();
|
||||
}
|
||||
// discard previous auth header
|
||||
connect.removeHeaders(AUTH.PROXY_AUTH_RESP);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -213,32 +241,4 @@ public class ProxyClient {
|
|||
return conn.getSocket();
|
||||
}
|
||||
|
||||
static class ProxyConnection extends DefaultHttpClientConnection {
|
||||
|
||||
private final HttpRoute route;
|
||||
|
||||
ProxyConnection(final HttpRoute route) {
|
||||
super();
|
||||
this.route = route;
|
||||
}
|
||||
|
||||
public HttpRoute getRoute() {
|
||||
return this.route;
|
||||
}
|
||||
|
||||
public boolean isSecure() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public SSLSession getSSLSession() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Socket getSocket() {
|
||||
return super.getSocket();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ import java.io.IOException;
|
|||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
|
||||
import org.apache.http.HttpException;
|
||||
import org.apache.http.annotation.ThreadSafe;
|
||||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.client.BackoffManager;
|
||||
import org.apache.http.client.ConnectionBackoffStrategy;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
|
@ -42,7 +42,7 @@ import org.apache.http.conn.routing.HttpRoute;
|
|||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
@ThreadSafe
|
||||
@Immutable
|
||||
class BackoffStrategyExec implements ClientExecChain {
|
||||
|
||||
private final ClientExecChain requestExecutor;
|
||||
|
|
|
@ -34,7 +34,6 @@ import org.apache.http.annotation.NotThreadSafe;
|
|||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpEntityEnclosingRequest;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.ProtocolException;
|
||||
import org.apache.http.ProtocolVersion;
|
||||
|
@ -56,7 +55,6 @@ class HttpRequestWrapper extends AbstractHttpMessage implements HttpRequest {
|
|||
private final HttpRequest original;
|
||||
|
||||
private URI uri;
|
||||
private HttpHost virtualHost;
|
||||
|
||||
private HttpRequestWrapper(final HttpRequest request) {
|
||||
super();
|
||||
|
@ -100,14 +98,6 @@ class HttpRequestWrapper extends AbstractHttpMessage implements HttpRequest {
|
|||
return this.original;
|
||||
}
|
||||
|
||||
public HttpHost getVirtualHost() {
|
||||
return this.virtualHost;
|
||||
}
|
||||
|
||||
public void setVirtualHost(final HttpHost virtualHost) {
|
||||
this.virtualHost = virtualHost;
|
||||
}
|
||||
|
||||
public boolean isRepeatable() {
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ import org.apache.http.client.methods.CloseableHttpResponse;
|
|||
import org.apache.http.client.methods.Configurable;
|
||||
import org.apache.http.client.methods.HttpExecutionAware;
|
||||
import org.apache.http.client.params.ClientPNames;
|
||||
import org.apache.http.client.params.HttpParamConfig;
|
||||
import org.apache.http.client.params.HttpClientParamConfig;
|
||||
import org.apache.http.client.protocol.ClientContext;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.config.Lookup;
|
||||
|
@ -105,7 +105,7 @@ class InternalHttpClient extends CloseableHttpClient {
|
|||
this.authSchemeRegistry = authSchemeRegistry;
|
||||
this.cookieStore = cookieStore;
|
||||
this.credentialsProvider = credentialsProvider;
|
||||
this.defaultConfig = defaultConfig != null ? defaultConfig : RequestConfig.DEFAULT;
|
||||
this.defaultConfig = defaultConfig;
|
||||
this.params = new BasicHttpParams();
|
||||
}
|
||||
|
||||
|
@ -160,22 +160,19 @@ class InternalHttpClient extends CloseableHttpClient {
|
|||
execListner = (HttpExecutionAware) request;
|
||||
}
|
||||
try {
|
||||
HttpParams params = request.getParams();
|
||||
HttpHost virtualHost = (HttpHost) params.getParameter(ClientPNames.VIRTUAL_HOST);
|
||||
|
||||
HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request);
|
||||
wrapper.setVirtualHost(virtualHost);
|
||||
HttpClientContext localcontext = setupContext(context);
|
||||
HttpRoute route = determineRoute(target, wrapper, localcontext);
|
||||
RequestConfig config = null;
|
||||
if (request instanceof Configurable) {
|
||||
config = ((Configurable) request).getConfig();
|
||||
} else {
|
||||
config = HttpParamConfig.getRequestConfig(params);
|
||||
}
|
||||
if (config == null) {
|
||||
config = this.defaultConfig;
|
||||
}
|
||||
if (config == null) {
|
||||
config = HttpClientParamConfig.getRequestConfig(params);
|
||||
}
|
||||
localcontext.setRequestConfig(config);
|
||||
return this.execChain.execute(route, wrapper, localcontext, execListner);
|
||||
} catch (HttpException httpException) {
|
||||
|
|
|
@ -41,7 +41,7 @@ import org.apache.http.HttpHost;
|
|||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpRequestInterceptor;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.annotation.ThreadSafe;
|
||||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.auth.AUTH;
|
||||
import org.apache.http.auth.AuthProtocolState;
|
||||
import org.apache.http.auth.AuthState;
|
||||
|
@ -77,7 +77,7 @@ import org.apache.http.util.EntityUtils;
|
|||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
@ThreadSafe
|
||||
@Immutable
|
||||
class MainClientExec implements ClientExecChain {
|
||||
|
||||
private final Log log = LogFactory.getLog(getClass());
|
||||
|
|
|
@ -36,23 +36,26 @@ import org.apache.commons.logging.LogFactory;
|
|||
import org.apache.http.HttpException;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.ProtocolException;
|
||||
import org.apache.http.annotation.ThreadSafe;
|
||||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.auth.AuthState;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpExecutionAware;
|
||||
import org.apache.http.client.params.ClientPNames;
|
||||
import org.apache.http.client.protocol.ClientContext;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.client.utils.URIUtils;
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.impl.auth.BasicScheme;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.protocol.ExecutionContext;
|
||||
import org.apache.http.protocol.HttpProcessor;
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
@ThreadSafe
|
||||
@Immutable
|
||||
@SuppressWarnings("deprecation")
|
||||
class ProtocolExec implements ClientExecChain {
|
||||
|
||||
private final Log log = LogFactory.getLog(getClass());
|
||||
|
@ -133,7 +136,8 @@ class ProtocolExec implements ClientExecChain {
|
|||
// Re-write request URI if needed
|
||||
rewriteRequestURI(request, route);
|
||||
|
||||
HttpHost virtualHost = request.getVirtualHost();
|
||||
HttpParams params = request.getParams();
|
||||
HttpHost virtualHost = (HttpHost) params.getParameter(ClientPNames.VIRTUAL_HOST);
|
||||
// HTTPCLIENT-1092 - add the port if necessary
|
||||
if (virtualHost != null && virtualHost.getPort() == -1) {
|
||||
int port = target.getPort();
|
||||
|
|
|
@ -139,7 +139,6 @@ class RedirectExec implements ClientExecChain {
|
|||
proxyAuthState.reset();
|
||||
}
|
||||
}
|
||||
request.setVirtualHost(null);
|
||||
}
|
||||
|
||||
currentRoute = this.routePlanner.determineRoute(newTarget, currentRequest, context);
|
||||
|
|
|
@ -34,7 +34,7 @@ import org.apache.commons.logging.LogFactory;
|
|||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpException;
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.annotation.NotThreadSafe;
|
||||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.client.HttpRequestRetryHandler;
|
||||
import org.apache.http.client.NonRepeatableRequestException;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
|
@ -46,7 +46,7 @@ import org.apache.http.conn.routing.HttpRoute;
|
|||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
@NotThreadSafe // e.g. managedConn
|
||||
@Immutable
|
||||
class RetryExec implements ClientExecChain {
|
||||
|
||||
private final Log log = LogFactory.getLog(getClass());
|
||||
|
|
|
@ -33,7 +33,7 @@ import java.io.InterruptedIOException;
|
|||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.HttpException;
|
||||
import org.apache.http.annotation.ThreadSafe;
|
||||
import org.apache.http.annotation.Immutable;
|
||||
import org.apache.http.client.ServiceUnavailableRetryStrategy;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpExecutionAware;
|
||||
|
@ -46,7 +46,7 @@ import org.apache.http.conn.routing.HttpRoute;
|
|||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
@ThreadSafe
|
||||
@Immutable
|
||||
class ServiceUnavailableRetryExec implements ClientExecChain {
|
||||
|
||||
private final Log log = LogFactory.getLog(getClass());
|
||||
|
|
|
@ -37,7 +37,6 @@ import org.apache.http.HttpHost;
|
|||
import org.apache.http.HttpRequest;
|
||||
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.HttpRoutePlanner;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
|
@ -83,15 +82,6 @@ public class DefaultHttpRoutePlanner implements HttpRoutePlanner {
|
|||
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,
|
||||
HttpRequest request,
|
||||
HttpContext context)
|
||||
|
@ -123,8 +113,7 @@ public class DefaultHttpRoutePlanner implements HttpRoutePlanner {
|
|||
|
||||
final Scheme schm;
|
||||
try {
|
||||
SchemeRegistry registry = getSchemeRegistry(context);
|
||||
schm = registry.getScheme(target.getSchemeName());
|
||||
schm = this.schemeRegistry.getScheme(target.getSchemeName());
|
||||
} catch (IllegalStateException ex) {
|
||||
throw new HttpException(ex.getMessage());
|
||||
}
|
||||
|
|
|
@ -36,6 +36,7 @@ import org.apache.http.annotation.Immutable;
|
|||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.conn.SchemePortResolver;
|
||||
import org.apache.http.conn.params.ConnRouteParams;
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.conn.routing.HttpRoutePlanner;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
|
@ -49,6 +50,7 @@ import org.apache.http.protocol.HttpContext;
|
|||
* @since 4.3
|
||||
*/
|
||||
@Immutable
|
||||
@SuppressWarnings("deprecation")
|
||||
public class DefaultRoutePlanner implements HttpRoutePlanner {
|
||||
|
||||
private final SchemePortResolver schemePortResolver;
|
||||
|
@ -69,6 +71,13 @@ public class DefaultRoutePlanner implements HttpRoutePlanner {
|
|||
if (request == null) {
|
||||
throw new IllegalArgumentException("Request may not be null");
|
||||
}
|
||||
|
||||
// If we have a forced route, we can do without a target.
|
||||
HttpRoute route = ConnRouteParams.getForcedRoute(request.getParams());
|
||||
if (route != null) {
|
||||
return route;
|
||||
}
|
||||
|
||||
HttpClientContext clientContext = HttpClientContext.adapt(context);
|
||||
RequestConfig config = clientContext.getRequestConfig();
|
||||
InetAddress local = config.getLocalAddress();
|
||||
|
|
|
@ -50,13 +50,6 @@ import org.apache.http.protocol.HttpContext;
|
|||
* {@link org.apache.http.conn.params.ConnRoutePNames parameters},
|
||||
* though not the {@link
|
||||
* org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY}.
|
||||
* <p>
|
||||
* The following parameters can be used to customize the behavior of this
|
||||
* class:
|
||||
* <ul>
|
||||
* <li>{@link org.apache.http.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li>
|
||||
* <li>{@link org.apache.http.conn.params.ConnRoutePNames#FORCED_ROUTE}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue