You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/06/03 21:35:58 UTC
svn commit: r1345750 -
/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/MainRequestExecutor.java
Author: olegk
Date: Sun Jun 3 19:35:57 2012
New Revision: 1345750
URL: http://svn.apache.org/viewvc?rev=1345750&view=rev
Log:
DefaultRequestDirector copied to MainRequestExecutor and ported to HttpClientRequestExecutor API with all redirect logic removed
Added:
httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/MainRequestExecutor.java
- copied, changed from r1345659, httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
Copied: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/MainRequestExecutor.java (from r1345659, httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/MainRequestExecutor.java?p2=httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/MainRequestExecutor.java&p1=httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java&r1=1345659&r2=1345750&rev=1345750&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java (original)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/MainRequestExecutor.java Sun Jun 3 19:35:57 2012
@@ -44,19 +44,14 @@ import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolException;
import org.apache.http.ProtocolVersion;
-import org.apache.http.annotation.NotThreadSafe;
+import org.apache.http.annotation.ThreadSafe;
import org.apache.http.auth.AuthProtocolState;
-import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.AuthenticationHandler;
import org.apache.http.client.AuthenticationStrategy;
+import org.apache.http.client.HttpClientRequestExecutor;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.NonRepeatableRequestException;
-import org.apache.http.client.RedirectException;
-import org.apache.http.client.RedirectHandler;
-import org.apache.http.client.RedirectStrategy;
-import org.apache.http.client.RequestDirector;
import org.apache.http.client.UserTokenHandler;
import org.apache.http.client.methods.AbortableHttpRequest;
import org.apache.http.client.methods.HttpUriRequest;
@@ -88,7 +83,6 @@ import org.apache.http.protocol.HttpRequ
import org.apache.http.util.EntityUtils;
/**
- * Default implementation of {@link RequestDirector}.
* <p>
* The following parameters can be used to customize the behavior of this
* class:
@@ -116,247 +110,87 @@ import org.apache.http.util.EntityUtils;
* <li>{@link org.apache.http.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li>
* <li>{@link org.apache.http.client.params.ClientPNames#COOKIE_POLICY}</li>
* <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_AUTHENTICATION}</li>
- * <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_REDIRECTS}</li>
- * <li>{@link org.apache.http.client.params.ClientPNames#MAX_REDIRECTS}</li>
- * <li>{@link org.apache.http.client.params.ClientPNames#ALLOW_CIRCULAR_REDIRECTS}</li>
* <li>{@link org.apache.http.client.params.ClientPNames#VIRTUAL_HOST}</li>
* <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HOST}</li>
* <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HEADERS}</li>
* <li>{@link org.apache.http.client.params.ClientPNames#CONN_MANAGER_TIMEOUT}</li>
* </ul>
*
- * @since 4.0
+ * @since 4.3
*/
-@SuppressWarnings("deprecation")
-@NotThreadSafe // e.g. managedConn
-public class DefaultRequestDirector implements RequestDirector {
+@ThreadSafe // e.g. managedConn
+public class MainRequestExecutor implements HttpClientRequestExecutor {
- private final Log log;
-
- /** The connection manager. */
- protected final ClientConnectionManager connManager;
-
- /** The route planner. */
- protected final HttpRoutePlanner routePlanner;
-
- /** The connection re-use strategy. */
- protected final ConnectionReuseStrategy reuseStrategy;
-
- /** The keep-alive duration strategy. */
- protected final ConnectionKeepAliveStrategy keepAliveStrategy;
-
- /** The request executor. */
- protected final HttpRequestExecutor requestExec;
-
- /** The HTTP protocol processor. */
- protected final HttpProcessor httpProcessor;
-
- /** The request retry handler. */
- protected final HttpRequestRetryHandler retryHandler;
-
- /** The redirect handler. */
- @Deprecated
- protected final RedirectHandler redirectHandler;
-
- /** The redirect strategy. */
- protected final RedirectStrategy redirectStrategy;
-
- /** The target authentication handler. */
- @Deprecated
- protected final AuthenticationHandler targetAuthHandler;
-
- /** The target authentication handler. */
- protected final AuthenticationStrategy targetAuthStrategy;
-
- /** The proxy authentication handler. */
- @Deprecated
- protected final AuthenticationHandler proxyAuthHandler;
-
- /** The proxy authentication handler. */
- protected final AuthenticationStrategy proxyAuthStrategy;
-
- /** The user token handler. */
- protected final UserTokenHandler userTokenHandler;
-
- /** The HTTP parameters. */
- protected final HttpParams params;
-
- /** The currently allocated connection. */
- protected ManagedClientConnection managedConn;
-
- protected final AuthState targetAuthState;
-
- protected final AuthState proxyAuthState;
+ private final Log log = LogFactory.getLog(getClass());
+ private final ClientConnectionManager connManager;
+ private final HttpRoutePlanner routePlanner;
+ private final ConnectionReuseStrategy reuseStrategy;
+ private final ConnectionKeepAliveStrategy keepAliveStrategy;
+ private final HttpRequestExecutor requestExec;
+ private final HttpProcessor httpProcessor;
+ private final HttpRequestRetryHandler retryHandler;
+ private final AuthenticationStrategy targetAuthStrategy;
+ private final AuthenticationStrategy proxyAuthStrategy;
private final HttpAuthenticator authenticator;
+ private final UserTokenHandler userTokenHandler;
+ private final HttpParams params;
- private int execCount;
-
- private int redirectCount;
-
- private int maxRedirects;
-
- private HttpHost virtualHost;
-
- @Deprecated
- public DefaultRequestDirector(
- final HttpRequestExecutor requestExec,
- final ClientConnectionManager conman,
- final ConnectionReuseStrategy reustrat,
- final ConnectionKeepAliveStrategy kastrat,
- final HttpRoutePlanner rouplan,
- final HttpProcessor httpProcessor,
- final HttpRequestRetryHandler retryHandler,
- final RedirectHandler redirectHandler,
- final AuthenticationHandler targetAuthHandler,
- final AuthenticationHandler proxyAuthHandler,
- final UserTokenHandler userTokenHandler,
- final HttpParams params) {
- this(LogFactory.getLog(DefaultRequestDirector.class),
- requestExec, conman, reustrat, kastrat, rouplan, httpProcessor, retryHandler,
- new DefaultRedirectStrategyAdaptor(redirectHandler),
- new AuthenticationStrategyAdaptor(targetAuthHandler),
- new AuthenticationStrategyAdaptor(proxyAuthHandler),
- userTokenHandler,
- params);
- }
-
-
- @Deprecated
- public DefaultRequestDirector(
- final Log log,
- final HttpRequestExecutor requestExec,
- final ClientConnectionManager conman,
- final ConnectionReuseStrategy reustrat,
- final ConnectionKeepAliveStrategy kastrat,
- final HttpRoutePlanner rouplan,
- final HttpProcessor httpProcessor,
- final HttpRequestRetryHandler retryHandler,
- final RedirectStrategy redirectStrategy,
- final AuthenticationHandler targetAuthHandler,
- final AuthenticationHandler proxyAuthHandler,
- final UserTokenHandler userTokenHandler,
- final HttpParams params) {
- this(LogFactory.getLog(DefaultRequestDirector.class),
- requestExec, conman, reustrat, kastrat, rouplan, httpProcessor, retryHandler,
- redirectStrategy,
- new AuthenticationStrategyAdaptor(targetAuthHandler),
- new AuthenticationStrategyAdaptor(proxyAuthHandler),
- userTokenHandler,
- params);
- }
-
- /**
- * @since 4.2
- */
- public DefaultRequestDirector(
- final Log log,
- final HttpRequestExecutor requestExec,
+ public MainRequestExecutor(
final ClientConnectionManager conman,
final ConnectionReuseStrategy reustrat,
final ConnectionKeepAliveStrategy kastrat,
final HttpRoutePlanner rouplan,
final HttpProcessor httpProcessor,
final HttpRequestRetryHandler retryHandler,
- final RedirectStrategy redirectStrategy,
final AuthenticationStrategy targetAuthStrategy,
final AuthenticationStrategy proxyAuthStrategy,
final UserTokenHandler userTokenHandler,
final HttpParams params) {
-
- if (log == null) {
- throw new IllegalArgumentException
- ("Log may not be null.");
- }
- if (requestExec == null) {
- throw new IllegalArgumentException
- ("Request executor may not be null.");
- }
if (conman == null) {
- throw new IllegalArgumentException
- ("Client connection manager may not be null.");
+ throw new IllegalArgumentException("Client connection manager may not be null");
}
if (reustrat == null) {
- throw new IllegalArgumentException
- ("Connection reuse strategy may not be null.");
+ throw new IllegalArgumentException("Connection reuse strategy may not be null");
}
if (kastrat == null) {
- throw new IllegalArgumentException
- ("Connection keep alive strategy may not be null.");
+ throw new IllegalArgumentException("Connection keep alive strategy may not be null");
}
if (rouplan == null) {
- throw new IllegalArgumentException
- ("Route planner may not be null.");
+ throw new IllegalArgumentException("Route planner may not be null");
}
if (httpProcessor == null) {
- throw new IllegalArgumentException
- ("HTTP protocol processor may not be null.");
+ throw new IllegalArgumentException("HTTP protocol processor may not be null");
}
if (retryHandler == null) {
- throw new IllegalArgumentException
- ("HTTP request retry handler may not be null.");
- }
- if (redirectStrategy == null) {
- throw new IllegalArgumentException
- ("Redirect strategy may not be null.");
+ throw new IllegalArgumentException("HTTP request retry handler may not be null");
}
if (targetAuthStrategy == null) {
- throw new IllegalArgumentException
- ("Target authentication strategy may not be null.");
+ throw new IllegalArgumentException("Target authentication strategy may not be null");
}
if (proxyAuthStrategy == null) {
- throw new IllegalArgumentException
- ("Proxy authentication strategy may not be null.");
+ throw new IllegalArgumentException("Proxy authentication strategy may not be null");
}
if (userTokenHandler == null) {
- throw new IllegalArgumentException
- ("User token handler may not be null.");
+ throw new IllegalArgumentException("User token handler may not be null");
}
if (params == null) {
- throw new IllegalArgumentException
- ("HTTP parameters may not be null");
+ throw new IllegalArgumentException("HTTP parameters may not be null");
}
- this.log = log;
- this.authenticator = new HttpAuthenticator(log);
- this.requestExec = requestExec;
+ this.authenticator = new HttpAuthenticator(log);
+ this.requestExec = new HttpRequestExecutor();
this.connManager = conman;
this.reuseStrategy = reustrat;
this.keepAliveStrategy = kastrat;
this.routePlanner = rouplan;
this.httpProcessor = httpProcessor;
this.retryHandler = retryHandler;
- this.redirectStrategy = redirectStrategy;
this.targetAuthStrategy = targetAuthStrategy;
this.proxyAuthStrategy = proxyAuthStrategy;
this.userTokenHandler = userTokenHandler;
this.params = params;
-
- if (redirectStrategy instanceof DefaultRedirectStrategyAdaptor) {
- this.redirectHandler = ((DefaultRedirectStrategyAdaptor) redirectStrategy).getHandler();
- } else {
- this.redirectHandler = null;
- }
- if (targetAuthStrategy instanceof AuthenticationStrategyAdaptor) {
- this.targetAuthHandler = ((AuthenticationStrategyAdaptor) targetAuthStrategy).getHandler();
- } else {
- this.targetAuthHandler = null;
- }
- if (proxyAuthStrategy instanceof AuthenticationStrategyAdaptor) {
- this.proxyAuthHandler = ((AuthenticationStrategyAdaptor) proxyAuthStrategy).getHandler();
- } else {
- this.proxyAuthHandler = null;
- }
-
- this.managedConn = null;
-
- this.execCount = 0;
- this.redirectCount = 0;
- this.targetAuthState = new AuthState();
- this.proxyAuthState = new AuthState();
- this.maxRedirects = this.params.getIntParameter(ClientPNames.MAX_REDIRECTS, 100);
}
-
private RequestWrapper wrapRequest(
final HttpRequest request) throws ProtocolException {
if (request instanceof HttpEntityEnclosingRequest) {
@@ -368,8 +202,7 @@ public class DefaultRequestDirector impl
}
}
-
- protected void rewriteRequestURI(
+ private void rewriteRequestURI(
final RequestWrapper request,
final HttpRoute route) throws ProtocolException {
try {
@@ -400,12 +233,14 @@ public class DefaultRequestDirector impl
}
}
-
- // non-javadoc, see interface ClientRequestDirector
- public HttpResponse execute(HttpHost target, HttpRequest request,
- HttpContext context)
- throws HttpException, IOException {
-
+ public HttpResponse execute(
+ final HttpHost target,
+ final HttpUriRequest request,
+ final HttpContext context) throws HttpException, IOException {
+
+ AuthState targetAuthState = new AuthState();
+ AuthState proxyAuthState = new AuthState();
+
context.setAttribute(ClientContext.TARGET_AUTH_STATE, targetAuthState);
context.setAttribute(ClientContext.PROXY_AUTH_STATE, proxyAuthState);
@@ -414,7 +249,7 @@ public class DefaultRequestDirector impl
origWrapper.setParams(params);
HttpRoute origRoute = determineRoute(target, origWrapper, context);
- virtualHost = (HttpHost) origWrapper.getParams().getParameter(ClientPNames.VIRTUAL_HOST);
+ HttpHost virtualHost = (HttpHost) origWrapper.getParams().getParameter(ClientPNames.VIRTUAL_HOST);
// HTTPCLIENT-1092 - add the port if necessary
if (virtualHost != null && virtualHost.getPort() == -1) {
@@ -423,6 +258,8 @@ public class DefaultRequestDirector impl
virtualHost = new HttpHost(virtualHost.getHostName(), port, virtualHost.getSchemeName());
}
}
+
+ ManagedClientConnection managedConn = null;
RoutedRequest roureq = new RoutedRequest(origWrapper, origRoute);
@@ -477,7 +314,7 @@ public class DefaultRequestDirector impl
}
try {
- tryConnect(roureq, context);
+ tryConnect(proxyAuthState, managedConn, roureq, context);
} catch (TunnelRefusedException ex) {
if (this.log.isDebugEnabled()) {
this.log.debug(ex.getMessage());
@@ -499,23 +336,22 @@ public class DefaultRequestDirector impl
rewriteRequestURI(wrapper, route);
// Use virtual host if set
- target = virtualHost;
-
- if (target == null) {
- target = route.getTargetHost();
+ HttpHost host = virtualHost;
+ if (host == null) {
+ host = route.getTargetHost();
}
HttpHost proxy = route.getProxyHost();
// Populate the execution context
- context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target);
+ context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, proxy);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, managedConn);
// Run request protocol interceptors
requestExec.preProcess(wrapper, httpProcessor, context);
- response = tryExecute(roureq, context);
+ response = tryExecute(managedConn, roureq, context);
if (response == null) {
// Need to start over
continue;
@@ -543,7 +379,8 @@ public class DefaultRequestDirector impl
managedConn.setIdleDuration(duration, TimeUnit.MILLISECONDS);
}
- RoutedRequest followup = handleResponse(roureq, response, context);
+ RoutedRequest followup = handleResponse(targetAuthState, proxyAuthState,
+ roureq, response, context);
if (followup == null) {
done = true;
} else {
@@ -571,7 +408,8 @@ public class DefaultRequestDirector impl
}
// check if we can use the same connection for the followup
if (!followup.getRoute().equals(roureq.getRoute())) {
- releaseConnection();
+ releaseConnection(managedConn);
+ managedConn = null;
}
roureq = followup;
}
@@ -593,9 +431,11 @@ public class DefaultRequestDirector impl
if ((response == null) || (response.getEntity() == null) ||
!response.getEntity().isStreaming()) {
// connection not needed and (assumed to be) in re-usable state
- if (reuse)
+ if (reuse) {
managedConn.markReusable();
- releaseConnection();
+ }
+ releaseConnection(managedConn);
+ managedConn = null;
} else {
// install an auto-release entity
HttpEntity entity = response.getEntity();
@@ -611,23 +451,26 @@ public class DefaultRequestDirector impl
ioex.initCause(ex);
throw ioex;
} catch (HttpException ex) {
- abortConnection();
+ abortConnection(managedConn);
throw ex;
} catch (IOException ex) {
- abortConnection();
+ abortConnection(managedConn);
throw ex;
} catch (RuntimeException ex) {
- abortConnection();
+ abortConnection(managedConn);
throw ex;
}
- } // execute
+ }
/**
* Establish connection either directly or through a tunnel and retry in case of
* a recoverable I/O failure
*/
private void tryConnect(
- final RoutedRequest req, final HttpContext context) throws HttpException, IOException {
+ final AuthState proxyAuthState,
+ final ManagedClientConnection managedConn,
+ final RoutedRequest req,
+ final HttpContext context) throws HttpException, IOException {
HttpRoute route = req.getRoute();
HttpRequest wrapper = req.getRequest();
@@ -642,7 +485,7 @@ public class DefaultRequestDirector impl
} else {
managedConn.setSocketTimeout(HttpConnectionParams.getSoTimeout(params));
}
- establishRoute(route, context);
+ establishRoute(proxyAuthState, managedConn, route, context);
break;
} catch (IOException ex) {
try {
@@ -670,11 +513,14 @@ public class DefaultRequestDirector impl
* Execute request and retry in case of a recoverable I/O failure
*/
private HttpResponse tryExecute(
- final RoutedRequest req, final HttpContext context) throws HttpException, IOException {
+ final ManagedClientConnection managedConn,
+ final RoutedRequest req,
+ final HttpContext context) throws HttpException, IOException {
RequestWrapper wrapper = req.getRequest();
HttpRoute route = req.getRoute();
HttpResponse response = null;
+ int execCount = 0;
Exception retryReason = null;
for (;;) {
// Increment total exec count (with redirects)
@@ -738,12 +584,7 @@ public class DefaultRequestDirector impl
return response;
}
- /**
- * Returns the connection back to the connection manager
- * and prepares for retrieving a new connection during
- * the next request.
- */
- protected void releaseConnection() {
+ private void releaseConnection(final ManagedClientConnection managedConn) {
// Release the connection through the ManagedConnection instead of the
// ConnectionManager directly. This lets the connection control how
// it is released.
@@ -752,55 +593,35 @@ public class DefaultRequestDirector impl
} catch(IOException ignored) {
this.log.debug("IOException releasing connection", ignored);
}
- managedConn = null;
}
/**
* Determines the route for a request.
* Called by {@link #execute}
* to determine the route for either the original or a followup request.
- *
- * @param target the target host for the request.
- * Implementations may accept <code>null</code>
- * if they can still determine a route, for example
- * to a default target or by inspecting the request.
- * @param request the request to execute
- * @param context the context to use for the execution,
- * never <code>null</code>
- *
- * @return the route the request should take
- *
- * @throws HttpException in case of a problem
*/
- protected HttpRoute determineRoute(HttpHost target,
- HttpRequest request,
- HttpContext context)
- throws HttpException {
-
- if (target == null) {
- target = (HttpHost) request.getParams().getParameter(
- ClientPNames.DEFAULT_HOST);
+ private HttpRoute determineRoute(
+ final HttpHost target,
+ final HttpRequest request,
+ final HttpContext context) throws HttpException {
+ HttpHost host = target;
+ if (host == null) {
+ host = (HttpHost) request.getParams().getParameter(ClientPNames.DEFAULT_HOST);
}
- if (target == null) {
- throw new IllegalStateException
- ("Target host must not be null, or set in parameters.");
+ if (host == null) {
+ throw new IllegalStateException("Target host may not be null");
}
-
- return this.routePlanner.determineRoute(target, request, context);
+ return this.routePlanner.determineRoute(host, request, context);
}
-
/**
* Establishes the target route.
- *
- * @param route the route to establish
- * @param context the context for the request execution
- *
- * @throws HttpException in case of a problem
- * @throws IOException in case of an IO problem
*/
- protected void establishRoute(HttpRoute route, HttpContext context)
- throws HttpException, IOException {
+ private void establishRoute(
+ final AuthState proxyAuthState,
+ final ManagedClientConnection managedConn,
+ final HttpRoute route,
+ final HttpContext context) throws HttpException, IOException {
HttpRouteDirector rowdy = new BasicRouteDirector();
int step;
@@ -816,7 +637,7 @@ public class DefaultRequestDirector impl
break;
case HttpRouteDirector.TUNNEL_TARGET: {
- boolean secure = createTunnelToTarget(route, context);
+ boolean secure = createTunnelToTarget(proxyAuthState, managedConn, route, context);
this.log.debug("Tunnel to target created.");
managedConn.tunnelTarget(secure, this.params);
} break;
@@ -850,9 +671,7 @@ public class DefaultRequestDirector impl
}
} while (step > HttpRouteDirector.COMPLETE);
-
- } // establishConnection
-
+ }
/**
* Creates a tunnel to the target server.
@@ -861,29 +680,20 @@ public class DefaultRequestDirector impl
* be created and sent, the response received and checked.
* This method does <i>not</i> update the connection with
* information about the tunnel, that is left to the caller.
- *
- * @param route the route to establish
- * @param context the context for request execution
- *
- * @return <code>true</code> if the tunnelled route is secure,
- * <code>false</code> otherwise.
- * The implementation here always returns <code>false</code>,
- * but derived classes may override.
- *
- * @throws HttpException in case of a problem
- * @throws IOException in case of an IO problem
*/
- protected boolean createTunnelToTarget(HttpRoute route,
- HttpContext context)
- throws HttpException, IOException {
+ private boolean createTunnelToTarget(
+ final AuthState proxyAuthState,
+ final ManagedClientConnection managedConn,
+ final HttpRoute route,
+ final HttpContext context) throws HttpException, IOException {
HttpHost proxy = route.getProxyHost();
HttpHost target = route.getTargetHost();
HttpResponse response = null;
for (;;) {
- if (!this.managedConn.isOpen()) {
- this.managedConn.open(route, context, this.params);
+ if (!managedConn.isOpen()) {
+ managedConn.open(route, context, this.params);
}
HttpRequest connect = createConnectRequest(route, context);
@@ -897,7 +707,7 @@ public class DefaultRequestDirector impl
this.requestExec.preProcess(connect, this.httpProcessor, context);
- response = this.requestExec.execute(connect, this.managedConn, context);
+ response = this.requestExec.execute(connect, managedConn, context);
response.setParams(this.params);
this.requestExec.postProcess(response, this.httpProcessor, context);
@@ -910,9 +720,9 @@ public class DefaultRequestDirector impl
if (HttpClientParams.isAuthenticating(this.params)) {
if (this.authenticator.isAuthenticationRequested(proxy, response,
- this.proxyAuthStrategy, this.proxyAuthState, context)) {
+ this.proxyAuthStrategy, proxyAuthState, context)) {
if (this.authenticator.authenticate(proxy, response,
- this.proxyAuthStrategy, this.proxyAuthState, context)) {
+ this.proxyAuthStrategy, proxyAuthState, context)) {
// Retry request
if (this.reuseStrategy.keepAlive(response, context)) {
this.log.debug("Connection kept alive");
@@ -920,7 +730,7 @@ public class DefaultRequestDirector impl
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
} else {
- this.managedConn.close();
+ managedConn.close();
}
} else {
break;
@@ -941,43 +751,29 @@ public class DefaultRequestDirector impl
response.setEntity(new BufferedHttpEntity(entity));
}
- this.managedConn.close();
+ managedConn.close();
throw new TunnelRefusedException("CONNECT refused by proxy: " +
response.getStatusLine(), response);
}
- this.managedConn.markReusable();
+ managedConn.markReusable();
// How to decide on security of the tunnelled connection?
// The socket factory knows only about the segment to the proxy.
// Even if that is secure, the hop to the target may be insecure.
// Leave it to derived classes, consider insecure by default here.
return false;
-
- } // createTunnelToTarget
-
-
+ }
/**
* Creates a tunnel to an intermediate proxy.
* This method is <i>not</i> implemented in this class.
* It just throws an exception here.
- *
- * @param route the route to establish
- * @param hop the hop in the route to establish now.
- * <code>route.getHopTarget(hop)</code>
- * will return the proxy to tunnel to.
- * @param context the context for request execution
- *
- * @return <code>true</code> if the partially tunnelled connection
- * is secure, <code>false</code> otherwise.
- *
- * @throws HttpException in case of a problem
- * @throws IOException in case of an IO problem
*/
- protected boolean createTunnelToProxy(HttpRoute route, int hop,
- HttpContext context)
- throws HttpException, IOException {
+ private boolean createTunnelToProxy(
+ final HttpRoute route,
+ final int hop,
+ final HttpContext context) throws HttpException, IOException {
// Have a look at createTunnelToTarget and replicate the parts
// you need in a custom derived class. If your proxies don't require
@@ -991,19 +787,13 @@ public class DefaultRequestDirector impl
throw new HttpException("Proxy chains are not supported.");
}
-
-
/**
* Creates the CONNECT request for tunnelling.
* Called by {@link #createTunnelToTarget createTunnelToTarget}.
- *
- * @param route the route to establish
- * @param context the context for request execution
- *
- * @return the CONNECT request for tunnelling
*/
- protected HttpRequest createConnectRequest(HttpRoute route,
- HttpContext context) {
+ private HttpRequest createConnectRequest(
+ final HttpRoute route,
+ final HttpContext context) {
// see RFC 2817, section 5.2 and
// INTERNET-DRAFT: Tunneling TCP based protocols through
// Web proxy servers
@@ -1025,85 +815,24 @@ public class DefaultRequestDirector impl
String authority = buffer.toString();
ProtocolVersion ver = HttpProtocolParams.getVersion(params);
- HttpRequest req = new BasicHttpRequest
- ("CONNECT", authority, ver);
-
+ HttpRequest req = new BasicHttpRequest("CONNECT", authority, ver);
return req;
}
-
/**
* Analyzes a response to check need for a followup.
- *
- * @param roureq the request and route.
- * @param response the response to analayze
- * @param context the context used for the current request execution
- *
- * @return the followup request and route if there is a followup, or
- * <code>null</code> if the response should be returned as is
- *
- * @throws HttpException in case of a problem
- * @throws IOException in case of an IO problem
*/
- protected RoutedRequest handleResponse(RoutedRequest roureq,
- HttpResponse response,
- HttpContext context)
- throws HttpException, IOException {
+ private RoutedRequest handleResponse(
+ final AuthState targetAuthState,
+ final AuthState proxyAuthState,
+ final RoutedRequest roureq,
+ final HttpResponse response,
+ final HttpContext context) throws HttpException, IOException {
HttpRoute route = roureq.getRoute();
RequestWrapper request = roureq.getRequest();
HttpParams params = request.getParams();
- if (HttpClientParams.isRedirecting(params) &&
- this.redirectStrategy.isRedirected(request, response, context)) {
-
- if (redirectCount >= maxRedirects) {
- throw new RedirectException("Maximum redirects ("
- + maxRedirects + ") exceeded");
- }
- redirectCount++;
-
- // Virtual host cannot be used any longer
- virtualHost = null;
-
- HttpUriRequest redirect = redirectStrategy.getRedirect(request, response, context);
- HttpRequest orig = request.getOriginal();
- redirect.setHeaders(orig.getAllHeaders());
-
- URI uri = redirect.getURI();
- if (uri.getHost() == null) {
- throw new ProtocolException("Redirect URI does not specify a valid host name: " + uri);
- }
-
- HttpHost newTarget = new HttpHost(
- uri.getHost(),
- uri.getPort(),
- uri.getScheme());
-
- // Reset auth states if redirecting to another host
- if (!route.getTargetHost().equals(newTarget)) {
- this.log.debug("Resetting target auth state");
- targetAuthState.reset();
- AuthScheme authScheme = proxyAuthState.getAuthScheme();
- if (authScheme != null && authScheme.isConnectionBased()) {
- this.log.debug("Resetting proxy auth state");
- proxyAuthState.reset();
- }
- }
-
- RequestWrapper wrapper = wrapRequest(redirect);
- wrapper.setParams(params);
-
- HttpRoute newRoute = determineRoute(newTarget, wrapper, context);
- RoutedRequest newRequest = new RoutedRequest(wrapper, newRoute);
-
- if (this.log.isDebugEnabled()) {
- this.log.debug("Redirecting to '" + uri + "' via " + newRoute);
- }
-
- return newRequest;
- }
-
if (HttpClientParams.isAuthenticating(params)) {
HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (target == null) {
@@ -1114,9 +843,9 @@ public class DefaultRequestDirector impl
target = new HttpHost(target.getHostName(), scheme.getDefaultPort(), target.getSchemeName());
}
if (this.authenticator.isAuthenticationRequested(target, response,
- this.targetAuthStrategy, this.targetAuthState, context)) {
+ this.targetAuthStrategy, targetAuthState, context)) {
if (this.authenticator.authenticate(target, response,
- this.targetAuthStrategy, this.targetAuthState, context)) {
+ this.targetAuthStrategy, targetAuthState, context)) {
// Re-try the same request via the same route
return roureq;
} else {
@@ -1126,9 +855,9 @@ public class DefaultRequestDirector impl
HttpHost proxy = route.getProxyHost();
if (this.authenticator.isAuthenticationRequested(proxy, response,
- this.proxyAuthStrategy, this.proxyAuthState, context)) {
+ this.proxyAuthStrategy, proxyAuthState, context)) {
if (this.authenticator.authenticate(proxy, response,
- this.proxyAuthStrategy, this.proxyAuthState, context)) {
+ this.proxyAuthStrategy, proxyAuthState, context)) {
// Re-try the same request via the same route
return roureq;
} else {
@@ -1137,35 +866,25 @@ public class DefaultRequestDirector impl
}
}
return null;
- } // handleResponse
-
+ }
/**
* Shuts down the connection.
* This method is called from a <code>catch</code> block in
* {@link #execute execute} during exception handling.
*/
- private void abortConnection() {
- ManagedClientConnection mcc = managedConn;
- if (mcc != null) {
+ private void abortConnection(final ManagedClientConnection managedConn) {
+ if (managedConn != null) {
// we got here as the result of an exception
// no response will be returned, release the connection
- managedConn = null;
try {
- mcc.abortConnection();
+ managedConn.abortConnection();
} catch (IOException ex) {
if (this.log.isDebugEnabled()) {
this.log.debug(ex.getMessage(), ex);
}
}
- // ensure the connection manager properly releases this connection
- try {
- mcc.releaseConnection();
- } catch(IOException ignored) {
- this.log.debug("Error releasing connection", ignored);
- }
}
- } // abortConnection
-
+ }
-} // class DefaultClientRequestDirector
+}