Code cleanups

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1483265 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-05-16 09:12:42 +00:00
parent 3c4e5f3dfd
commit ff1acb48a8
8 changed files with 23 additions and 17 deletions

View File

@ -45,7 +45,7 @@ import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider; import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.ClientContext; import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry; import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder; import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.ConnectionSocketFactory;
@ -207,9 +207,9 @@ public class Executor {
*/ */
public Response execute( public Response execute(
final Request request) throws ClientProtocolException, IOException { final Request request) throws ClientProtocolException, IOException {
this.localContext.setAttribute(ClientContext.CREDS_PROVIDER, this.credentialsProvider); this.localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
this.localContext.setAttribute(ClientContext.AUTH_CACHE, this.authCache); this.localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);
this.localContext.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore); this.localContext.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
final HttpRequestBase httprequest = request.getHttpRequest(); final HttpRequestBase httprequest = request.getHttpRequest();
httprequest.reset(); httprequest.reset();
return new Response(this.httpclient.execute(httprequest, this.localContext)); return new Response(this.httpclient.execute(httprequest, this.localContext));

View File

@ -119,6 +119,7 @@ public class CachingHttpClientBuilder extends HttpClientBuilder {
return null; return null;
} }
@SuppressWarnings("resource")
private SchedulingStrategy createSchedulingStrategy(final CacheConfig config) { private SchedulingStrategy createSchedulingStrategy(final CacheConfig config) {
return schedulingStrategy != null ? schedulingStrategy : new ImmediateSchedulingStrategy(config); return schedulingStrategy != null ? schedulingStrategy : new ImmediateSchedulingStrategy(config);
} }

View File

@ -551,13 +551,14 @@ public class SSLSocketFactory implements LayeredConnectionSocketFactory, SchemeL
final HttpContext context) throws IOException, ConnectTimeoutException { final HttpContext context) throws IOException, ConnectTimeoutException {
Args.notNull(host, "HTTP host"); Args.notNull(host, "HTTP host");
Args.notNull(remoteAddress, "Remote address"); Args.notNull(remoteAddress, "Remote address");
Socket sock = socket != null ? socket : createSocket(context); final Socket sock = socket != null ? socket : createSocket(context);
if (localAddress != null) { if (localAddress != null) {
sock.bind(localAddress); sock.bind(localAddress);
} }
try { try {
sock.connect(remoteAddress, connectTimeout); sock.connect(remoteAddress, connectTimeout);
} catch (final SocketTimeoutException ex) { } catch (final SocketTimeoutException ex) {
closeSocket(sock);
throw new ConnectTimeoutException(host, remoteAddress); throw new ConnectTimeoutException(host, remoteAddress);
} }
// Setup SSL layering if necessary // Setup SSL layering if necessary
@ -565,10 +566,17 @@ public class SSLSocketFactory implements LayeredConnectionSocketFactory, SchemeL
final SSLSocket sslsock = (SSLSocket) sock; final SSLSocket sslsock = (SSLSocket) sock;
sslsock.startHandshake(); sslsock.startHandshake();
verifyHostname(sslsock, host.getHostName()); verifyHostname(sslsock, host.getHostName());
return sock;
} else { } else {
sock = createLayeredSocket(sock, host.getHostName(), remoteAddress.getPort(), context); return createLayeredSocket(sock, host.getHostName(), remoteAddress.getPort(), context);
}
}
private void closeSocket(final Socket sock) {
try {
sock.close();
} catch (final IOException ignore) {
} }
return sock;
} }
public Socket createLayeredSocket( public Socket createLayeredSocket(

View File

@ -456,6 +456,7 @@ public class HttpClientBuilder {
} }
} }
} }
@SuppressWarnings("resource")
final PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager( final PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create() RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainSocketFactory.getSocketFactory()) .register("http", PlainSocketFactory.getSocketFactory())

View File

@ -78,12 +78,10 @@ public class BrowserCompatSpec extends CookieSpecBase {
}; };
private final String[] datepatterns; private final String[] datepatterns;
private final BrowserCompatSpecFactory.SecurityLevel securityLevel;
/** Default constructor */ /** Default constructor */
public BrowserCompatSpec(final String[] datepatterns, final BrowserCompatSpecFactory.SecurityLevel securityLevel) { public BrowserCompatSpec(final String[] datepatterns, final BrowserCompatSpecFactory.SecurityLevel securityLevel) {
super(); super();
this.securityLevel = securityLevel;
if (datepatterns != null) { if (datepatterns != null) {
this.datepatterns = datepatterns.clone(); this.datepatterns = datepatterns.clone();
} else { } else {

View File

@ -194,7 +194,7 @@ public class MainClientExec implements ClientExecChain {
try { try {
if (execAware != null) { if (execAware != null) {
if (execAware.isAborted()) { if (execAware.isAborted()) {
connHolder.abortConnection(); connHolder.close();
throw new RequestAbortedException("Request aborted"); throw new RequestAbortedException("Request aborted");
} else { } else {
execAware.setCancellable(connHolder); execAware.setCancellable(connHolder);

View File

@ -141,7 +141,7 @@ public class MinimalClientExec implements ClientExecChain {
try { try {
if (execAware != null) { if (execAware != null) {
if (execAware.isAborted()) { if (execAware.isAborted()) {
releaseTrigger.abortConnection(); releaseTrigger.close();
throw new RequestAbortedException("Request aborted"); throw new RequestAbortedException("Request aborted");
} else { } else {
execAware.setCancellable(releaseTrigger); execAware.setCancellable(releaseTrigger);

View File

@ -45,13 +45,11 @@ import org.apache.http.client.methods.HttpExecutionAware;
import org.apache.http.client.methods.HttpRequestWrapper; import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.ClientPNames; 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.protocol.HttpClientContext;
import org.apache.http.client.utils.URIUtils; import org.apache.http.client.utils.URIUtils;
import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.params.HttpParams; import org.apache.http.params.HttpParams;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpProcessor; import org.apache.http.protocol.HttpProcessor;
import org.apache.http.util.Args; import org.apache.http.util.Args;
@ -168,9 +166,9 @@ public class ProtocolExec implements ClientExecChain {
} }
// Run request protocol interceptors // Run request protocol interceptors
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target); context.setAttribute(HttpClientContext.HTTP_TARGET_HOST, target);
context.setAttribute(ClientContext.ROUTE, route); context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
context.setAttribute(ExecutionContext.HTTP_REQUEST, request); context.setAttribute(HttpClientContext.HTTP_REQUEST, request);
this.httpProcessor.process(request, context); this.httpProcessor.process(request, context);
@ -178,7 +176,7 @@ public class ProtocolExec implements ClientExecChain {
context, execAware); context, execAware);
try { try {
// Run response protocol interceptors // Run response protocol interceptors
context.setAttribute(ExecutionContext.HTTP_RESPONSE, response); context.setAttribute(HttpClientContext.HTTP_RESPONSE, response);
this.httpProcessor.process(response, context); this.httpProcessor.process(response, context);
return response; return response;
} catch (final RuntimeException ex) { } catch (final RuntimeException ex) {