fixed media type resolution algorithm
This commit is contained in:
parent
34a4fba335
commit
83c83d4d15
|
@ -195,16 +195,18 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H
|
|||
* @return the corresponding media type, or <code>null</code> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -130,7 +130,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
|
|||
|
||||
private boolean ignoreAcceptHeader = false;
|
||||
|
||||
private boolean useJaf = true;
|
||||
private boolean useJaf = jafPresent;
|
||||
|
||||
private ConcurrentMap<String, MediaType> mediaTypes = new ConcurrentHashMap<String, MediaType>();
|
||||
|
||||
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue