Refactored connection management code in DefaultClientRequestDirector#execute(). Preparing to implement authentication handling for tunneling requests via an authenticating proxy

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@539113 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2007-05-17 21:03:49 +00:00
parent f136fddcca
commit 123cc0b7a5
1 changed files with 35 additions and 73 deletions

View File

@ -398,13 +398,36 @@ public class DefaultClientRequestDirector
} }
} // while not done } // while not done
} finally { // The connection is in or can be brought to a re-usable state.
// if 'done' is false, we're handling an exception boolean reuse = reuseStrategy.keepAlive(response, context);
cleanupConnection(done, response, context);
// check for entity, release connection if possible
if ((response == null) || (response.getEntity() == null) ||
!response.getEntity().isStreaming()) {
// connection not needed and (assumed to be) in re-usable state
if (reuse)
managedConn.markReusable();
connManager.releaseConnection(managedConn);
managedConn = null;
} else {
// install an auto-release entity
HttpEntity entity = response.getEntity();
entity = new BasicManagedEntity(entity, managedConn, reuse);
response.setEntity(entity);
} }
return response; return response;
} catch (HttpException ex) {
abortConnection();
throw ex;
} catch (IOException ex) {
abortConnection();
throw ex;
} catch (RuntimeException ex) {
abortConnection();
throw ex;
}
} // execute } // execute
@ -726,45 +749,16 @@ public class DefaultClientRequestDirector
/** /**
* Releases the connection if possible. * Shuts down the connection.
* This method is called from a <code>finally</code> block in * This method is called from a <code>catch</code> block in
* {@link #execute execute}, possibly during exception handling. * {@link #execute execute} during exception handling.
*
* @param success <code>true</code> if a response is to be returned
* from {@link #execute execute}, or
* <code>false</code> if exception handling is in progress
* @param response the response available for return by
* {@link #execute execute}, or <code>null</code>
* @param context the context used for the last request execution
* *
* @throws IOException in case of an IO problem * @throws IOException in case of an IO problem
*/ */
protected void cleanupConnection(boolean success, private void abortConnection() throws IOException {
HttpResponse response,
HttpContext context)
throws IOException {
ManagedClientConnection mcc = managedConn; ManagedClientConnection mcc = managedConn;
if (mcc == null) if (mcc != null) {
return; // nothing to be cleaned up
if (success) {
// Not in exception handling, there probably is a response.
// The connection is in or can be brought to a re-usable state.
boolean reuse = reuseStrategy.keepAlive(response, context);
// check for entity, release connection if possible
if ((response == null) || (response.getEntity() == null) ||
!response.getEntity().isStreaming()) {
// connection not needed and (assumed to be) in re-usable state
managedConn = null;
if (reuse)
mcc.markReusable();
connManager.releaseConnection(mcc);
} else {
setupResponseEntity(response, context, reuse);
}
} else {
// we got here as the result of an exception // we got here as the result of an exception
// no response will be returned, release the connection // no response will be returned, release the connection
managedConn = null; managedConn = null;
@ -778,7 +772,7 @@ public class DefaultClientRequestDirector
} }
} }
} }
} // cleanupConnection } // abortConnection
private void processChallenges( private void processChallenges(
@ -849,36 +843,4 @@ public class DefaultClientRequestDirector
authState.setCredentials(creds); authState.setCredentials(creds);
} }
/**
* Prepares the entity in the ultimate response being returned.
* The default implementation here installs an entity with auto-release
* capability for the connection.
* <br/>
* This method might be overridden to buffer the response entity
* and release the connection immediately.
* Derived implementations MUST release the connection if an exception
* is thrown here!
*
* @param response the response holding the entity to prepare
* @param context the context used for the last request execution
* @param reuse <code>true</code> if the connection should be
* kept alive and re-used for another request,
* <code>false</code> if the connection should be
* closed and not re-used
*
* @throws IOException in case of an IO problem.
* The connection MUST be released in this method if
* an exception is thrown!
*/
protected void setupResponseEntity(HttpResponse response,
HttpContext context,
boolean reuse)
throws IOException {
// install an auto-release entity
HttpEntity entity = response.getEntity();
response.setEntity(new BasicManagedEntity(entity, managedConn, reuse));
}
} // class DefaultClientRequestDirector } // class DefaultClientRequestDirector