Fix MediaType lookup for ResourceHttpRequestHandler

As of 4.3 ResourceHttpRequestHandler delegates to the configured
ContentNegotiationManager, or one created internally, to look up
the media type for are resource.

This commit ensures the internally created ContentNegotiationManager is
correctly injected with the ServletContext through which it can perform
lookups as before.

Also the ServletPathContentNegotiationStrategy now checks the
ServletContext first and then delegates to its parent the
PathContentNegotiationStrategy and not vice versa. This is
consistent with how handleNoMatch (also in the same class) works
and also matches how ResourceHttpRequestHandler worked before 4.3.

Issue: SPR-14368
This commit is contained in:
Rossen Stoyanchev
2016-06-27 14:56:55 -04:00
parent a30ab30e4e
commit e38623df87
3 changed files with 45 additions and 17 deletions

View File

@@ -20,6 +20,7 @@ import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
@@ -110,6 +111,8 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
private ContentNegotiationManager contentNegotiationManager;
private final ContentNegotiationManagerFactoryBean cnmFactoryBean = new ContentNegotiationManagerFactoryBean();
private CorsConfiguration corsConfiguration;
@@ -249,6 +252,11 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
return this.corsConfiguration;
}
@Override
protected void initServletContext(ServletContext servletContext) {
this.cnmFactoryBean.setServletContext(servletContext);
}
@Override
public void afterPropertiesSet() throws Exception {
@@ -261,7 +269,8 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
}
initAllowedLocations();
if (this.contentNegotiationManager == null) {
this.contentNegotiationManager = initContentNegotiationManager();
this.cnmFactoryBean.afterPropertiesSet();
this.contentNegotiationManager = this.cnmFactoryBean.getObject();
}
if (this.resourceHttpMessageConverter == null) {
this.resourceHttpMessageConverter = new ResourceHttpMessageConverter();
@@ -291,18 +300,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
}
}
/**
* Create the {@code ContentNegotiationManager} to use to resolve the
* {@link MediaType} for requests. This implementation delegates to
* {@link ContentNegotiationManagerFactoryBean} with default settings.
*/
protected ContentNegotiationManager initContentNegotiationManager() {
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
factory.afterPropertiesSet();
return factory.getObject();
}
/**
* Processes a resource request.
* <p>Checks for the existence of the requested resource in the configured list of locations.