Skip to content

Commit 818e4b0

Browse files
committed
Refine Content-Range support for Resources
This commit restricts the support of `"Content-Range"` when returning `Resource` instances from Controllers - now only "HTTP 200 OK" responses will be considered, as Controllers might want to handle content range themselves. Issue: SPR-16921
1 parent d5df097 commit 818e4b0

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
*
6565
* @author Arjen Poutsma
6666
* @author Rossen Stoyanchev
67+
* @author Brian Clozel
6768
* @since 3.1
6869
*/
6970
public abstract class AbstractMessageConverterMethodProcessor extends AbstractMessageConverterMethodArgumentResolver
@@ -194,7 +195,8 @@ protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter
194195

195196
if (isResourceType(value, returnType)) {
196197
outputMessage.getHeaders().set(HttpHeaders.ACCEPT_RANGES, "bytes");
197-
if (value != null && inputMessage.getHeaders().getFirst(HttpHeaders.RANGE) != null) {
198+
if (value != null && inputMessage.getHeaders().getFirst(HttpHeaders.RANGE) != null
199+
&& outputMessage.getServletResponse().getStatus() == 200) {
198200
Resource resource = (Resource) value;
199201
try {
200202
List<HttpRange> httpRanges = inputMessage.getHeaders().getRange();
@@ -315,7 +317,7 @@ protected Class<?> getReturnValueType(@Nullable Object value, MethodParameter re
315317
}
316318

317319
/**
318-
* Return whether the returned value or the declared return type extend {@link Resource}.
320+
* Return whether the returned value or the declared return type extends {@link Resource}.
319321
*/
320322
protected boolean isResourceType(@Nullable Object value, MethodParameter returnType) {
321323
Class<?> clazz = getReturnValueType(value, returnType);

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,24 @@ public void disableRangeSupportForStreamingResponses() throws Exception {
568568
assertThat(servletResponse.getHeader(HttpHeaders.ACCEPT_RANGES), Matchers.isEmptyOrNullString());
569569
}
570570

571+
@Test //SPR-16921
572+
public void disableRangeSupportIfContentRangePresent() throws Exception {
573+
ResponseEntity<Resource> returnValue = ResponseEntity
574+
.status(HttpStatus.PARTIAL_CONTENT)
575+
.header(HttpHeaders.RANGE, "bytes=0-5")
576+
.body(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)));
577+
578+
given(resourceRegionMessageConverter.canWrite(any(), eq(null))).willReturn(true);
579+
given(resourceRegionMessageConverter.canWrite(any(), eq(APPLICATION_OCTET_STREAM))).willReturn(true);
580+
581+
processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest);
582+
583+
then(resourceRegionMessageConverter).should(never()).write(anyCollection(), any(), any());
584+
assertEquals(206, servletResponse.getStatus());
585+
}
586+
571587
@Test //SPR-14767
572-
public void shouldHandleValidatorHeadersInPutResponses() throws Exception {
588+
public void shouldHandleValidatorHeadersInputResponses() throws Exception {
573589
servletRequest.setMethod("PUT");
574590
String etagValue = "\"some-etag\"";
575591
ResponseEntity<String> returnValue = ResponseEntity.ok().header(HttpHeaders.ETAG, etagValue).body("body");

0 commit comments

Comments
 (0)