SPR-7354 - Added equivalent of JAX-RS @Consumes to Spring MVC
This commit is contained in:
@@ -66,7 +66,7 @@ abstract class AbstractNameValueCondition<T> extends AbstractRequestCondition {
|
||||
protected abstract boolean matchValue(HttpServletRequest request);
|
||||
|
||||
@Override
|
||||
public int getWeight() {
|
||||
public int getSpecificity() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,18 +17,25 @@
|
||||
package org.springframework.web.servlet.mvc.method.condition;
|
||||
|
||||
/**
|
||||
* Abstract base class for {@link RequestCondition} that provides a standard {@link Comparable} implementation.
|
||||
* Abstract base class for {@link RequestCondition} that provides a standard {@link Comparable} implementation based on
|
||||
* the conditions {@linkplain #getSpecificity() specificity}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.1
|
||||
*/
|
||||
public abstract class AbstractRequestCondition implements RequestCondition {
|
||||
|
||||
public abstract int getWeight();
|
||||
/**
|
||||
* Returns the conditions specificity. More specific conditions should return a higher value than ones which are less
|
||||
* so.
|
||||
*
|
||||
* @return the conditions specificity
|
||||
*/
|
||||
protected abstract int getSpecificity();
|
||||
|
||||
public int compareTo(RequestCondition o) {
|
||||
AbstractRequestCondition other = (AbstractRequestCondition) o;
|
||||
return other.getWeight() - this.getWeight();
|
||||
return other.getSpecificity() - this.getSpecificity();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -44,7 +44,20 @@ class ConsumesRequestCondition extends AbstractRequestCondition {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWeight() {
|
||||
public int getSpecificity() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj != null && obj instanceof ConsumesRequestCondition) {
|
||||
ConsumesRequestCondition other = (ConsumesRequestCondition) obj;
|
||||
return this.mediaType.equals(other.mediaType);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.condition;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link RequestCondition} implementation that represents a logical NOT (i.e. !).
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.1
|
||||
*/
|
||||
class LogicalNegationRequestCondition extends AbstractRequestCondition {
|
||||
|
||||
private final RequestCondition requestCondition;
|
||||
|
||||
LogicalNegationRequestCondition(RequestCondition requestCondition) {
|
||||
Assert.notNull(requestCondition, "'requestCondition' must not be null");
|
||||
this.requestCondition = requestCondition;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getSpecificity() {
|
||||
if (requestCondition instanceof AbstractRequestCondition) {
|
||||
return ((AbstractRequestCondition) requestCondition).getSpecificity();
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean match(HttpServletRequest request) {
|
||||
return !requestCondition.match(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o != null && o instanceof LogicalNegationRequestCondition) {
|
||||
LogicalNegationRequestCondition other = (LogicalNegationRequestCondition) o;
|
||||
return this.requestCondition.equals(other.requestCondition);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return requestCondition.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "!(" + requestCondition.toString() + ")";
|
||||
}
|
||||
}
|
||||
@@ -38,12 +38,12 @@ abstract class RequestConditionComposite extends AbstractRequestCondition {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWeight() {
|
||||
public int getSpecificity() {
|
||||
int weight = 0;
|
||||
for (RequestCondition condition : conditions) {
|
||||
if (condition instanceof AbstractRequestCondition) {
|
||||
AbstractRequestCondition abstractRequestCondition = (AbstractRequestCondition) condition;
|
||||
weight += abstractRequestCondition.getWeight();
|
||||
weight += abstractRequestCondition.getSpecificity();
|
||||
}
|
||||
}
|
||||
return weight;
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.List;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Factory for {@link RequestCondition} objects.
|
||||
@@ -39,7 +40,7 @@ public abstract class RequestConditionFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWeight() {
|
||||
public int getSpecificity() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -55,10 +56,9 @@ public abstract class RequestConditionFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWeight() {
|
||||
public int getSpecificity() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
@@ -94,6 +94,24 @@ public abstract class RequestConditionFactory {
|
||||
return copy[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the given condition in a logical NOT, i.e. the returned condition will return {@code true} for {@link
|
||||
* RequestCondition#match(HttpServletRequest)} if the given condition return {@code false}, and vice-versa.
|
||||
*
|
||||
* @return a condition that represents a logical NOT
|
||||
*/
|
||||
public static RequestCondition not(RequestCondition condition) {
|
||||
if (condition == TRUE_CONDITION) {
|
||||
return falseCondition();
|
||||
}
|
||||
else if (condition == FALSE_CONDITION) {
|
||||
return trueCondition();
|
||||
}
|
||||
else {
|
||||
return new LogicalNegationRequestCondition(condition);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@@ -175,7 +193,11 @@ public abstract class RequestConditionFactory {
|
||||
RequestCondition[] result = new RequestCondition[headers.length];
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
HeaderRequestCondition header = new HeaderRequestCondition(headers[i]);
|
||||
if (isMediaTypeHeader(header.name)) {
|
||||
if ("Content-Type".equalsIgnoreCase(header.name) && StringUtils.hasLength(header.value)) {
|
||||
RequestCondition consumesCondition = new ConsumesRequestCondition(header.value);
|
||||
result[i] = header.isNegated ? not(consumesCondition) : consumesCondition;
|
||||
}
|
||||
else if (isMediaTypeHeader(header.name)) {
|
||||
result[i] = new MediaTypeHeaderRequestCondition(headers[i]);
|
||||
}
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user