diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java
index e8c2bc99b7..a66aaf0cde 100644
--- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java
+++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java
@@ -195,16 +195,18 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H
* @return the corresponding media type, or null
if none found
*/
protected MediaType getMediaType(Resource resource) {
+ MediaType mediaType = null;
String mimeType = getServletContext().getMimeType(resource.getFilename());
if (StringUtils.hasText(mimeType)) {
- return new MediaType(mimeType);
+ mediaType = MediaType.parseMediaType(mimeType);
}
- else if (jafPresent) {
- return ActivationMediaTypeFactory.getMediaType(resource.getFilename());
- }
- else {
- return null;
+ if (jafPresent && (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType))) {
+ MediaType jafMediaType = ActivationMediaTypeFactory.getMediaType(resource.getFilename());
+ if (jafMediaType != null && !MediaType.APPLICATION_OCTET_STREAM.equals(jafMediaType)) {
+ mediaType = jafMediaType;
+ }
}
+ return mediaType;
}
/**
diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java
index e980f444a3..e304511d26 100644
--- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java
+++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java
@@ -130,7 +130,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
private boolean ignoreAcceptHeader = false;
- private boolean useJaf = true;
+ private boolean useJaf = jafPresent;
private ConcurrentMap mediaTypes = new ConcurrentHashMap();
@@ -421,10 +421,13 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
if (mediaType == null) {
String mimeType = getServletContext().getMimeType(filename);
if (StringUtils.hasText(mimeType)) {
- mediaType = new MediaType(mimeType);
+ mediaType = MediaType.parseMediaType(mimeType);
}
- else if (this.useJaf && jafPresent) {
- mediaType = ActivationMediaTypeFactory.getMediaType(filename);
+ if (this.useJaf && (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType))) {
+ MediaType jafMediaType = ActivationMediaTypeFactory.getMediaType(filename);
+ if (jafMediaType != null && !MediaType.APPLICATION_OCTET_STREAM.equals(jafMediaType)) {
+ mediaType = jafMediaType;
+ }
}
if (mediaType != null) {
this.mediaTypes.putIfAbsent(extension, mediaType);
diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java
index d2e431d52a..5c3240d8fa 100644
--- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java
+++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java
@@ -16,16 +16,6 @@
package org.springframework.web.servlet.view;
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.verify;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -38,6 +28,7 @@ import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -49,6 +40,9 @@ import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
+
/**
* @author Arjen Poutsma
*/
@@ -60,7 +54,11 @@ public class ContentNegotiatingViewResolverTests {
@Before
public void createViewResolver() {
+ StaticWebApplicationContext wac = new StaticWebApplicationContext();
+ wac.setServletContext(new MockServletContext());
+ wac.refresh();
viewResolver = new ContentNegotiatingViewResolver();
+ viewResolver.setApplicationContext(wac);
request = new MockHttpServletRequest("GET", "/test");
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
}
@@ -79,14 +77,15 @@ public class ContentNegotiatingViewResolverTests {
@Test
public void getMediaTypeFromFilenameJaf() {
- assertEquals("Invalid content type", new MediaType("text", "html"),
- viewResolver.getMediaTypeFromFilename("test.html"));
+ assertEquals("Invalid content type", new MediaType("application", "vnd.ms-excel"),
+ viewResolver.getMediaTypeFromFilename("test.xls"));
}
@Test
public void getMediaTypeFromFilenameNoJaf() {
viewResolver.setUseJaf(false);
- assertNull("Invalid content type", viewResolver.getMediaTypeFromFilename("test.html"));
+ assertEquals("Invalid content type", MediaType.APPLICATION_OCTET_STREAM,
+ viewResolver.getMediaTypeFromFilename("test.xls"));
}
@Test