Skip to content

Commit

Permalink
Use null in MockServletContext for unknown mime types
Browse files Browse the repository at this point in the history
MockServletContext.getMimeTypes now returns null if the Java Activation
Framework returns "application/octet-stream", which is the default
media type it returns if the mime type is unknown. This enforces the
contract for ServletContext.getMimeTypes (return null for uknown mime
types) but does mean "application/octet-stream" cannot be returned.

Issue: SPR-10334
  • Loading branch information
rstoyanchev committed Mar 7, 2013
1 parent eefd1c4 commit 8e4e0f3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,16 @@ public int getEffectiveMinorVersion() {
return this.effectiveMinorVersion;
}

/**
* This method uses the Java Activation framework, which returns
* "application/octet-stream" when the mime type is unknown (i.e. it never returns
* {@code null}). In order to maintain the {@link ServletContext#getMimeType(String)
* contract, as of version 3.2.2, this method returns null if the mimeType is
* "application/octet-stream".
*/
public String getMimeType(String filePath) {
return MimeTypeResolver.getMimeType(filePath);
String mimeType = MimeTypeResolver.getMimeType(filePath);
return ("application/octet-stream".equals(mimeType)) ? null : mimeType;
}

public Set<String> getResourcePaths(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
Expand Down Expand Up @@ -258,9 +257,17 @@ public int getEffectiveMinorVersion() {
return this.effectiveMinorVersion;
}

/**
* This method uses the Java Activation framework, which returns
* "application/octet-stream" when the mime type is unknown (i.e. it never returns
* {@code null}). In order to maintain the {@link ServletContext#getMimeType(String)
* contract, as of version 3.2.2, this method returns null if the mimeType is
* "application/octet-stream".
*/
@Override
public String getMimeType(String filePath) {
return MimeTypeResolver.getMimeType(filePath);
String mimeType = MimeTypeResolver.getMimeType(filePath);
return ("application/octet-stream".equals(mimeType)) ? null : mimeType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ public void resolveMediaTypesFromJaf() {
assertEquals(Arrays.asList(new MediaType("application", "vnd.ms-excel")), mediaTypes);
}

// SPR-10334

@Test
public void getMediaTypeFromFilenameNoJaf() {

this.servletRequest.setRequestURI("test.xls");
this.servletRequest.setRequestURI("test.json");

ServletContext servletContext = this.servletRequest.getServletContext();
PathExtensionContentNegotiationStrategy strategy =
Expand All @@ -86,7 +88,7 @@ public void getMediaTypeFromFilenameNoJaf() {

List<MediaType> mediaTypes = strategy.resolveMediaTypes(this.webRequest);

assertEquals(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM), mediaTypes);
assertEquals(Collections.emptyList(), mediaTypes);
}

// SPR-8678
Expand Down

0 comments on commit 8e4e0f3

Please sign in to comment.