SPR-7346 - @RequestHeader negation expressions (e.g. !Accept=text/plain) are not applied
This commit is contained in:
@@ -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");
|
||||
* 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.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.5.2
|
||||
*/
|
||||
abstract class ServletAnnotationMappingUtils {
|
||||
@@ -52,7 +53,8 @@ abstract class ServletAnnotationMappingUtils {
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public static boolean checkParameters(String[] params, HttpServletRequest request) {
|
||||
@@ -70,10 +72,11 @@ abstract class ServletAnnotationMappingUtils {
|
||||
}
|
||||
}
|
||||
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);
|
||||
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.
|
||||
* @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
|
||||
*/
|
||||
public static boolean checkHeaders(String[] headers, HttpServletRequest request) {
|
||||
@@ -101,7 +105,8 @@ abstract class ServletAnnotationMappingUtils {
|
||||
}
|
||||
}
|
||||
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);
|
||||
if (isMediaTypeHeader(key)) {
|
||||
List<MediaType> requestMediaTypes = MediaType.parseMediaTypes(request.getHeader(key));
|
||||
@@ -109,7 +114,8 @@ abstract class ServletAnnotationMappingUtils {
|
||||
boolean found = false;
|
||||
for (Iterator<MediaType> valIter = valueMediaTypes.iterator(); valIter.hasNext() && !found;) {
|
||||
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();
|
||||
if (valueMediaType.includes(requestMediaType)) {
|
||||
found = true;
|
||||
@@ -118,11 +124,11 @@ abstract class ServletAnnotationMappingUtils {
|
||||
|
||||
}
|
||||
if (!found) {
|
||||
return false;
|
||||
return negated;
|
||||
}
|
||||
}
|
||||
else if (!value.equals(request.getHeader(key))) {
|
||||
return false;
|
||||
return negated;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1315,7 +1315,24 @@ public class ServletAnnotationControllerTests {
|
||||
servlet.service(request, response);
|
||||
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
|
||||
public void acceptHeaders() throws ServletException, IOException {
|
||||
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
|
||||
public static class AcceptHeadersController {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user