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-04 21:13:28 -07:00
parent f8806cc43a
commit 0cae0e5e70
3 changed files with 86 additions and 1 deletions

View File

@@ -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())));
}
/**

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);
}
}
}

View File

@@ -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<MediaType> 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<String> expected = Collections.list(servletRequest.getHeaders(HttpHeaders.ACCEPT));
assertThat(expected, hasSize(2));
assertThat(expected, hasItems(MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.APPLICATION_ATOM_XML_VALUE));
}
}