How to register MIME types in MockServletContext
Prior to this commit, it was unclear that it was possible to register custom MIME types when using MockServletContext. This commit updates the Javadoc for MockServletContext.getMimeType() with an example of how to achieve this using the MimetypesFileTypeMap from the Java Activation Framework. Issue: SPR-12126
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -45,6 +45,7 @@ import javax.servlet.descriptor.JspConfigDescriptor;
|
|||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
@@ -152,8 +153,8 @@ public class MockServletContext implements ServletContext {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new MockServletContext, using no base path and a
|
* Create a new {@code MockServletContext}, using no base path and a
|
||||||
* DefaultResourceLoader (i.e. the classpath root as WAR root).
|
* {@link DefaultResourceLoader} (i.e. the classpath root as WAR root).
|
||||||
* @see org.springframework.core.io.DefaultResourceLoader
|
* @see org.springframework.core.io.DefaultResourceLoader
|
||||||
*/
|
*/
|
||||||
public MockServletContext() {
|
public MockServletContext() {
|
||||||
@@ -161,7 +162,7 @@ public class MockServletContext implements ServletContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new MockServletContext, using a DefaultResourceLoader.
|
* Create a new {@code MockServletContext}, using a {@link DefaultResourceLoader}.
|
||||||
* @param resourceBasePath the root directory of the WAR (should not end with a slash)
|
* @param resourceBasePath the root directory of the WAR (should not end with a slash)
|
||||||
* @see org.springframework.core.io.DefaultResourceLoader
|
* @see org.springframework.core.io.DefaultResourceLoader
|
||||||
*/
|
*/
|
||||||
@@ -170,7 +171,7 @@ public class MockServletContext implements ServletContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new MockServletContext, using the specified ResourceLoader
|
* Create a new {@code MockServletContext}, using the specified {@link ResourceLoader}
|
||||||
* and no base path.
|
* and no base path.
|
||||||
* @param resourceLoader the ResourceLoader to use (or null for the default)
|
* @param resourceLoader the ResourceLoader to use (or null for the default)
|
||||||
*/
|
*/
|
||||||
@@ -179,8 +180,8 @@ public class MockServletContext implements ServletContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new MockServletContext using the supplied resource base path and
|
* Create a new {@code MockServletContext} using the supplied resource base
|
||||||
* resource loader.
|
* path and resource loader.
|
||||||
* <p>Registers a {@link MockRequestDispatcher} for the Servlet named
|
* <p>Registers a {@link MockRequestDispatcher} for the Servlet named
|
||||||
* {@literal 'default'}.
|
* {@literal 'default'}.
|
||||||
* @param resourceBasePath the root directory of the WAR (should not end with a slash)
|
* @param resourceBasePath the root directory of the WAR (should not end with a slash)
|
||||||
@@ -201,8 +202,8 @@ public class MockServletContext implements ServletContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build a full resource location for the given path,
|
* Build a full resource location for the given path, prepending the resource
|
||||||
* prepending the resource base path of this MockServletContext.
|
* base path of this {@code MockServletContext}.
|
||||||
* @param path the path as specified
|
* @param path the path as specified
|
||||||
* @return the full resource path
|
* @return the full resource path
|
||||||
*/
|
*/
|
||||||
@@ -271,10 +272,23 @@ public class MockServletContext implements ServletContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method uses the Java Activation framework, which returns "application/octet-stream"
|
* This method uses the default
|
||||||
* when the mime type is unknown (i.e. it never returns {@code null}). In order to maintain
|
* {@link javax.activation.FileTypeMap#getDefaultFileTypeMap() FileTypeMap}
|
||||||
* the {@link ServletContext#getMimeType(String)} contract, this method returns {@code null}
|
* from the Java Activation Framework to resolve MIME types.
|
||||||
* if the mimeType is "application/octet-stream", as of Spring 3.2.2.
|
* <p>The Java Activation Framework returns {@code "application/octet-stream"}
|
||||||
|
* if the MIME type is unknown (i.e., it never returns {@code null}). Thus, in
|
||||||
|
* order to honor the {@link ServletContext#getMimeType(String)} contract,
|
||||||
|
* this method returns {@code null} if the MIME type is
|
||||||
|
* {@code "application/octet-stream"}.
|
||||||
|
* <p>{@code MockServletContext} does not provide a direct mechanism for
|
||||||
|
* setting a custom MIME type; however, if the default {@code FileTypeMap}
|
||||||
|
* is an instance of {@code javax.activation.MimetypesFileTypeMap}, a custom
|
||||||
|
* MIME type named {@code text/enigma} can be registered for a custom
|
||||||
|
* {@code .puzzle} file extension in the following manner:
|
||||||
|
* <pre style="code">
|
||||||
|
* MimetypesFileTypeMap mimetypesFileTypeMap = (MimetypesFileTypeMap) FileTypeMap.getDefaultFileTypeMap();
|
||||||
|
* mimetypesFileTypeMap.addMimeTypes("text/enigma puzzle");
|
||||||
|
* </pre>
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String getMimeType(String filePath) {
|
public String getMimeType(String filePath) {
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ public class MockServletContextTests {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void getMimeTypeWithCustomConfiguredType() {
|
public void getMimeTypeWithCustomConfiguredType() {
|
||||||
FileTypeMap defaultFileTypeMap = MimetypesFileTypeMap.getDefaultFileTypeMap();
|
FileTypeMap defaultFileTypeMap = FileTypeMap.getDefaultFileTypeMap();
|
||||||
assertThat(defaultFileTypeMap, instanceOf(MimetypesFileTypeMap.class));
|
assertThat(defaultFileTypeMap, instanceOf(MimetypesFileTypeMap.class));
|
||||||
MimetypesFileTypeMap mimetypesFileTypeMap = (MimetypesFileTypeMap) defaultFileTypeMap;
|
MimetypesFileTypeMap mimetypesFileTypeMap = (MimetypesFileTypeMap) defaultFileTypeMap;
|
||||||
mimetypesFileTypeMap.addMimeTypes("text/enigma enigma");
|
mimetypesFileTypeMap.addMimeTypes("text/enigma enigma");
|
||||||
|
|||||||
Reference in New Issue
Block a user