SPR-7354 - Added equivalent of JAX-RS @Consumes to Spring MVC
This commit is contained in:
parent
ff89c0e55a
commit
0cdb237576
|
|
@ -35,7 +35,6 @@ abstract class AbstractNameValueCondition<T> 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<T> extends AbstractRequestCondition {
|
|||
|
||||
protected abstract boolean matchValue(HttpServletRequest request);
|
||||
|
||||
@Override
|
||||
public int getWeight() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = name.hashCode();
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<RequestCondition> conditions;
|
||||
|
||||
protected RequestConditionComposite(List<RequestCondition> conditions) {
|
||||
super(getWeight(conditions));
|
||||
Assert.notEmpty(conditions, "'conditions' must not be empty");
|
||||
this.conditions = Collections.unmodifiableList(conditions);
|
||||
}
|
||||
|
||||
private static int getWeight(List<RequestCondition> conditions) {
|
||||
@Override
|
||||
public int getWeight() {
|
||||
int weight = 0;
|
||||
for (RequestCondition condition : conditions) {
|
||||
if (condition instanceof AbstractRequestCondition) {
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
Loading…
Reference in New Issue