Class identity comparisons wherever possible (and further polishing)

Issue: SPR-12926
This commit is contained in:
Juergen Hoeller
2015-12-09 12:28:09 +01:00
parent 4261f34b63
commit 11806b9215
29 changed files with 127 additions and 109 deletions

View File

@@ -131,7 +131,7 @@ public class HttpEntity<T> {
if (this == other) {
return true;
}
if (other == null || !other.getClass().equals(getClass())) {
if (other == null || other.getClass() != getClass()) {
return false;
}
HttpEntity<?> otherEntity = (HttpEntity<?>) other;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,7 +23,8 @@ import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
/**
* Abstract base class for {@link ClientHttpRequestFactory} implementations that decorate another request factory.
* Abstract base class for {@link ClientHttpRequestFactory} implementations
* that decorate another request factory.
*
* @author Arjen Poutsma
* @since 3.1
@@ -34,11 +35,11 @@ public abstract class AbstractClientHttpRequestFactoryWrapper implements ClientH
/**
* Creates a {@code AbstractClientHttpRequestFactoryWrapper} wrapping the given request factory.
* Create a {@code AbstractClientHttpRequestFactoryWrapper} wrapping the given request factory.
* @param requestFactory the request factory to be wrapped
*/
protected AbstractClientHttpRequestFactoryWrapper(ClientHttpRequestFactory requestFactory) {
Assert.notNull(requestFactory, "'requestFactory' must not be null");
Assert.notNull(requestFactory, "ClientHttpRequestFactory must not be null");
this.requestFactory = requestFactory;
}
@@ -50,12 +51,12 @@ public abstract class AbstractClientHttpRequestFactoryWrapper implements ClientH
*/
@Override
public final ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
return createRequest(uri, httpMethod, requestFactory);
return createRequest(uri, httpMethod, this.requestFactory);
}
/**
* Create a new {@link ClientHttpRequest} for the specified URI and HTTP method by using the
* passed-on request factory.
* Create a new {@link ClientHttpRequest} for the specified URI and HTTP method
* by using the passed-on request factory.
* <p>Called from {@link #createRequest(URI, HttpMethod)}.
* @param uri the URI to create a request for
* @param httpMethod the HTTP method to execute

View File

@@ -190,12 +190,14 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpClient client = getHttpClient();
Assert.state(client != null, "Synchronous execution requires an HttpClient to be set");
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
if (context == null) {
context = HttpClientContext.create();
}
// Request configuration not set in the context
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
// Use request configuration given by the user, when available
@@ -210,6 +212,7 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
}
if (this.bufferRequestBody) {
return new HttpComponentsClientHttpRequest(client, httpRequest, context);
}
@@ -285,20 +288,20 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
switch (httpMethod) {
case GET:
return new HttpGet(uri);
case DELETE:
return new HttpDelete(uri);
case HEAD:
return new HttpHead(uri);
case OPTIONS:
return new HttpOptions(uri);
case POST:
return new HttpPost(uri);
case PUT:
return new HttpPut(uri);
case TRACE:
return new HttpTrace(uri);
case PATCH:
return new HttpPatch(uri);
case DELETE:
return new HttpDelete(uri);
case OPTIONS:
return new HttpOptions(uri);
case TRACE:
return new HttpTrace(uri);
default:
throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -42,52 +42,51 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
private URI uri;
protected InterceptingClientHttpRequest(ClientHttpRequestFactory requestFactory,
List<ClientHttpRequestInterceptor> interceptors,
URI uri,
HttpMethod method) {
List<ClientHttpRequestInterceptor> interceptors, URI uri, HttpMethod method) {
this.requestFactory = requestFactory;
this.interceptors = interceptors;
this.method = method;
this.uri = uri;
}
@Override
public HttpMethod getMethod() {
return method;
return this.method;
}
@Override
public URI getURI() {
return uri;
return this.uri;
}
@Override
protected final ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
RequestExecution requestExecution = new RequestExecution();
InterceptingRequestExecution requestExecution = new InterceptingRequestExecution();
return requestExecution.execute(this, bufferedOutput);
}
private class RequestExecution implements ClientHttpRequestExecution {
private class InterceptingRequestExecution implements ClientHttpRequestExecution {
private final Iterator<ClientHttpRequestInterceptor> iterator;
private RequestExecution() {
public InterceptingRequestExecution() {
this.iterator = interceptors.iterator();
}
@Override
public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException {
if (iterator.hasNext()) {
ClientHttpRequestInterceptor nextInterceptor = iterator.next();
if (this.iterator.hasNext()) {
ClientHttpRequestInterceptor nextInterceptor = this.iterator.next();
return nextInterceptor.intercept(request, body, this);
}
else {
ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), request.getMethod());
delegate.getHeaders().putAll(request.getHeaders());
if (body.length > 0) {
StreamUtils.copy(body, delegate.getBody());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,20 +32,22 @@ public class InterceptingClientHttpRequestFactory extends AbstractClientHttpRequ
private final List<ClientHttpRequestInterceptor> interceptors;
/**
* Creates a new instance of the {@code InterceptingClientHttpRequestFactory} with the given parameters.
*
* Create a new instance of the {@code InterceptingClientHttpRequestFactory} with the given parameters.
* @param requestFactory the request factory to wrap
* @param interceptors the interceptors that are to be applied. Can be {@code null}.
* @param interceptors the interceptors that are to be applied (can be {@code null})
*/
public InterceptingClientHttpRequestFactory(ClientHttpRequestFactory requestFactory,
List<ClientHttpRequestInterceptor> interceptors) {
super(requestFactory);
this.interceptors = interceptors != null ? interceptors : Collections.<ClientHttpRequestInterceptor>emptyList();
this.interceptors = (interceptors != null ? interceptors : Collections.<ClientHttpRequestInterceptor>emptyList());
}
@Override
protected ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod, ClientHttpRequestFactory requestFactory) {
return new InterceptingClientHttpRequest(requestFactory, interceptors, uri, httpMethod);
return new InterceptingClientHttpRequest(requestFactory, this.interceptors, uri, httpMethod);
}
}

