Fix issue with parsing media types
Invalid Content-Type or Accept header values previously resulted in the IllegalArgumentException getting propagated. After this change such errors are detected and generally treated as a "no match", which may for example result in a 406 in the case of the Accept header. Issue: SPR-9148
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2012 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.
|
||||
@@ -18,19 +18,23 @@ package org.springframework.web.servlet.mvc.condition;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
/**
|
||||
* Supports media type expressions as described in:
|
||||
* {@link RequestMapping#consumes()} and {@link RequestMapping#produces()}.
|
||||
*
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
*/
|
||||
abstract class AbstractMediaTypeExpression implements Comparable<AbstractMediaTypeExpression>, MediaTypeExpression {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private final MediaType mediaType;
|
||||
|
||||
private final boolean isNegated;
|
||||
@@ -60,8 +64,16 @@ abstract class AbstractMediaTypeExpression implements Comparable<AbstractMediaTy
|
||||
}
|
||||
|
||||
public final boolean match(HttpServletRequest request) {
|
||||
boolean match = matchMediaType(request);
|
||||
return !isNegated ? match : !match;
|
||||
try {
|
||||
boolean match = matchMediaType(request);
|
||||
return !isNegated ? match : !match;
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Could not parse media type header: " + ex.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract boolean matchMediaType(HttpServletRequest request);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2012 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.
|
||||
@@ -41,7 +41,7 @@ import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
|
||||
/**
|
||||
* Extends {@link AbstractMessageConverterMethodArgumentResolver} with the ability to handle method return
|
||||
* Extends {@link AbstractMessageConverterMethodArgumentResolver} with the ability to handle method return
|
||||
* values by writing to the response with {@link HttpMessageConverter}s.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
@@ -103,7 +103,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
|
||||
|
||||
List<MediaType> acceptableMediaTypes = getAcceptableMediaTypes(inputMessage);
|
||||
List<MediaType> producibleMediaTypes = getProducibleMediaTypes(inputMessage.getServletRequest(), returnValueClass);
|
||||
|
||||
|
||||
Set<MediaType> compatibleMediaTypes = new LinkedHashSet<MediaType>();
|
||||
for (MediaType a : acceptableMediaTypes) {
|
||||
for (MediaType p : producibleMediaTypes) {
|
||||
@@ -115,10 +115,10 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
|
||||
if (compatibleMediaTypes.isEmpty()) {
|
||||
throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
|
||||
}
|
||||
|
||||
|
||||
List<MediaType> mediaTypes = new ArrayList<MediaType>(compatibleMediaTypes);
|
||||
MediaType.sortBySpecificity(mediaTypes);
|
||||
|
||||
|
||||
MediaType selectedMediaType = null;
|
||||
for (MediaType mediaType : mediaTypes) {
|
||||
if (mediaType.isConcrete()) {
|
||||
@@ -130,7 +130,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (selectedMediaType != null) {
|
||||
for (HttpMessageConverter<?> messageConverter : messageConverters) {
|
||||
if (messageConverter.canWrite(returnValueClass, selectedMediaType)) {
|
||||
@@ -166,7 +166,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
|
||||
if (converter.canWrite(returnValueClass, null)) {
|
||||
result.addAll(converter.getSupportedMediaTypes());
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
@@ -175,8 +175,16 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
|
||||
}
|
||||
|
||||
private List<MediaType> getAcceptableMediaTypes(HttpInputMessage inputMessage) {
|
||||
List<MediaType> result = inputMessage.getHeaders().getAccept();
|
||||
return result.isEmpty() ? Collections.singletonList(MediaType.ALL) : result;
|
||||
try {
|
||||
List<MediaType> result = inputMessage.getHeaders().getAccept();
|
||||
return result.isEmpty() ? Collections.singletonList(MediaType.ALL) : result;
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Could not parse Accept header: " + ex.getMessage());
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user