Revised supportedMethods null handling in HttpRequestMethodNotSupportedException

Issue: SPR-15377
(cherry picked from commit 5ea4abd)
This commit is contained in:
Juergen Hoeller 2017-03-24 11:06:47 +01:00
parent 2233ec04ff
commit d0177b0f55
3 changed files with 29 additions and 24 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 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,6 +24,7 @@ import java.util.Set;
import javax.servlet.ServletException;
import org.springframework.http.HttpMethod;
import org.springframework.util.StringUtils;
/**
* Exception thrown when a request handler does not support a
@ -48,24 +49,6 @@ public class HttpRequestMethodNotSupportedException extends ServletException {
this(method, (String[]) null);
}
/**
* Create a new HttpRequestMethodNotSupportedException.
* @param method the unsupported HTTP request method
* @param supportedMethods the actually supported HTTP methods
*/
public HttpRequestMethodNotSupportedException(String method, String[] supportedMethods) {
this(method, supportedMethods, "Request method '" + method + "' not supported");
}
/**
* Create a new HttpRequestMethodNotSupportedException.
* @param method the unsupported HTTP request method
* @param supportedMethods the actually supported HTTP methods
*/
public HttpRequestMethodNotSupportedException(String method, Collection<String> supportedMethods) {
this(method, supportedMethods.toArray(new String[supportedMethods.size()]));
}
/**
* Create a new HttpRequestMethodNotSupportedException.
* @param method the unsupported HTTP request method
@ -75,6 +58,24 @@ public class HttpRequestMethodNotSupportedException extends ServletException {
this(method, null, msg);
}
/**
* Create a new HttpRequestMethodNotSupportedException.
* @param method the unsupported HTTP request method
* @param supportedMethods the actually supported HTTP methods (may be {@code null})
*/
public HttpRequestMethodNotSupportedException(String method, Collection<String> supportedMethods) {
this(method, StringUtils.toStringArray(supportedMethods));
}
/**
* Create a new HttpRequestMethodNotSupportedException.
* @param method the unsupported HTTP request method
* @param supportedMethods the actually supported HTTP methods (may be {@code null})
*/
public HttpRequestMethodNotSupportedException(String method, String[] supportedMethods) {
this(method, supportedMethods, "Request method '" + method + "' not supported");
}
/**
* Create a new HttpRequestMethodNotSupportedException.
* @param method the unsupported HTTP request method
@ -96,16 +97,21 @@ public class HttpRequestMethodNotSupportedException extends ServletException {
}
/**
* Return the actually supported HTTP methods, if known.
* Return the actually supported HTTP methods, or {@code null} if not known.
*/
public String[] getSupportedMethods() {
return this.supportedMethods;
}
/**
* Return the actually supported HTTP methods, if known, as {@link HttpMethod} instances.
* Return the actually supported HTTP methods as {@link HttpMethod} instances,
* or {@code null} if not known.
* @since 3.2
*/
public Set<HttpMethod> getSupportedHttpMethods() {
if (this.supportedMethods == null) {
return null;
}
List<HttpMethod> supportedMethods = new LinkedList<HttpMethod>();
for (String value : this.supportedMethods) {
HttpMethod resolved = HttpMethod.resolve(value);

View File

@ -252,7 +252,7 @@ public abstract class ResponseEntityExceptionHandler {
pageNotFoundLogger.warn(ex.getMessage());
Set<HttpMethod> supportedMethods = ex.getSupportedHttpMethods();
if (!supportedMethods.isEmpty()) {
if (!CollectionUtils.isEmpty(supportedMethods)) {
headers.setAllow(supportedMethods);
}
return handleExceptionInternal(ex, null, headers, status, request);

View File

@ -375,8 +375,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
// Check whether we should support the request method.
String method = request.getMethod();
if (this.supportedMethods != null && !this.supportedMethods.contains(method)) {
throw new HttpRequestMethodNotSupportedException(
method, StringUtils.toStringArray(this.supportedMethods));
throw new HttpRequestMethodNotSupportedException(method, this.supportedMethods);
}
// Check whether a session is required.