SPR-7354 - Added equivalent of JAX-RS @Consumes to Spring MVC
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4198 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
a37c85f4fc
commit
ca89d6e123
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* 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.
|
||||
|
|
@ -16,13 +16,6 @@
|
|||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.beans.PropertyEditorSupport;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
|
|
@ -47,7 +40,6 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
|
|
@ -61,6 +53,7 @@ import javax.xml.bind.annotation.XmlRootElement;
|
|||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
||||
import org.springframework.aop.interceptor.SimpleTraceInterceptor;
|
||||
import org.springframework.aop.support.DefaultPointcutAdvisor;
|
||||
|
|
@ -140,6 +133,8 @@ import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolv
|
|||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.util.NestedServletException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* The origin of this test fixture is {@link ServletHandlerMethodTests}. The tests were adapted to run against
|
||||
* the HandlerMethod infrastructure rather than against the DefaultAnnotationHandlerMapping, the
|
||||
|
|
@ -1000,19 +995,19 @@ public class ServletHandlerMethodTests {
|
|||
initDispatcherServlet(ContentTypeHeadersController.class, null);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/something");
|
||||
request.addHeader("Content-Type", "application/pdf");
|
||||
request.setContentType("application/pdf");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("pdf", response.getContentAsString());
|
||||
|
||||
request = new MockHttpServletRequest("POST", "/something");
|
||||
request.addHeader("Content-Type", "text/html");
|
||||
request.setContentType("text/html");
|
||||
response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("text", response.getContentAsString());
|
||||
|
||||
request = new MockHttpServletRequest("POST", "/something");
|
||||
request.addHeader("Content-Type", "application/xml");
|
||||
request.setContentType("application/xml");
|
||||
response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals(404, response.getStatus());
|
||||
|
|
@ -1023,13 +1018,13 @@ public class ServletHandlerMethodTests {
|
|||
initDispatcherServlet(NegatedContentTypeHeadersController.class, null);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/something");
|
||||
request.addHeader("Content-Type", "application/pdf");
|
||||
request.setContentType("application/pdf");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("pdf", response.getContentAsString());
|
||||
|
||||
request = new MockHttpServletRequest("POST", "/something");
|
||||
request.addHeader("Content-Type", "text/html");
|
||||
request.setContentType("text/html");
|
||||
response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("non-pdf", response.getContentAsString());
|
||||
|
|
|
|||
Loading…
Reference in New Issue