Skip to content

Commit 83c83d4

Browse files
committed
fixed media type resolution algorithm
1 parent 34a4fba commit 83c83d4

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,18 @@ protected boolean isInvalidPath(String path) {
195195
* @return the corresponding media type, or <code>null</code> if none found
196196
*/
197197
protected MediaType getMediaType(Resource resource) {
198+
MediaType mediaType = null;
198199
String mimeType = getServletContext().getMimeType(resource.getFilename());
199200
if (StringUtils.hasText(mimeType)) {
200-
return new MediaType(mimeType);
201+
mediaType = MediaType.parseMediaType(mimeType);
201202
}
202-
else if (jafPresent) {
203-
return ActivationMediaTypeFactory.getMediaType(resource.getFilename());
204-
}
205-
else {
206-
return null;
203+
if (jafPresent && (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType))) {
204+
MediaType jafMediaType = ActivationMediaTypeFactory.getMediaType(resource.getFilename());
205+
if (jafMediaType != null && !MediaType.APPLICATION_OCTET_STREAM.equals(jafMediaType)) {
206+
mediaType = jafMediaType;
207+
}
207208
}
209+
return mediaType;
208210
}
209211

210212
/**

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
130130

131131
private boolean ignoreAcceptHeader = false;
132132

133-
private boolean useJaf = true;
133+
private boolean useJaf = jafPresent;
134134

135135
private ConcurrentMap<String, MediaType> mediaTypes = new ConcurrentHashMap<String, MediaType>();
136136

@@ -421,10 +421,13 @@ protected MediaType getMediaTypeFromFilename(String filename) {
421421
if (mediaType == null) {
422422
String mimeType = getServletContext().getMimeType(filename);
423423
if (StringUtils.hasText(mimeType)) {
424-
mediaType = new MediaType(mimeType);
424+
mediaType = MediaType.parseMediaType(mimeType);
425425
}
426-
else if (this.useJaf && jafPresent) {
427-
mediaType = ActivationMediaTypeFactory.getMediaType(filename);
426+
if (this.useJaf && (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType))) {
427+
MediaType jafMediaType = ActivationMediaTypeFactory.getMediaType(filename);
428+
if (jafMediaType != null && !MediaType.APPLICATION_OCTET_STREAM.equals(jafMediaType)) {
429+
mediaType = jafMediaType;
430+
}
428431
}
429432
if (mediaType != null) {
430433
this.mediaTypes.putIfAbsent(extension, mediaType);

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@
1616

1717
package org.springframework.web.servlet.view;
1818

19-
import static org.easymock.EasyMock.createMock;
20-
import static org.easymock.EasyMock.expect;
21-
import static org.easymock.EasyMock.replay;
22-
import static org.easymock.EasyMock.verify;
23-
import static org.junit.Assert.assertEquals;
24-
import static org.junit.Assert.assertNotNull;
25-
import static org.junit.Assert.assertNull;
26-
import static org.junit.Assert.assertSame;
27-
import static org.junit.Assert.assertTrue;
28-
2919
import java.util.ArrayList;
3020
import java.util.Arrays;
3121
import java.util.Collections;
@@ -38,6 +28,7 @@
3828
import org.junit.After;
3929
import org.junit.Before;
4030
import org.junit.Test;
31+
4132
import org.springframework.http.MediaType;
4233
import org.springframework.mock.web.MockHttpServletRequest;
4334
import org.springframework.mock.web.MockHttpServletResponse;
@@ -49,6 +40,9 @@
4940
import org.springframework.web.servlet.View;
5041
import org.springframework.web.servlet.ViewResolver;
5142

43+
import static org.easymock.EasyMock.*;
44+
import static org.junit.Assert.*;
45+
5246
/**
5347
* @author Arjen Poutsma
5448
*/
@@ -60,7 +54,11 @@ public class ContentNegotiatingViewResolverTests {
6054

6155
@Before
6256
public void createViewResolver() {
57+
StaticWebApplicationContext wac = new StaticWebApplicationContext();
58+
wac.setServletContext(new MockServletContext());
59+
wac.refresh();
6360
viewResolver = new ContentNegotiatingViewResolver();
61+
viewResolver.setApplicationContext(wac);
6462
request = new MockHttpServletRequest("GET", "/test");
6563
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
6664
}
@@ -79,14 +77,15 @@ public void getMediaTypeFromFilenameMediaTypes() {
7977

8078
@Test
8179
public void getMediaTypeFromFilenameJaf() {
82-
assertEquals("Invalid content type", new MediaType("text", "html"),
83-
viewResolver.getMediaTypeFromFilename("test.html"));
80+
assertEquals("Invalid content type", new MediaType("application", "vnd.ms-excel"),
81+
viewResolver.getMediaTypeFromFilename("test.xls"));
8482
}
8583

8684
@Test
8785
public void getMediaTypeFromFilenameNoJaf() {
8886
viewResolver.setUseJaf(false);
89-
assertNull("Invalid content type", viewResolver.getMediaTypeFromFilename("test.html"));
87+
assertEquals("Invalid content type", MediaType.APPLICATION_OCTET_STREAM,
88+
viewResolver.getMediaTypeFromFilename("test.xls"));
9089
}
9190

9291
@Test

0 commit comments

Comments
 (0)