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
|
* @return the corresponding media type, or <code>null</code> if none found
|
||||||
*/
|
*/
|
||||||
protected MediaType getMediaType(Resource resource) {
|
protected MediaType getMediaType(Resource resource) {
|
||||||
|
MediaType mediaType = null;
|
||||||
String mimeType = getServletContext().getMimeType(resource.getFilename());
|
String mimeType = getServletContext().getMimeType(resource.getFilename());
|
||||||
if (StringUtils.hasText(mimeType)) {
|
if (StringUtils.hasText(mimeType)) {
|
||||||
return new MediaType(mimeType);
|
mediaType = MediaType.parseMediaType(mimeType);
|
||||||
}
|
}
|
||||||
else if (jafPresent) {
|
if (jafPresent && (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType))) {
|
||||||
return ActivationMediaTypeFactory.getMediaType(resource.getFilename());
|
MediaType jafMediaType = ActivationMediaTypeFactory.getMediaType(resource.getFilename());
|
||||||
}
|
if (jafMediaType != null && !MediaType.APPLICATION_OCTET_STREAM.equals(jafMediaType)) {
|
||||||
else {
|
mediaType = jafMediaType;
|
||||||
return null;
|
}
|
||||||
}
|
}
|
||||||
|
return mediaType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -130,7 +130,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
|
||||||
|
|
||||||
private boolean ignoreAcceptHeader = false;
|
private boolean ignoreAcceptHeader = false;
|
||||||
|
|
||||||
private boolean useJaf = true;
|
private boolean useJaf = jafPresent;
|
||||||
|
|
||||||
private ConcurrentMap<String, MediaType> mediaTypes = new ConcurrentHashMap<String, MediaType>();
|
private ConcurrentMap<String, MediaType> mediaTypes = new ConcurrentHashMap<String, MediaType>();
|
||||||
|
|
||||||
|
@ -421,10 +421,13 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
|
||||||
if (mediaType == null) {
|
if (mediaType == null) {
|
||||||
String mimeType = getServletContext().getMimeType(filename);
|
String mimeType = getServletContext().getMimeType(filename);
|
||||||
if (StringUtils.hasText(mimeType)) {
|
if (StringUtils.hasText(mimeType)) {
|
||||||
mediaType = new MediaType(mimeType);
|
mediaType = MediaType.parseMediaType(mimeType);
|
||||||
}
|
}
|
||||||
else if (this.useJaf && jafPresent) {
|
if (this.useJaf && (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType))) {
|
||||||
mediaType = ActivationMediaTypeFactory.getMediaType(filename);
|
MediaType jafMediaType = ActivationMediaTypeFactory.getMediaType(filename);
|
||||||
|
if (jafMediaType != null && !MediaType.APPLICATION_OCTET_STREAM.equals(jafMediaType)) {
|
||||||
|
mediaType = jafMediaType;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (mediaType != null) {
|
if (mediaType != null) {
|
||||||
this.mediaTypes.putIfAbsent(extension, mediaType);
|
this.mediaTypes.putIfAbsent(extension, mediaType);
|
||||||
|
|
|
@ -16,16 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.web.servlet.view;
|
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.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -38,6 +28,7 @@ import java.util.Set;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.mock.web.MockHttpServletRequest;
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
import org.springframework.mock.web.MockHttpServletResponse;
|
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.View;
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
|
|
||||||
|
import static org.easymock.EasyMock.*;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
*/
|
*/
|
||||||
|
@ -60,7 +54,11 @@ public class ContentNegotiatingViewResolverTests {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void createViewResolver() {
|
public void createViewResolver() {
|
||||||
|
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||||
|
wac.setServletContext(new MockServletContext());
|
||||||
|
wac.refresh();
|
||||||
viewResolver = new ContentNegotiatingViewResolver();
|
viewResolver = new ContentNegotiatingViewResolver();
|
||||||
|
viewResolver.setApplicationContext(wac);
|
||||||
request = new MockHttpServletRequest("GET", "/test");
|
request = new MockHttpServletRequest("GET", "/test");
|
||||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||||
}
|
}
|
||||||
|
@ -79,14 +77,15 @@ public class ContentNegotiatingViewResolverTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getMediaTypeFromFilenameJaf() {
|
public void getMediaTypeFromFilenameJaf() {
|
||||||
assertEquals("Invalid content type", new MediaType("text", "html"),
|
assertEquals("Invalid content type", new MediaType("application", "vnd.ms-excel"),
|
||||||
viewResolver.getMediaTypeFromFilename("test.html"));
|
viewResolver.getMediaTypeFromFilename("test.xls"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getMediaTypeFromFilenameNoJaf() {
|
public void getMediaTypeFromFilenameNoJaf() {
|
||||||
viewResolver.setUseJaf(false);
|
viewResolver.setUseJaf(false);
|
||||||
assertNull("Invalid content type", viewResolver.getMediaTypeFromFilename("test.html"));
|
assertEquals("Invalid content type", MediaType.APPLICATION_OCTET_STREAM,
|
||||||
|
viewResolver.getMediaTypeFromFilename("test.xls"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue