SPR-7346 - @RequestHeader negation expressions (e.g. !Accept=text/plain) are not applied
This commit is contained in:
parent
dcad6c341b
commit
f8425a5f7b
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2010 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -29,6 +29,7 @@ import org.springframework.web.util.WebUtils;
|
||||||
* Helper class for annotation-based request mapping.
|
* Helper class for annotation-based request mapping.
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Arjen Poutsma
|
||||||
* @since 2.5.2
|
* @since 2.5.2
|
||||||
*/
|
*/
|
||||||
abstract class ServletAnnotationMappingUtils {
|
abstract class ServletAnnotationMappingUtils {
|
||||||
|
|
@ -52,7 +53,8 @@ abstract class ServletAnnotationMappingUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether the given request matches the specified parameter conditions.
|
* Check whether the given request matches the specified parameter conditions.
|
||||||
* @param params the parameter conditions, following {@link RequestMapping#params()}
|
* @param params the parameter conditions, following
|
||||||
|
* {@link org.springframework.web.bind.annotation.RequestMapping#params() RequestMapping.#params()}
|
||||||
* @param request the current HTTP request to check
|
* @param request the current HTTP request to check
|
||||||
*/
|
*/
|
||||||
public static boolean checkParameters(String[] params, HttpServletRequest request) {
|
public static boolean checkParameters(String[] params, HttpServletRequest request) {
|
||||||
|
|
@ -70,10 +72,11 @@ abstract class ServletAnnotationMappingUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
String key = param.substring(0, separator);
|
boolean negated = separator > 0 && param.charAt(separator - 1) == '!';
|
||||||
|
String key = !negated ? param.substring(0, separator) : param.substring(0, separator - 1);
|
||||||
String value = param.substring(separator + 1);
|
String value = param.substring(separator + 1);
|
||||||
if (!value.equals(request.getParameter(key))) {
|
if (!value.equals(request.getParameter(key))) {
|
||||||
return false;
|
return negated;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -83,7 +86,8 @@ abstract class ServletAnnotationMappingUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether the given request matches the specified header conditions.
|
* Check whether the given request matches the specified header conditions.
|
||||||
* @param headers the header conditions, following {@link RequestMapping#headers()}
|
* @param headers the header conditions, following
|
||||||
|
* {@link org.springframework.web.bind.annotation.RequestMapping#headers() RequestMapping.headers()}
|
||||||
* @param request the current HTTP request to check
|
* @param request the current HTTP request to check
|
||||||
*/
|
*/
|
||||||
public static boolean checkHeaders(String[] headers, HttpServletRequest request) {
|
public static boolean checkHeaders(String[] headers, HttpServletRequest request) {
|
||||||
|
|
@ -101,7 +105,8 @@ abstract class ServletAnnotationMappingUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
String key = header.substring(0, separator);
|
boolean negated = separator > 0 && header.charAt(separator - 1) == '!';
|
||||||
|
String key = !negated ? header.substring(0, separator) : header.substring(0, separator - 1);
|
||||||
String value = header.substring(separator + 1);
|
String value = header.substring(separator + 1);
|
||||||
if (isMediaTypeHeader(key)) {
|
if (isMediaTypeHeader(key)) {
|
||||||
List<MediaType> requestMediaTypes = MediaType.parseMediaTypes(request.getHeader(key));
|
List<MediaType> requestMediaTypes = MediaType.parseMediaTypes(request.getHeader(key));
|
||||||
|
|
@ -109,7 +114,8 @@ abstract class ServletAnnotationMappingUtils {
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
for (Iterator<MediaType> valIter = valueMediaTypes.iterator(); valIter.hasNext() && !found;) {
|
for (Iterator<MediaType> valIter = valueMediaTypes.iterator(); valIter.hasNext() && !found;) {
|
||||||
MediaType valueMediaType = valIter.next();
|
MediaType valueMediaType = valIter.next();
|
||||||
for (Iterator<MediaType> reqIter = requestMediaTypes.iterator(); reqIter.hasNext() && !found;) {
|
for (Iterator<MediaType> reqIter = requestMediaTypes.iterator();
|
||||||
|
reqIter.hasNext() && !found;) {
|
||||||
MediaType requestMediaType = reqIter.next();
|
MediaType requestMediaType = reqIter.next();
|
||||||
if (valueMediaType.includes(requestMediaType)) {
|
if (valueMediaType.includes(requestMediaType)) {
|
||||||
found = true;
|
found = true;
|
||||||
|
|
@ -118,11 +124,11 @@ abstract class ServletAnnotationMappingUtils {
|
||||||
|
|
||||||
}
|
}
|
||||||
if (!found) {
|
if (!found) {
|
||||||
return false;
|
return negated;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!value.equals(request.getHeader(key))) {
|
else if (!value.equals(request.getHeader(key))) {
|
||||||
return false;
|
return negated;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1315,7 +1315,24 @@ public class ServletAnnotationControllerTests {
|
||||||
servlet.service(request, response);
|
servlet.service(request, response);
|
||||||
assertEquals("text", response.getContentAsString());
|
assertEquals("text", response.getContentAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void negatedContentTypeHeaders() throws ServletException, IOException {
|
||||||
|
initServlet(NegatedContentTypeHeadersController.class);
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/something");
|
||||||
|
request.addHeader("Content-Type", "application/pdf");
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
servlet.service(request, response);
|
||||||
|
assertEquals("pdf", response.getContentAsString());
|
||||||
|
|
||||||
|
request = new MockHttpServletRequest("POST", "/something");
|
||||||
|
request.addHeader("Content-Type", "text/html");
|
||||||
|
response = new MockHttpServletResponse();
|
||||||
|
servlet.service(request, response);
|
||||||
|
assertEquals("non-pdf", response.getContentAsString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void acceptHeaders() throws ServletException, IOException {
|
public void acceptHeaders() throws ServletException, IOException {
|
||||||
initServlet(AcceptHeadersController.class);
|
initServlet(AcceptHeadersController.class);
|
||||||
|
|
@ -2431,6 +2448,21 @@ public class ServletAnnotationControllerTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public static class NegatedContentTypeHeadersController {
|
||||||
|
|
||||||
|
@RequestMapping(value = "/something", headers = "content-type=application/pdf")
|
||||||
|
public void handlePdf(Writer writer) throws IOException {
|
||||||
|
writer.write("pdf");
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/something", headers = "content-type!=application/pdf")
|
||||||
|
public void handleNonPdf(Writer writer) throws IOException {
|
||||||
|
writer.write("non-pdf");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public static class AcceptHeadersController {
|
public static class AcceptHeadersController {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -252,7 +252,8 @@ public @interface RequestMapping {
|
||||||
* The parameters of the mapped request, narrowing the primary mapping.
|
* The parameters of the mapped request, narrowing the primary mapping.
|
||||||
* <p>Same format for any environment: a sequence of "myParam=myValue" style
|
* <p>Same format for any environment: a sequence of "myParam=myValue" style
|
||||||
* expressions, with a request only mapped if each such parameter is found
|
* expressions, with a request only mapped if each such parameter is found
|
||||||
* to have the given value. "myParam" style expressions are also supported,
|
* to have the given value. Expressions can be negated by using the "!=" operator,
|
||||||
|
* as in "myParam!=myValue". "myParam" style expressions are also supported,
|
||||||
* with such parameters having to be present in the request (allowed to have
|
* with such parameters having to be present in the request (allowed to have
|
||||||
* any value). Finally, "!myParam" style expressions indicate that the
|
* any value). Finally, "!myParam" style expressions indicate that the
|
||||||
* specified parameter is <i>not</i> supposed to be present in the request.
|
* specified parameter is <i>not</i> supposed to be present in the request.
|
||||||
|
|
@ -275,7 +276,8 @@ public @interface RequestMapping {
|
||||||
* The headers of the mapped request, narrowing the primary mapping.
|
* The headers of the mapped request, narrowing the primary mapping.
|
||||||
* <p>Same format for any environment: a sequence of "My-Header=myValue" style
|
* <p>Same format for any environment: a sequence of "My-Header=myValue" style
|
||||||
* expressions, with a request only mapped if each such header is found
|
* expressions, with a request only mapped if each such header is found
|
||||||
* to have the given value. "My-Header" style expressions are also supported,
|
* to have the given value. Expressions can be negated by using the "!=" operator,
|
||||||
|
* as in "My-Header!=myValue". "My-Header" style expressions are also supported,
|
||||||
* with such headers having to be present in the request (allowed to have
|
* with such headers having to be present in the request (allowed to have
|
||||||
* any value). Finally, "!My-Header" style expressions indicate that the
|
* any value). Finally, "!My-Header" style expressions indicate that the
|
||||||
* specified header is <i>not</i> supposed to be present in the request.
|
* specified header is <i>not</i> supposed to be present in the request.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue