SPR-7346 - @RequestHeader negation expressions (e.g. !Accept=text/plain) are not applied

This commit is contained in:
Arjen Poutsma
2010-07-15 08:49:18 +00:00
parent dcad6c341b
commit f8425a5f7b
3 changed files with 52 additions and 12 deletions

View File

@@ -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;
}
}
}