Add support for HTTP PATCH method

The HTTP PATCH method is now supported whereever HTTP methods are used.
Annotated controllers can be mapped to RequestMethod.PATCH.

On the client side the RestTemplate execute(..) and exchange(..)
methods can be used with HttpMethod.PATCH. In terms of HTTP client
libraries, Apache HttpComponents HttpClient version 4.2 or later is
required (see HTTPCLIENT-1191). The JDK HttpURLConnection does not
support the HTTP PATCH method.

Issue: SPR-7985
This commit is contained in:
Rossen Stoyanchev
2012-06-22 16:47:33 -04:00
parent f05e2bc56f
commit a0747458e7
18 changed files with 211 additions and 98 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 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.
@@ -26,6 +26,6 @@ package org.springframework.http;
*/
public enum HttpMethod {
GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE
GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -141,6 +141,9 @@ public class CommonsClientHttpRequestFactory implements ClientHttpRequestFactory
return new PutMethod(uri);
case TRACE:
return new TraceMethod(uri);
case PATCH:
throw new IllegalArgumentException(
"HTTP method PATCH not available before Apache HttpComponents HttpClient 4.2");
default:
throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -17,12 +17,9 @@
package org.springframework.http.client;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.URI;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
@@ -40,6 +37,10 @@ import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HttpContext;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* {@link org.springframework.http.client.ClientHttpRequestFactory} implementation that uses
@@ -155,11 +156,30 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
return new HttpPut(uri);
case TRACE:
return new HttpTrace(uri);
case PATCH:
return createHttpPatch(uri);
default:
throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
}
}
private HttpUriRequest createHttpPatch(URI uri) {
String className = "org.apache.http.client.methods.HttpPatch";
ClassLoader classloader = this.getClass().getClassLoader();
if (!ClassUtils.isPresent(className, classloader)) {
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);
}
}
/**
* Template method that allows for manipulating the {@link HttpUriRequest} before it is
* returned as part of a {@link HttpComponentsClientHttpRequest}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -152,7 +152,7 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory
else {
connection.setInstanceFollowRedirects(false);
}
if ("PUT".equals(httpMethod) || "POST".equals(httpMethod)) {
if ("PUT".equals(httpMethod) || "POST".equals(httpMethod) || "PATCH".equals(httpMethod)) {
connection.setDoOutput(true);
}
else {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 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.
@@ -21,7 +21,7 @@ import java.util.List;
import org.springframework.http.MediaType;
/**
* Exception thrown when a client POSTs or PUTs content
* Exception thrown when a client POSTs, PUTs, or PATCHes content of a type
* not supported by request handler.
*
* @author Arjen Poutsma

View File

@@ -271,7 +271,7 @@ public @interface RequestMapping {
/**
* The HTTP request methods to map to, narrowing the primary mapping:
* GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE.
* GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
* <p><b>Supported at the type level as well as at the method level!</b>
* When used at the type level, all method-level mappings inherit
* this HTTP method restriction (i.e. the type-level restriction

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 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.
@@ -22,7 +22,7 @@ package org.springframework.web.bind.annotation;
* {@link RequestMapping} annotation.
*
* <p>Note that, by default, {@link org.springframework.web.servlet.DispatcherServlet}
* supports GET, HEAD, POST, PUT and DELETE only. DispatcherServlet will
* supports GET, HEAD, POST, PUT, PATCH and DELETE only. DispatcherServlet will
* process TRACE and OPTIONS with the default HttpServlet behavior unless
* explicitly told to dispatch those request types as well: Check out
* the "dispatchOptionsRequest" and "dispatchTraceRequest" properties,
@@ -36,6 +36,6 @@ package org.springframework.web.bind.annotation;
*/
public enum RequestMethod {
GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE
GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -44,24 +44,24 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
/**
* {@link javax.servlet.Filter} that makes form encoded data available through
* the {@code ServletRequest.getParameter*()} family of methods during HTTP PUT
* requests.
*
* <p>The Servlet spec requires form data to be available for HTTP POST but
* not for HTTP PUT requests. This filter intercepts HTTP PUT requests where
* content type is {@code 'application/x-www-form-urlencoded'}, reads form
* encoded content from the body of the request, and wraps the ServletRequest
* {@link javax.servlet.Filter} that makes form encoded data available through
* the {@code ServletRequest.getParameter*()} family of methods during HTTP PUT
* or PATCH requests.
*
* <p>The Servlet spec requires form data to be available for HTTP POST but
* not for HTTP PUT or PATCH requests. This filter intercepts HTTP PUT and PATCH
* requests where content type is {@code 'application/x-www-form-urlencoded'},
* reads form encoded content from the body of the request, and wraps the ServletRequest
* in order to make the form data available as request parameters just like
* it is for HTTP POST requests.
*
*
* @author Rossen Stoyanchev
* @since 3.1
*/
public class HttpPutFormContentFilter extends OncePerRequestFilter {
private final FormHttpMessageConverter formConverter = new XmlAwareFormHttpMessageConverter();
/**
* The default character set to use for reading form data.
*/
@@ -73,7 +73,7 @@ public class HttpPutFormContentFilter extends OncePerRequestFilter {
protected void doFilterInternal(final HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
if ("PUT".equals(request.getMethod()) && isFormContentType(request)) {
if (("PUT".equals(request.getMethod()) || "PATCH".equals(request.getMethod())) && isFormContentType(request)) {
HttpInputMessage inputMessage = new ServletServerHttpRequest(request) {
@Override
public InputStream getBody() throws IOException {
@@ -102,7 +102,7 @@ public class HttpPutFormContentFilter extends OncePerRequestFilter {
}
private static class HttpPutFormContentRequestWrapper extends HttpServletRequestWrapper {
private MultiValueMap<String, String> formParameters;
public HttpPutFormContentRequestWrapper(HttpServletRequest request, MultiValueMap<String, String> parameters) {