Skip to content

Commit

Permalink
DATAREST-863 - Adapt to changes in Accept header lookup in Spring 4.3.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
odrotbohm committed Aug 5, 2016
1 parent 684e0f9 commit 344c3ac
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void exposesJsonUnderApiRootByDefault() throws Exception {

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -563,6 +564,7 @@ public Part getPart(String name) throws IOException, ServletException {
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
Expand All @@ -578,6 +580,14 @@ public CustomAcceptHeaderHttpServletRequest(HttpServletRequest request, List<Med
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;
}

/*
Expand All @@ -593,5 +603,19 @@ public String getHeader(String name) {

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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 344c3ac

Please sign in to comment.