SPR-7354 - Added equivalent of JAX-RS @Consumes to Spring MVC

This commit is contained in:
Arjen Poutsma
2011-04-08 12:06:53 +00:00
parent 0cdb237576
commit 44b4f59c5e
4 changed files with 49 additions and 26 deletions

View File

@@ -164,12 +164,9 @@ public final class RequestKey {
Set<RequestMethod> methods = union(this.methods, methodKey.methods);
RequestCondition params = RequestConditionFactory.and(this.paramsCondition, methodKey.paramsCondition);
RequestCondition headers = RequestConditionFactory.and(this.headersCondition, methodKey.headersCondition);
RequestCondition consumes;
// if (methodKey.consumesCondition.weight() > this.consumesCondition.weight()) {
//
// }
RequestCondition consumes = RequestConditionFactory.mostSpecific(methodKey.consumesCondition, this.consumesCondition);
return new RequestKey(patterns, methods, params, headers, null);
return new RequestKey(patterns, methods, params, headers, consumes);
}
private static Set<String> combinePatterns(Collection<String> typePatterns,
@@ -213,14 +210,16 @@ public final class RequestKey {
* @return a new request key that contains all matching attributes
*/
public RequestKey getMatchingKey(HttpServletRequest request, PathMatcher pathMatcher, UrlPathHelper urlPathHelper) {
if (!checkMethod(request) || !checkParams(request) || !checkHeaders(request)) {
if (!checkMethod(request) || !paramsCondition.match(request) || !headersCondition.match(request) ||
!consumesCondition.match(request)) {
return null;
}
else {
List<String> matchingPatterns = getMatchingPatterns(request, pathMatcher, urlPathHelper);
if (!matchingPatterns.isEmpty()) {
Set<RequestMethod> matchingMethods = getMatchingMethods(request);
return new RequestKey(matchingPatterns, matchingMethods, this.paramsCondition, this.headersCondition, null);
return new RequestKey(matchingPatterns, matchingMethods, this.paramsCondition, this.headersCondition,
this.consumesCondition);
}
else {
return null;
@@ -259,14 +258,6 @@ public final class RequestKey {
return methods.isEmpty() || methods.contains(RequestMethod.valueOf(request.getMethod()));
}
private boolean checkParams(HttpServletRequest request) {
return paramsCondition.match(request);
}
private boolean checkHeaders(HttpServletRequest request) {
return headersCondition.match(request);
}
private String getMatchingPattern(String pattern, String lookupPath, PathMatcher pathMatcher) {
if (pattern.equals(lookupPath) || pathMatcher.match(pattern, lookupPath)) {
return pattern;

View File

@@ -22,8 +22,8 @@ import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
/**
* @author Arjen Poutsma
*/
* @author Arjen Poutsma
*/
class ConsumesRequestCondition extends AbstractRequestCondition {
private final MediaType mediaType;
@@ -33,12 +33,14 @@ class ConsumesRequestCondition extends AbstractRequestCondition {
}
public boolean match(HttpServletRequest request) {
String contentTypeString = request.getContentType();
if (StringUtils.hasLength(contentTypeString)) {
MediaType contentType = MediaType.parseMediaType(contentTypeString);
return this.mediaType.includes(contentType);
MediaType contentType;
if (StringUtils.hasLength(request.getContentType())) {
contentType = MediaType.parseMediaType(request.getContentType());
}
return false;
else {
contentType = MediaType.APPLICATION_OCTET_STREAM;
}
return this.mediaType.includes(contentType);
}
@Override

View File

@@ -84,6 +84,16 @@ public abstract class RequestConditionFactory {
return FALSE_CONDITION;
}
public static RequestCondition mostSpecific(RequestCondition... conditions) {
if (ObjectUtils.isEmpty(conditions)) {
return trueCondition();
}
RequestCondition[] copy = new RequestCondition[conditions.length];
System.arraycopy(conditions, 0, copy, 0, conditions.length);
Arrays.sort(copy);
return copy[0];
}
/**
* Combines the given conditions into a logical AND, i.e. the returned condition will return {@code true} for {@link
* RequestCondition#match(HttpServletRequest)} if all of the given conditions do so.