Polishing

This commit is contained in:
Juergen Hoeller
2016-06-07 15:42:16 +02:00
parent 6807bcb863
commit 8c4bc3656b
48 changed files with 283 additions and 287 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -45,20 +45,6 @@ public abstract class HttpRange {
private static final String BYTE_RANGE_PREFIX = "bytes=";
/**
* Return the start of the range given the total length of a representation.
* @param length the length of the representation
* @return the start of this range for the representation
*/
public abstract long getRangeStart(long length);
/**
* Return the end of the range (inclusive) given the total length of a representation.
* @param length the length of the representation
* @return the end of the range for the representation
*/
public abstract long getRangeEnd(long length);
/**
* Turn a {@code Resource} into a {@link ResourceRegion} using the range
* information contained in the current {@code HttpRange}.
@@ -78,11 +64,25 @@ public abstract class HttpRange {
long end = getRangeEnd(contentLength);
return new ResourceRegion(resource, start, end - start + 1);
}
catch (IOException exc) {
throw new IllegalArgumentException("Can't convert this Resource to a ResourceRegion", exc);
catch (IOException ex) {
throw new IllegalArgumentException("Failed to convert Resource to ResourceRegion", ex);
}
}
/**
* Return the start of the range given the total length of a representation.
* @param length the length of the representation
* @return the start of this range for the representation
*/
public abstract long getRangeStart(long length);
/**
* Return the end of the range (inclusive) given the total length of a representation.
* @param length the length of the representation
* @return the end of the range for the representation
*/
public abstract long getRangeEnd(long length);
/**
* Create an {@code HttpRange} from the given position to the end.
@@ -165,7 +165,6 @@ public abstract class HttpRange {
* Convert each {@code HttpRange} into a {@code ResourceRegion},
* selecting the appropriate segment of the given {@code Resource}
* using the HTTP Range information.
*
* @param ranges the list of ranges
* @param resource the resource to select the regions from
* @return the list of regions for the given resource

View File

@@ -156,13 +156,20 @@ public class RequestEntity<T> extends HttpEntity<T> {
/**
* Return the type of the request's body.
* @return the request's body type
* @return the request's body type, or {@code null} if not known
* @since 4.3
*/
public Type getType() {
return (this.type == null && this.getBody() != null ? this.getBody().getClass() : this.type );
if (this.type == null) {
T body = getBody();
if (body != null) {
return body.getClass();
}
}
return this.type;
}
@Override
public boolean equals(Object other) {
if (this == other) {
@@ -172,8 +179,8 @@ public class RequestEntity<T> extends HttpEntity<T> {
return false;
}
RequestEntity<?> otherEntity = (RequestEntity<?>) other;
return (ObjectUtils.nullSafeEquals(this.method, otherEntity.method) &&
ObjectUtils.nullSafeEquals(this.url, otherEntity.url));
return (ObjectUtils.nullSafeEquals(getMethod(), otherEntity.getMethod()) &&
ObjectUtils.nullSafeEquals(getUrl(), otherEntity.getUrl()));
}
@Override
@@ -187,9 +194,9 @@ public class RequestEntity<T> extends HttpEntity<T> {
@Override
public String toString() {
StringBuilder builder = new StringBuilder("<");
builder.append(this.method);
builder.append(getMethod());
builder.append(' ');
builder.append(this.url);
builder.append(getUrl());
builder.append(',');
T body = getBody();
HttpHeaders headers = getHeaders();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,16 +16,16 @@
package org.springframework.http.client;
import java.io.IOException;
import org.springframework.http.HttpRequest;
import org.springframework.util.concurrent.ListenableFuture;
import java.io.IOException;
/**
* Represents the context of a client-side HTTP request execution.
*
* <p>Used to invoke the next interceptor in the interceptor chain, or - if the
* calling interceptor is last - execute the request itself.
* <p>Used to invoke the next interceptor in the interceptor chain, or -
* if the calling interceptor is last - execute the request itself.
*
* @author Jakub Narloch
* @author Rossen Stoyanchev
@@ -35,15 +35,13 @@ import java.io.IOException;
public interface AsyncClientHttpRequestExecution {
/**
* Resume the request execution by invoking next interceptor in the chain
* Resume the request execution by invoking the next interceptor in the chain
* or executing the request to the remote service.
*
* @param request the http request, containing the http method and headers
* @param body the body of the request
* @return the future
* @param request the HTTP request, containing the HTTP method and headers
* @param body the body of the request
* @return a corresponding future handle
* @throws IOException in case of I/O errors
*/
ListenableFuture<ClientHttpResponse> executeAsync(HttpRequest request, byte[] body)
throws IOException;
ListenableFuture<ClientHttpResponse> executeAsync(HttpRequest request, byte[] body) throws IOException;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,20 +16,19 @@
package org.springframework.http.client;
import java.io.IOException;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.support.InterceptingAsyncHttpAccessor;
import org.springframework.util.concurrent.ListenableFuture;
import java.io.IOException;
/**
* Intercepts client-side HTTP requests. Implementations of this interface can be
* {@linkplain org.springframework.web.client.AsyncRestTemplate#setInterceptors(java.util.List)
* registered} with the {@link org.springframework.web.client.AsyncRestTemplate
* AsyncRestTemplate} as to modify the outgoing {@link HttpRequest} and/or
* register to modify the incoming {@link ClientHttpResponse} with help of a
* {@link org.springframework.util.concurrent.ListenableFutureAdapter
* ListenableFutureAdapter}.
* {@linkplain org.springframework.web.client.AsyncRestTemplate#setInterceptors registered}
* with the {@link org.springframework.web.client.AsyncRestTemplate} as to modify
* the outgoing {@link HttpRequest} and/or register to modify the incoming
* {@link ClientHttpResponse} with help of a
* {@link org.springframework.util.concurrent.ListenableFutureAdapter}.
*
* <p>The main entry point for interceptors is {@link #intercept}.
*
@@ -41,34 +40,32 @@ import java.io.IOException;
*/
public interface AsyncClientHttpRequestInterceptor {
/**
* Intercept the given request, and return a response future. The given
* {@link AsyncClientHttpRequestExecution} allows the interceptor to pass on
* the request to the next entity in the chain.
*
* <p>An implementation might follow this pattern:
* <ol>
* <li>Examine the {@linkplain HttpRequest request} and body</li>
* <li>Optionally {@linkplain org.springframework.http.client.support.HttpRequestWrapper
* wrap} the request to filter HTTP attributes.</li>
* <li>Optionally modify the body of the request.</li>
* <li>One of the following:
* <ul>
* <li>execute the request through {@link ClientHttpRequestExecution}</li>
* <li>don't execute the request to block the execution altogether</li>
* </ul>
* <li>Optionally adapt the response to filter HTTP attributes with the help of
* {@link org.springframework.util.concurrent.ListenableFutureAdapter
* ListenableFutureAdapter}.</li>
* </ol>
*
* @param request the request, containing method, URI, and headers
* @param body the body of the request
* @param execution the request execution
* @return the response future
* @throws IOException in case of I/O errors
*/
ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body,
AsyncClientHttpRequestExecution execution) throws IOException;
/**
* Intercept the given request, and return a response future. The given
* {@link AsyncClientHttpRequestExecution} allows the interceptor to pass on
* the request to the next entity in the chain.
* <p>An implementation might follow this pattern:
* <ol>
* <li>Examine the {@linkplain HttpRequest request} and body</li>
* <li>Optionally {@linkplain org.springframework.http.client.support.HttpRequestWrapper
* wrap} the request to filter HTTP attributes.</li>
* <li>Optionally modify the body of the request.</li>
* <li>One of the following:
* <ul>
* <li>execute the request through {@link ClientHttpRequestExecution}</li>
* <li>don't execute the request to block the execution altogether</li>
* </ul>
* <li>Optionally adapt the response to filter HTTP attributes with the help of
* {@link org.springframework.util.concurrent.ListenableFutureAdapter
* ListenableFutureAdapter}.</li>
* </ol>
* @param request the request, containing method, URI, and headers
* @param body the body of the request
* @param execution the request execution
* @return the response future
* @throws IOException in case of I/O errors
*/
ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body,
AsyncClientHttpRequestExecution execution) throws IOException;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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,29 +32,28 @@ import org.springframework.http.HttpMethod;
*/
public class InterceptingAsyncClientHttpRequestFactory implements AsyncClientHttpRequestFactory {
private AsyncClientHttpRequestFactory delegate;
private AsyncClientHttpRequestFactory delegate;
private List<AsyncClientHttpRequestInterceptor> interceptors;
private List<AsyncClientHttpRequestInterceptor> interceptors;
/**
* Create new instance of {@link InterceptingAsyncClientHttpRequestFactory}
* with delegated request factory and list of interceptors.
*
* @param delegate the request factory to delegate to
* @param interceptors the list of interceptors to use
*/
public InterceptingAsyncClientHttpRequestFactory(AsyncClientHttpRequestFactory delegate,
List<AsyncClientHttpRequestInterceptor> interceptors) {
/**
* Create new instance of {@link InterceptingAsyncClientHttpRequestFactory}
* with delegated request factory and list of interceptors.
* @param delegate the request factory to delegate to
* @param interceptors the list of interceptors to use
*/
public InterceptingAsyncClientHttpRequestFactory(AsyncClientHttpRequestFactory delegate,
List<AsyncClientHttpRequestInterceptor> interceptors) {
this.delegate = delegate;
this.interceptors = (interceptors != null ? interceptors :
Collections.<AsyncClientHttpRequestInterceptor>emptyList());
}
this.delegate = delegate;
this.interceptors = (interceptors != null ? interceptors : Collections.<AsyncClientHttpRequestInterceptor>emptyList());
}
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod method) {
return new InterceptingAsyncClientHttpRequest(this.delegate, this.interceptors, uri, method);
}
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod method) {
return new InterceptingAsyncClientHttpRequest(this.delegate, this.interceptors, uri, method);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,17 +16,17 @@
package org.springframework.http.client.support;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.client.AsyncClientHttpRequestFactory;
import org.springframework.http.client.AsyncClientHttpRequestInterceptor;
import org.springframework.http.client.InterceptingAsyncClientHttpRequestFactory;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/**
* The HTTP accessor that extends the base {@link AsyncHttpAccessor} with request
* intercepting functionality.
* The HTTP accessor that extends the base {@link AsyncHttpAccessor} with
* request intercepting functionality.
*
* @author Jakub Narloch
* @author Rossen Stoyanchev
@@ -39,8 +39,7 @@ public abstract class InterceptingAsyncHttpAccessor extends AsyncHttpAccessor {
/**
* Sets the request interceptors that this accessor should use.
*
* Set the request interceptors that this accessor should use.
* @param interceptors the list of interceptors
*/
public void setInterceptors(List<AsyncClientHttpRequestInterceptor> interceptors) {
@@ -54,6 +53,7 @@ public abstract class InterceptingAsyncHttpAccessor extends AsyncHttpAccessor {
return this.interceptors;
}
@Override
public AsyncClientHttpRequestFactory getAsyncRequestFactory() {
AsyncClientHttpRequestFactory delegate = super.getAsyncRequestFactory();

View File

@@ -65,7 +65,7 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
}
@Override
protected ResourceRegion readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage)
protected ResourceRegion readInternal(Class<?> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
return null;
@@ -119,10 +119,8 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
}
}
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage)
throws IOException {
Assert.notNull(region, "ResourceRegion should not be null");
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
Assert.notNull(region, "ResourceRegion must not be null");
HttpHeaders responseHeaders = outputMessage.getHeaders();
long start = region.getPosition();
long end = start + region.getCount() - 1;
@@ -179,6 +177,7 @@ public class ResourceRegionHttpMessageConverter extends AbstractGenericHttpMessa
}
private static void println(OutputStream os) throws IOException {
os.write('\r');
os.write('\n');

View File

@@ -77,12 +77,11 @@ public @interface ModelAttribute {
String name() default "";
/**
* Allows declaring data binding disabled directly on an
* {@code @ModelAttribute} method parameter or on the attribute returned from
* an {@code @ModelAttribute} method, both of which would prevent data
* binding for that attribute.
* <p>By default this is set to "true" in which case data binding applies.
* Set this to "false" to disable data binding.
* Allows declaring data binding disabled directly on an {@code @ModelAttribute}
* method parameter or on the attribute returned from an {@code @ModelAttribute}
* method, both of which would prevent data binding for that attribute.
* <p>By default this is set to {@code true} in which case data binding applies.
* Set this to {@code false} to disable data binding.
* @since 4.3
*/
boolean binding() default true;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -56,9 +56,9 @@ public @interface RequestAttribute {
/**
* Whether the request attribute is required.
* <p>Defaults to {@code true}, leading to an exception being thrown
* if the attribute is missing. Switch this to {@code false} if you prefer
* a {@code null} or Java 1.8+ {@code java.util.Optional} if the attribute
* <p>Defaults to {@code true}, leading to an exception being thrown if
* the attribute is missing. Switch this to {@code false} if you prefer
* a {@code null} or Java 8 {@code java.util.Optional} if the attribute
* doesn't exist.
*/
boolean required() default true;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -66,7 +66,7 @@ public @interface SessionAttribute {
* Whether the session attribute is required.
* <p>Defaults to {@code true}, leading to an exception being thrown
* if the attribute is missing in the session or there is no session.
* Switch this to {@code false} if you prefer a {@code null} or Java 1.8+
* Switch this to {@code false} if you prefer a {@code null} or Java 8
* {@code java.util.Optional} if the attribute doesn't exist.
*/
boolean required() default true;

View File

@@ -154,7 +154,6 @@ public class AsyncRestTemplate extends InterceptingAsyncHttpAccessor implements
/**
* Configure default URI variable values. This is a shortcut for:
* <pre class="code">
*
* DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler();
* handler.setDefaultUriVariables(...);
*
@@ -167,7 +166,7 @@ public class AsyncRestTemplate extends InterceptingAsyncHttpAccessor implements
public void setDefaultUriVariables(Map<String, ?> defaultUriVariables) {
UriTemplateHandler handler = this.syncTemplate.getUriTemplateHandler();
Assert.isInstanceOf(AbstractUriTemplateHandler.class, handler,
"Can only use this property in conjunction with a DefaultUriTemplateHandler.");
"Can only use this property in conjunction with a DefaultUriTemplateHandler");
((AbstractUriTemplateHandler) handler).setDefaultUriVariables(defaultUriVariables);
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.client;
import java.io.UnsupportedEncodingException;
@@ -21,12 +22,12 @@ import java.nio.charset.Charset;
import org.springframework.http.HttpHeaders;
/**
* Abstract base class for exceptions that contain actual HTTP response data.
* Common base class for exceptions that contain actual HTTP response data.
*
* @author Rossen Stoyanchev
* @since 4.3
*/
public abstract class RestClientResponseException extends RestClientException {
public class RestClientResponseException extends RestClientException {
private static final long serialVersionUID = -8803556342728481792L;

View File

@@ -240,7 +240,6 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
/**
* Configure default URI variable values. This is a shortcut for:
* <pre class="code">
*
* DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler();
* handler.setDefaultUriVariables(...);
*
@@ -252,7 +251,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
*/
public void setDefaultUriVariables(Map<String, ?> defaultUriVariables) {
Assert.isInstanceOf(AbstractUriTemplateHandler.class, this.uriTemplateHandler,
"Can only use this property in conjunction with an AbstractUriTemplateHandler.");
"Can only use this property in conjunction with an AbstractUriTemplateHandler");
((AbstractUriTemplateHandler) this.uriTemplateHandler).setDefaultUriVariables(defaultUriVariables);
}

View File

@@ -63,6 +63,7 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
FORWARDED_HEADER_NAMES.add("X-Forwarded-Prefix");
}
private final UrlPathHelper pathHelper = new UrlPathHelper();
@@ -114,7 +115,6 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
private final Map<String, List<String>> headers;
public ForwardedHeaderRequestWrapper(HttpServletRequest request, UrlPathHelper pathHelper) {
super(request);

View File

@@ -46,11 +46,12 @@ public abstract class MultipartResolutionDelegate {
static {
try {
servletPartClass = ClassUtils.forName(
"javax.servlet.http.Part", MultipartResolutionDelegate.class.getClassLoader());
servletPartClass = ClassUtils.forName("javax.servlet.http.Part",
MultipartResolutionDelegate.class.getClassLoader());
}
catch (ClassNotFoundException ex) {
// Servlet 3.0 Part type not available - Part references simply not supported then.
// Servlet 3.0 javax.servlet.http.Part type not available -
// Part references simply not supported then.
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.util;
import java.net.URI;
@@ -106,6 +107,7 @@ public abstract class AbstractUriTemplateHandler implements UriTemplateHandler {
return insertBaseUrl(url);
}
/**
* Actually expand and encode the URI template.
*/
@@ -116,13 +118,15 @@ public abstract class AbstractUriTemplateHandler implements UriTemplateHandler {
*/
protected abstract URI expandInternal(String uriTemplate, Object... uriVariables);
/**
* Insert a base URL (if configured) unless the given URL has a host already.
*/
private URI insertBaseUrl(URI url) {
try {
if (getBaseUrl() != null && url.getHost() == null) {
url = new URI(getBaseUrl() + url.toString());
String baseUrl = getBaseUrl();
if (baseUrl != null && url.getHost() == null) {
url = new URI(baseUrl + url.toString());
}
return url;
}