Skip to content

Commit f2d7d66

Browse files
committed
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
1 parent acd268e commit f2d7d66

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

org.springframework.web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -146,7 +146,15 @@ public void setAccept(List<MediaType> acceptableMediaTypes) {
146146
*/
147147
public List<MediaType> getAccept() {
148148
String value = getFirst(ACCEPT);
149-
return (value != null ? MediaType.parseMediaTypes(value) : Collections.<MediaType>emptyList());
149+
List<MediaType> result = (value != null) ? MediaType.parseMediaTypes(value) : Collections.<MediaType>emptyList();
150+
151+
// Some containers parse 'Accept' into multiple values
152+
if ((result.size() == 1) && (headers.get(ACCEPT).size() > 1)) {
153+
value = StringUtils.collectionToCommaDelimitedString(headers.get(ACCEPT));
154+
result = MediaType.parseMediaTypes(value);
155+
}
156+
157+
return result;
150158
}
151159

152160
/**

org.springframework.web/src/test/java/org/springframework/http/HttpHeadersTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,6 +55,16 @@ public void accept() {
5555
assertEquals("Invalid Accept header", "text/html, text/plain", headers.getFirst("Accept"));
5656
}
5757

58+
// SPR-9655
59+
60+
@Test
61+
public void acceptiPlanet() {
62+
headers.add("Accept", "text/html");
63+
headers.add("Accept", "text/plain");
64+
List<MediaType> expected = Arrays.asList(new MediaType("text", "html"), new MediaType("text", "plain"));
65+
assertEquals("Invalid Accept header", expected, headers.getAccept());
66+
}
67+
5868
@Test
5969
public void acceptCharsets() {
6070
Charset charset1 = Charset.forName("UTF-8");

0 commit comments

Comments
 (0)