View File

@@ -39,7 +39,6 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
/**
* {@link org.springframework.http.client.ClientHttpRequestFactory} implementation that
* uses <a href="http://netty.io/">Netty 4</a> to create requests.
@@ -69,12 +68,12 @@ public class Netty4ClientHttpRequestFactory implements ClientHttpRequestFactory,
private SslContext sslContext;
private volatile Bootstrap bootstrap;
private int connectTimeout = -1;
private int readTimeout = -1;
private volatile Bootstrap bootstrap;
/**
* Create a new {@code Netty4ClientHttpRequestFactory} with a default

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -136,6 +136,7 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory,
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpURLConnection connection = openConnection(uri.toURL(), this.proxy);
prepareConnection(connection, httpMethod.name());
if (this.bufferRequestBody) {
return new SimpleBufferingClientHttpRequest(connection, this.outputStreaming);
}
@@ -146,20 +147,23 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory,
/**
* {@inheritDoc}
* <p>Setting the {@link #setTaskExecutor(org.springframework.core.task.AsyncListenableTaskExecutor) taskExecutor} property
* is required before calling this method.
* <p>Setting the {@link #setTaskExecutor taskExecutor} property is required before calling this method.
*/
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
Assert.state(this.taskExecutor != null, "Asynchronous execution requires an AsyncTaskExecutor to be set");
Assert.state(this.taskExecutor != null,
"Asynchronous execution requires an AsyncTaskExecutor to be set");
HttpURLConnection connection = openConnection(uri.toURL(), this.proxy);
prepareConnection(connection, httpMethod.name());
if (this.bufferRequestBody) {
return new SimpleBufferingAsyncClientHttpRequest(connection, this.outputStreaming, this.taskExecutor);
return new SimpleBufferingAsyncClientHttpRequest(
connection, this.outputStreaming, this.taskExecutor);
}
else {
return new SimpleStreamingAsyncClientHttpRequest(connection, this.chunkSize,
this.outputStreaming, this.taskExecutor);
return new SimpleStreamingAsyncClientHttpRequest(
connection, this.chunkSize, this.outputStreaming, this.taskExecutor);
}
}
@@ -192,20 +196,24 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory,
if (this.readTimeout >= 0) {
connection.setReadTimeout(this.readTimeout);
}
connection.setDoInput(true);
if ("GET".equals(httpMethod)) {
connection.setInstanceFollowRedirects(true);
}
else {
connection.setInstanceFollowRedirects(false);
}
if ("PUT".equals(httpMethod) || "POST".equals(httpMethod) ||
if ("POST".equals(httpMethod) || "PUT".equals(httpMethod) ||
"PATCH".equals(httpMethod) || "DELETE".equals(httpMethod)) {
connection.setDoOutput(true);
}
else {
connection.setDoOutput(false);
}
connection.setRequestMethod(httpMethod);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,8 +24,10 @@ import org.springframework.http.HttpRequest;
import org.springframework.util.Assert;
/**
* Provides a convenient implementation of the {@link HttpRequest} interface that can be overridden to adapt the
* request. Methods default to calling through to the wrapped request object.
* Provides a convenient implementation of the {@link HttpRequest} interface
* that can be overridden to adapt the request.
*
* <p>These methods default to calling through to the wrapped request object.
*
* @author Arjen Poutsma
* @since 3.1
@@ -36,24 +38,24 @@ public class HttpRequestWrapper implements HttpRequest {
/**
* Creates a new {@code HttpRequest} wrapping the given request object.
*
* Create a new {@code HttpRequest} wrapping the given request object.
* @param request the request object to be wrapped
*/
public HttpRequestWrapper(HttpRequest request) {
Assert.notNull(request, "'request' must not be null");
Assert.notNull(request, "HttpRequest must not be null");
this.request = request;
}
/**
* Returns the wrapped request.
* Return the wrapped request.
*/
public HttpRequest getRequest() {
return request;
return this.request;
}
/**
* Returns the method of the wrapped request.
* Return the method of the wrapped request.
*/
@Override
public HttpMethod getMethod() {
@@ -61,7 +63,7 @@ public class HttpRequestWrapper implements HttpRequest {
}
/**
* Returns the URI of the wrapped request.
* Return the URI of the wrapped request.
*/
@Override
public URI getURI() {
@@ -69,7 +71,7 @@ public class HttpRequestWrapper implements HttpRequest {
}
/**
* Returns the headers of the wrapped request.
* Return the headers of the wrapped request.
*/
@Override
public HttpHeaders getHeaders() {