From f2d7d663b4f75f27c71928ea8cf8923380aee244 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 22 Oct 2012 16:17:27 -0400 Subject: [PATCH] Update HttpHeaders.getAccept method Some servlet containers (iPlanet) parse the Accept header and return multiple values from request.getHeader("Accept"). The HttpHeaders getAccept method has been updated to accommodate that hopefully without causing any other issues. The extra functionality is in effect only if we find only one MediaType and there is more than one value for the 'Accept' header. Issue: SPR-9655 --- .../java/org/springframework/http/HttpHeaders.java | 12 ++++++++++-- .../org/springframework/http/HttpHeadersTests.java | 12 +++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/org.springframework.web/src/main/java/org/springframework/http/HttpHeaders.java b/org.springframework.web/src/main/java/org/springframework/http/HttpHeaders.java index 2bc17bb2c7c..6db94ef2476 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/org.springframework.web/src/main/java/org/springframework/http/HttpHeaders.java @@ -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. @@ -146,7 +146,15 @@ public class HttpHeaders implements MultiValueMap { */ public List getAccept() { String value = getFirst(ACCEPT); - return (value != null ? MediaType.parseMediaTypes(value) : Collections.emptyList()); + List result = (value != null) ? MediaType.parseMediaTypes(value) : Collections.emptyList(); + + // Some containers parse 'Accept' into multiple values + if ((result.size() == 1) && (headers.get(ACCEPT).size() > 1)) { + value = StringUtils.collectionToCommaDelimitedString(headers.get(ACCEPT)); + result = MediaType.parseMediaTypes(value); + } + + return result; } /** diff --git a/org.springframework.web/src/test/java/org/springframework/http/HttpHeadersTests.java b/org.springframework.web/src/test/java/org/springframework/http/HttpHeadersTests.java index 1b82eeb8d6a..129cd89f1ca 100644 --- a/org.springframework.web/src/test/java/org/springframework/http/HttpHeadersTests.java +++ b/org.springframework.web/src/test/java/org/springframework/http/HttpHeadersTests.java @@ -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. @@ -55,6 +55,16 @@ public class HttpHeadersTests { assertEquals("Invalid Accept header", "text/html, text/plain", headers.getFirst("Accept")); } + // SPR-9655 + + @Test + public void acceptiPlanet() { + headers.add("Accept", "text/html"); + headers.add("Accept", "text/plain"); + List expected = Arrays.asList(new MediaType("text", "html"), new MediaType("text", "plain")); + assertEquals("Invalid Accept header", expected, headers.getAccept()); + } + @Test public void acceptCharsets() { Charset charset1 = Charset.forName("UTF-8");