From 0cae0e5e705dd3a8a258e91a14833d9f55eadcee Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 4 Aug 2016 21:13:28 -0700 Subject: [PATCH] DATAREST-863 - Adapt to changes in Accept header lookup in Spring 4.3. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../HalBrowserIntegrationTests.java | 2 +- .../webmvc/BasePathAwareHandlerMapping.java | 24 ++++++++ ...ceptHeaderHttpServletRequestUnitTests.java | 61 +++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CustomAcceptHeaderHttpServletRequestUnitTests.java diff --git a/spring-data-rest-hal-browser/src/test/java/org/springframework/data/rest/webmvc/halbrowser/HalBrowserIntegrationTests.java b/spring-data-rest-hal-browser/src/test/java/org/springframework/data/rest/webmvc/halbrowser/HalBrowserIntegrationTests.java index bb5001e62..79a46dda8 100644 --- a/spring-data-rest-hal-browser/src/test/java/org/springframework/data/rest/webmvc/halbrowser/HalBrowserIntegrationTests.java +++ b/spring-data-rest-hal-browser/src/test/java/org/springframework/data/rest/webmvc/halbrowser/HalBrowserIntegrationTests.java @@ -80,7 +80,7 @@ public class HalBrowserIntegrationTests { mvc.perform(get(BASE_PATH).accept(MediaType.ALL)).// andExpect(status().isOk()).// - andExpect(header().string(HttpHeaders.CONTENT_TYPE, is(MediaTypes.HAL_JSON.toString()))); + andExpect(header().string(HttpHeaders.CONTENT_TYPE, startsWith(MediaTypes.HAL_JSON.toString()))); } /** diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java index 8dfee41e2..c80aa61cb 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java @@ -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 acceptMediaTypes; + private final List 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 acceptMediaTypeStrings = new ArrayList(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 getHeaders(String name) { + + if (HttpHeaders.ACCEPT.equalsIgnoreCase(name) && acceptMediaTypes != null) { + return Collections.enumeration(acceptMediaTypeStrings); + } + + return super.getHeaders(name); + } } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CustomAcceptHeaderHttpServletRequestUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CustomAcceptHeaderHttpServletRequestUnitTests.java new file mode 100644 index 000000000..fec19ac52 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CustomAcceptHeaderHttpServletRequestUnitTests.java @@ -0,0 +1,61 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +import org.junit.Test; +import org.springframework.data.rest.webmvc.BasePathAwareHandlerMapping.CustomAcceptHeaderHttpServletRequest; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.util.StringUtils; + +/** + * Unit tests for {@link CustomAcceptHeaderHttpServletRequest}. + * + * @author Oliver Gierke + * @soundtrack Spring engineering team meeting @ SpringOne Platform 2016 + */ +public class CustomAcceptHeaderHttpServletRequestUnitTests { + + HttpServletRequest request = new MockHttpServletRequest(); + + /** + * @see DATAREST-863 + */ + @Test + public void returnsRegisterdHeadersOnAccessForMultipleOnes() { + + List mediaTypes = Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_ATOM_XML); + HttpServletRequest servletRequest = new CustomAcceptHeaderHttpServletRequest(request, mediaTypes); + + assertThat(servletRequest.getHeader(HttpHeaders.ACCEPT), + is(StringUtils.collectionToCommaDelimitedString(mediaTypes))); + + List expected = Collections.list(servletRequest.getHeaders(HttpHeaders.ACCEPT)); + + assertThat(expected, hasSize(2)); + assertThat(expected, hasItems(MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.APPLICATION_ATOM_XML_VALUE)); + } +}