DATAREST-863 - Adapt to changes in Accept header lookup in Spring 4.3.

Spring 4.3's HeaderContentNegotiationStrategy switched from looking up the Accept header via the method returning a single (potentially comma separated) one to the method returning multiple headers in the first place. We now added an override of HttpServletRequestWrapper.getHeaders(…) to out custom adapter defaulting the media type to make sure the defaulting is visible and thus the right handler methods are looked up.

That previously missing caused the DelegatingHandlerMapping selecting the redirect to the HAL browser in case a request to the API root was sent without an accept header as the defaulting of the header to application/hal+json wasn't properly exposed anymore and the redirect to the browser — declaring a produces clause of text/html — was not causing any media type mismatch anymore.
This commit is contained in:
Oliver Gierke
2016-08-01 21:15:00 -07:00
parent 684e0f9aaf
commit 344c3ac840
3 changed files with 86 additions and 1 deletions

View File

@@ -23,6 +23,7 @@ import java.net.URI;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
@@ -563,6 +564,7 @@ public class BasePathAwareHandlerMapping extends RequestMappingHandlerMapping {
static class CustomAcceptHeaderHttpServletRequest extends HttpServletRequestWrapper {
private final List<MediaType> acceptMediaTypes;
private final List<String> acceptMediaTypeStrings;
/**
* Creates a new {@link CustomAcceptHeaderHttpServletRequest} for the given delegate {@link HttpServletRequest} and
@@ -578,6 +580,14 @@ public class BasePathAwareHandlerMapping extends RequestMappingHandlerMapping {
Assert.notEmpty(acceptMediaTypes, "MediaTypes must not be empty!");
this.acceptMediaTypes = acceptMediaTypes;
List<String> acceptMediaTypeStrings = new ArrayList<String>(acceptMediaTypes.size());
for (MediaType mediaType : acceptMediaTypes) {
acceptMediaTypeStrings.add(mediaType.toString());
}
this.acceptMediaTypeStrings = acceptMediaTypeStrings;
}
/*
@@ -593,5 +603,19 @@ public class BasePathAwareHandlerMapping extends RequestMappingHandlerMapping {
return super.getHeader(name);
}
/*
* (non-Javadoc)
* @see javax.servlet.http.HttpServletRequestWrapper#getHeaders(java.lang.String)
*/
@Override
public Enumeration<String> getHeaders(String name) {
if (HttpHeaders.ACCEPT.equalsIgnoreCase(name) && acceptMediaTypes != null) {
return Collections.enumeration(acceptMediaTypeStrings);
}
return super.getHeaders(name);
}
}
}