Remove consumes from @RequestMapping

This commit is contained in:
Rossen Stoyanchev
2011-04-21 14:54:55 +00:00
parent acb9433e5c
commit 945cf43e2d
5 changed files with 48 additions and 40 deletions

View File

@@ -129,7 +129,8 @@ public class RequestMappingHandlerMethodMapping extends AbstractHandlerMethodMap
private static RequestMappingInfo createFromRequestMapping(RequestMapping annotation) {
return new RequestMappingInfo(Arrays.asList(annotation.value()), Arrays.asList(annotation.method()),
RequestConditionFactory.parseParams(annotation.params()),
RequestConditionFactory.parseHeaders(annotation.headers()));
RequestConditionFactory.parseHeaders(annotation.headers()),
RequestConditionFactory.parseConsumes());
}
@Override

View File

@@ -53,6 +53,8 @@ public final class RequestMappingInfo {
private final RequestCondition headersCondition;
private final RequestCondition consumesCondition;
private int hash;
/**
@@ -61,7 +63,7 @@ public final class RequestMappingInfo {
* <p>Package protected for testing purposes.
*/
RequestMappingInfo(Collection<String> patterns, Collection<RequestMethod> methods) {
this(patterns, methods, null, null);
this(patterns, methods, null, null, null);
}
/**
@@ -70,11 +72,13 @@ public final class RequestMappingInfo {
public RequestMappingInfo(Collection<String> patterns,
Collection<RequestMethod> methods,
RequestCondition paramsCondition,
RequestCondition headersCondition) {
RequestCondition headersCondition,
RequestCondition consumesCondition) {
this.patterns = asUnmodifiableSet(prependLeadingSlash(patterns));
this.methods = asUnmodifiableSet(methods);
this.paramsCondition = paramsCondition != null ? paramsCondition : RequestConditionFactory.trueCondition();
this.headersCondition = headersCondition != null ? headersCondition : RequestConditionFactory.trueCondition();
this.consumesCondition = consumesCondition != null ? consumesCondition : RequestConditionFactory.trueCondition();
}
private static Set<String> prependLeadingSlash(Collection<String> patterns) {
@@ -139,6 +143,7 @@ public final class RequestMappingInfo {
* <li>HTTP methods are combined as union of all HTTP methods listed in both keys.
* <li>Request parameter are combined into a logical AND.
* <li>Request header are combined into a logical AND.
* <li>Consumes .. TODO
* </ul>
* @param methodKey the key to combine with
* @param pathMatcher to {@linkplain PathMatcher#combine(String, String) combine} the patterns
@@ -149,8 +154,9 @@ public final class RequestMappingInfo {
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 = RequestConditionFactory.mostSpecific(methodKey.consumesCondition, this.consumesCondition);
return new RequestMappingInfo(patterns, methods, params, headers);
return new RequestMappingInfo(patterns, methods, params, headers, consumes);
}
private static Set<String> combinePatterns(Collection<String> typePatterns,
@@ -197,14 +203,16 @@ public final class RequestMappingInfo {
* @return a new request key that contains all matching attributes, or {@code null} if not all conditions match
*/
public RequestMappingInfo getMatchingRequestMapping(String lookupPath, HttpServletRequest request, PathMatcher pathMatcher) {
if (!checkMethod(request) || !paramsCondition.match(request) || !headersCondition.match(request)) {
if (!checkMethod(request) || !paramsCondition.match(request) || !headersCondition.match(request) ||
!consumesCondition.match(request)) {
return null;
}
else {
List<String> matchingPatterns = getMatchingPatterns(lookupPath, request, pathMatcher);
if (!matchingPatterns.isEmpty()) {
Set<RequestMethod> matchingMethods = getMatchingMethod(request);
return new RequestMappingInfo(matchingPatterns, matchingMethods, this.paramsCondition, this.headersCondition);
return new RequestMappingInfo(matchingPatterns, matchingMethods, this.paramsCondition, this.headersCondition,
this.consumesCondition);
}
else {
return null;
@@ -297,6 +305,7 @@ public final class RequestMappingInfo {
}
builder.append(",params=").append(paramsCondition.toString());
builder.append(",headers=").append(headersCondition.toString());
builder.append(",consumes=").append(consumesCondition.toString());
builder.append('}');
return builder.toString();
}

View File

@@ -100,7 +100,7 @@ public class RequestKeyComparatorTests {
RequestMappingInfo empty = new RequestMappingInfo(null, null);
RequestMappingInfo oneMethod = new RequestMappingInfo(null, asList(RequestMethod.GET));
RequestMappingInfo oneMethodOneParam =
new RequestMappingInfo(null, asList(RequestMethod.GET), RequestConditionFactory.parseParams("foo"), null);
new RequestMappingInfo(null, asList(RequestMethod.GET), RequestConditionFactory.parseParams("foo"), null, null);
List<RequestMappingInfo> list = asList(empty, oneMethod, oneMethodOneParam);
Collections.shuffle(list);
Collections.sort(list, handlerMapping.getMappingComparator("", request));
@@ -113,8 +113,8 @@ public class RequestKeyComparatorTests {
@Test
@Ignore // TODO : remove ignore
public void acceptHeaders() {
RequestMappingInfo html = new RequestMappingInfo(null, null, null, RequestConditionFactory.parseHeaders("accept=text/html"));
RequestMappingInfo xml = new RequestMappingInfo(null, null, null, RequestConditionFactory.parseHeaders("accept=application/xml"));
RequestMappingInfo html = new RequestMappingInfo(null, null, null, RequestConditionFactory.parseHeaders("accept=text/html"), null);
RequestMappingInfo xml = new RequestMappingInfo(null, null, null, RequestConditionFactory.parseHeaders("accept=application/xml"), null);
RequestMappingInfo none = new RequestMappingInfo(null, null);
request.addHeader("Accept", "application/xml, text/html");

View File

@@ -179,12 +179,12 @@ public class RequestKeyTests {
request.setParameter("foo", "bar");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
RequestMappingInfo key = new RequestMappingInfo(asList("/foo"), null, RequestConditionFactory.parseParams("foo=bar"), null);
RequestMappingInfo key = new RequestMappingInfo(asList("/foo"), null, RequestConditionFactory.parseParams("foo=bar"), null, null);
RequestMappingInfo match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNotNull(match);
key = new RequestMappingInfo(singleton("/foo"), null, RequestConditionFactory.parseParams("foo!=bar"), null);
key = new RequestMappingInfo(singleton("/foo"), null, RequestConditionFactory.parseParams("foo!=bar"), null, null);
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNull(match);
@@ -197,36 +197,36 @@ public class RequestKeyTests {
request.addHeader("foo", "bar");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
RequestMappingInfo key = new RequestMappingInfo(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo=bar"));
RequestMappingInfo key = new RequestMappingInfo(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo=bar"), null);
RequestMappingInfo match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNotNull(match);
key = new RequestMappingInfo(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo!=bar"));
key = new RequestMappingInfo(singleton("/foo"), null, null, RequestConditionFactory.parseHeaders("foo!=bar"), null);
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNull(match);
}
// @Test
// public void consumesCondition() {
// PathMatcher pathMatcher = new AntPathMatcher();
// MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
// request.setContentType("text/plain");
// String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
//
// RequestMappingInfo key = new RequestMappingInfo(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
// "text/plain"));
// RequestMappingInfo match = key.getMatchingKey(lookupPath, request, pathMatcher);
//
// assertNotNull(match);
//
// key = new RequestMappingInfo(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
// "application/xml"));
// match = key.getMatchingKey(lookupPath, request, pathMatcher);
//
// assertNull(match);
// }
@Test
public void consumesCondition() {
PathMatcher pathMatcher = new AntPathMatcher();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.setContentType("text/plain");
String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
RequestMappingInfo key = new RequestMappingInfo(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
"text/plain"));
RequestMappingInfo match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNotNull(match);
key = new RequestMappingInfo(singleton("/foo"), null, null, null, RequestConditionFactory.parseConsumes(
"application/xml"));
match = key.getMatchingRequestMapping(lookupPath, request, pathMatcher);
assertNull(match);
}
private RequestMappingInfo createKeyFromPatterns(String... patterns) {
return new RequestMappingInfo(asList(patterns), null);