From 4623568bce4b95f4016204cb23a9f9e723c2f6dc Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 25 Jun 2012 14:11:48 -0400 Subject: [PATCH] Polish client support for HTTP PATCH Issue: SPR-7985 --- ...HttpComponentsClientHttpRequestFactory.java | 18 +++++++----------- .../web/client/RestTemplate.java | 4 ++++ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java index 7e8f6c5d0b4..d3da596e906 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java @@ -17,7 +17,6 @@ package org.springframework.http.client; import java.io.IOException; -import java.lang.reflect.Constructor; import java.net.URI; import org.apache.http.client.HttpClient; @@ -25,6 +24,7 @@ import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpOptions; +import org.apache.http.client.methods.HttpPatch; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpTrace; @@ -56,6 +56,9 @@ import org.springframework.util.ClassUtils; */ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory, DisposableBean { + private static final boolean HTTP_PATCH_AVAILABLE = ClassUtils.isPresent( + "org.apache.http.client.methods.HttpPatch", HttpComponentsClientHttpRequestFactory.class.getClassLoader()); + private static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 100; private static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 5; @@ -164,19 +167,12 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest } private HttpUriRequest createHttpPatch(URI uri) { - String className = "org.apache.http.client.methods.HttpPatch"; - ClassLoader classloader = this.getClass().getClassLoader(); - if (!ClassUtils.isPresent(className, classloader)) { + if (!HTTP_PATCH_AVAILABLE) { throw new IllegalArgumentException( "HTTP method PATCH not available before Apache HttpComponents HttpClient 4.2"); } - try { - Class clazz = classloader.loadClass(className); - Constructor constructor = clazz.getConstructor(URI.class); - return (HttpUriRequest) constructor.newInstance(uri); - } - catch (Throwable ex) { - throw new IllegalStateException("Unable to instantiate " + className, ex); + else { + return new HttpPatch(uri); } } diff --git a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java index 770551895df..6e6bcf5ab3c 100644 --- a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java @@ -69,6 +69,10 @@ import org.springframework.web.util.UriUtils; * any{@link #exchange} * {@link #execute} * + *

The {@code exchange} and {@code execute} methods are generalized versions of the more specific methods listed + * above them. They support additional, less frequently used combinations including support for requests using the + * HTTP PATCH method. However, note that the underlying HTTP library must also support the desired combination.

+ * *

For each of these HTTP methods, there are three corresponding Java methods in the {@code RestTemplate}. Two * variant take a {@code String} URI as first argument (eg. {@link #getForObject(String, Class, Object[])}, {@link * #getForObject(String, Class, Map)}), and are capable of substituting any {@linkplain UriTemplate URI templates} in