HTTPCLIENT-1588: code optimization

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1644166 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-12-09 20:20:18 +00:00
parent 4e4424a584
commit e767d59e5d
2 changed files with 12 additions and 22 deletions

View File

@ -29,7 +29,6 @@ package org.apache.http.impl.client;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.net.URI;
import org.apache.commons.logging.Log;
@ -37,7 +36,6 @@ import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
@ -219,12 +217,14 @@ public abstract class CloseableHttpClient implements HttpClient, Closeable {
throws IOException, ClientProtocolException {
Args.notNull(responseHandler, "Response handler");
final HttpResponse response = execute(target, request, context);
final T result;
final CloseableHttpResponse response = execute(target, request, context);
try {
result = responseHandler.handleResponse(response);
} catch (final Exception t) {
final T result = responseHandler.handleResponse(response);
final HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
return result;
} catch (final ClientProtocolException t) {
// Try to salvage the underlying connection in case of a protocol exception
final HttpEntity entity = response.getEntity();
try {
EntityUtils.consume(entity);
@ -233,20 +233,10 @@ public abstract class CloseableHttpClient implements HttpClient, Closeable {
// important and will be thrown to the caller.
this.log.warn("Error consuming content after an exception.", t2);
}
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
if (t instanceof IOException) {
throw (IOException) t;
}
throw new UndeclaredThrowableException(t);
throw t;
} finally {
response.close();
}
// Handling the response was successful. Ensure that the content has
// been fully consumed.
final HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
return result;
}
}

View File

@ -166,7 +166,7 @@ public class TestCloseableHttpClient {
Mockito.eq(new HttpHost("somehost", 444, "https")),
Mockito.same(httpget),
(HttpContext) Mockito.isNull());
Mockito.verify(content).close();
Mockito.verify(response).close();
throw ex;
}
}
@ -190,7 +190,7 @@ public class TestCloseableHttpClient {
Mockito.eq(new HttpHost("somehost", 444, "https")),
Mockito.same(httpget),
(HttpContext) Mockito.isNull());
Mockito.verify(content).close();
Mockito.verify(response).close();
throw ex;
}
}