Skip to content

Commit 094bb99

Browse files
committed
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.
1 parent dfd48f5 commit 094bb99

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

spring-data-rest-hal-browser/src/test/java/org/springframework/data/rest/webmvc/halbrowser/HalBrowserIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void exposesJsonUnderApiRootByDefault() throws Exception {
8080

8181
mvc.perform(get(BASE_PATH).accept(MediaType.ALL)).//
8282
andExpect(status().isOk()).//
83-
andExpect(header().string(HttpHeaders.CONTENT_TYPE, is(MediaTypes.HAL_JSON.toString())));
83+
andExpect(header().string(HttpHeaders.CONTENT_TYPE, startsWith(MediaTypes.HAL_JSON.toString())));
8484
}
8585

8686
/**

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.security.Principal;
2424
import java.util.ArrayList;
2525
import java.util.Collection;
26+
import java.util.Collections;
2627
import java.util.Enumeration;
2728
import java.util.List;
2829
import java.util.Locale;
@@ -563,6 +564,7 @@ public Part getPart(String name) throws IOException, ServletException {
563564
static class CustomAcceptHeaderHttpServletRequest extends HttpServletRequestWrapper {
564565

565566
private final List<MediaType> acceptMediaTypes;
567+
private final List<String> acceptMediaTypeStrings;
566568

567569
/**
568570
* Creates a new {@link CustomAcceptHeaderHttpServletRequest} for the given delegate {@link HttpServletRequest} and
@@ -578,6 +580,14 @@ public CustomAcceptHeaderHttpServletRequest(HttpServletRequest request, List<Med
578580
Assert.notEmpty(acceptMediaTypes, "MediaTypes must not be empty!");
579581

580582
this.acceptMediaTypes = acceptMediaTypes;
583+
584+
List<String> acceptMediaTypeStrings = new ArrayList<String>(acceptMediaTypes.size());
585+
586+
for (MediaType mediaType : acceptMediaTypes) {
587+
acceptMediaTypeStrings.add(mediaType.toString());
588+
}
589+
590+
this.acceptMediaTypeStrings = acceptMediaTypeStrings;
581591
}
582592

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

594604
return super.getHeader(name);
595605
}
606+
607+
/*
608+
* (non-Javadoc)
609+
* @see javax.servlet.http.HttpServletRequestWrapper#getHeaders(java.lang.String)
610+
*/
611+
@Override
612+
public Enumeration<String> getHeaders(String name) {
613+
614+
if (HttpHeaders.ACCEPT.equalsIgnoreCase(name) && acceptMediaTypes != null) {
615+
return Collections.enumeration(acceptMediaTypeStrings);
616+
}
617+
618+
return super.getHeaders(name);
619+
}
596620
}
597621
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.rest.webmvc;
17+
18+
import static org.hamcrest.Matchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
import java.util.List;
24+
25+
import javax.servlet.http.HttpServletRequest;
26+
27+
import org.junit.Test;
28+
import org.springframework.data.rest.webmvc.BasePathAwareHandlerMapping.CustomAcceptHeaderHttpServletRequest;
29+
import org.springframework.http.HttpHeaders;
30+
import org.springframework.http.MediaType;
31+
import org.springframework.mock.web.MockHttpServletRequest;
32+
import org.springframework.util.StringUtils;
33+
34+
/**
35+
* Unit tests for {@link CustomAcceptHeaderHttpServletRequest}.
36+
*
37+
* @author Oliver Gierke
38+
* @soundtrack Spring engineering team meeting @ SpringOne Platform 2016
39+
*/
40+
public class CustomAcceptHeaderHttpServletRequestUnitTests {
41+
42+
HttpServletRequest request = new MockHttpServletRequest();
43+
44+
/**
45+
* @see DATAREST-863
46+
*/
47+
@Test
48+
public void returnsRegisterdHeadersOnAccessForMultipleOnes() {
49+
50+
List<MediaType> mediaTypes = Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_ATOM_XML);
51+
HttpServletRequest servletRequest = new CustomAcceptHeaderHttpServletRequest(request, mediaTypes);
52+
53+
assertThat(servletRequest.getHeader(HttpHeaders.ACCEPT),
54+
is(StringUtils.collectionToCommaDelimitedString(mediaTypes)));
55+
56+
List<String> expected = Collections.list(servletRequest.getHeaders(HttpHeaders.ACCEPT));
57+
58+
assertThat(expected, hasSize(2));
59+
assertThat(expected, hasItems(MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.APPLICATION_ATOM_XML_VALUE));
60+
}
61+
}

0 commit comments

Comments
 (0)