From 0cdb2375761552cb0a6d5b8b9eb7da17aaa6abe2 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Fri, 8 Apr 2011 11:12:54 +0000 Subject: [PATCH] SPR-7354 - Added equivalent of JAX-RS @Consumes to Spring MVC --- .../condition/AbstractNameValueCondition.java | 6 +++++- .../condition/AbstractRequestCondition.java | 12 ++---------- .../condition/ConsumesRequestCondition.java | 5 ++++- .../condition/RequestConditionComposite.java | 7 +++++-- .../method/condition/RequestConditionFactory.java | 15 +++++++++++++-- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/AbstractNameValueCondition.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/AbstractNameValueCondition.java index 567b5fd961..034866f5b7 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/AbstractNameValueCondition.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/AbstractNameValueCondition.java @@ -35,7 +35,6 @@ abstract class AbstractNameValueCondition extends AbstractRequestCondition { protected final boolean isNegated; AbstractNameValueCondition(String expression) { - super(1); int separator = expression.indexOf('='); if (separator == -1) { this.isNegated = expression.startsWith("!"); @@ -66,6 +65,11 @@ abstract class AbstractNameValueCondition extends AbstractRequestCondition { protected abstract boolean matchValue(HttpServletRequest request); + @Override + public int getWeight() { + return 1; + } + @Override public int hashCode() { int result = name.hashCode(); diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/AbstractRequestCondition.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/AbstractRequestCondition.java index 4cc921b5e0..4694f2d43b 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/AbstractRequestCondition.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/AbstractRequestCondition.java @@ -24,19 +24,11 @@ package org.springframework.web.servlet.mvc.method.condition; */ public abstract class AbstractRequestCondition implements RequestCondition { - private final int weight; - - protected AbstractRequestCondition(int weight) { - this.weight = weight; - } - - public int getWeight() { - return weight; - } + public abstract int getWeight(); public int compareTo(RequestCondition o) { AbstractRequestCondition other = (AbstractRequestCondition) o; - return other.weight - this.weight; + return other.getWeight() - this.getWeight(); } } diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/ConsumesRequestCondition.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/ConsumesRequestCondition.java index 9147146b25..7de8d28891 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/ConsumesRequestCondition.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/ConsumesRequestCondition.java @@ -29,7 +29,6 @@ class ConsumesRequestCondition extends AbstractRequestCondition { private final MediaType mediaType; ConsumesRequestCondition(String mediaType) { - super(1); this.mediaType = MediaType.parseMediaType(mediaType); } @@ -42,4 +41,8 @@ class ConsumesRequestCondition extends AbstractRequestCondition { return false; } + @Override + public int getWeight() { + return 1; + } } diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/RequestConditionComposite.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/RequestConditionComposite.java index c56b329311..86a3265d3f 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/RequestConditionComposite.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/RequestConditionComposite.java @@ -20,6 +20,8 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; +import org.springframework.util.Assert; + /** * Abstract base class for {@link RequestCondition} implementations that wrap other request conditions. * @@ -31,11 +33,12 @@ abstract class RequestConditionComposite extends AbstractRequestCondition { protected final List conditions; protected RequestConditionComposite(List conditions) { - super(getWeight(conditions)); + Assert.notEmpty(conditions, "'conditions' must not be empty"); this.conditions = Collections.unmodifiableList(conditions); } - private static int getWeight(List conditions) { + @Override + public int getWeight() { int weight = 0; for (RequestCondition condition : conditions) { if (condition instanceof AbstractRequestCondition) { diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/RequestConditionFactory.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/RequestConditionFactory.java index 85e53128b2..6c9fc89e18 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/RequestConditionFactory.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/condition/RequestConditionFactory.java @@ -33,22 +33,33 @@ import org.springframework.util.ObjectUtils; */ public abstract class RequestConditionFactory { - private static final RequestCondition TRUE_CONDITION = new AbstractRequestCondition(0) { + private static final RequestCondition TRUE_CONDITION = new AbstractRequestCondition() { public boolean match(HttpServletRequest request) { return true; } + @Override + public int getWeight() { + return 0; + } + @Override public String toString() { return "TRUE"; } }; - private static final RequestCondition FALSE_CONDITION = new AbstractRequestCondition(0) { + private static final RequestCondition FALSE_CONDITION = new AbstractRequestCondition() { public boolean match(HttpServletRequest request) { return false; } + @Override + public int getWeight() { + return 0; + } + + @Override public String toString() { return "FALSE";