From c611083415845bcb9758c0f92c4749a712b049f0 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 1 Mar 2013 15:16:08 -0500 Subject: [PATCH] Catch IAE when parsing content type Issue: SPR-10308 --- .../method/RequestMappingInfoHandlerMapping.java | 7 ++++++- .../RequestMappingInfoHandlerMappingTests.java | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java index cc242bf756..89fcb4e9e1 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java @@ -205,7 +205,12 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe if (!consumableMediaTypes.isEmpty()) { MediaType contentType = null; if (StringUtils.hasLength(request.getContentType())) { - contentType = MediaType.parseMediaType(request.getContentType()); + try { + contentType = MediaType.parseMediaType(request.getContentType()); + } + catch (IllegalArgumentException ex) { + throw new HttpMediaTypeNotSupportedException(ex.getMessage()); + } } throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList(consumableMediaTypes)); } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java index 0998bf59f0..099197c07d 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java @@ -180,6 +180,19 @@ public class RequestMappingInfoHandlerMappingTests { } } + @Test + public void testMediaTypeNotValue() throws Exception { + try { + MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/person/1"); + request.setContentType("bogus"); + this.handlerMapping.getHandler(request); + fail("HttpMediaTypeNotSupportedException expected"); + } + catch (HttpMediaTypeNotSupportedException ex) { + assertEquals("Invalid media type \"bogus\": does not contain '/'", ex.getMessage()); + } + } + @Test public void mediaTypeNotAccepted() throws Exception { testMediaTypeNotAccepted("/persons");