Support for byte-range requests in WebMvc.fn
This commit introduces support for byte-range requests in Servlet Functional endpoints. Closes gh-24562
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -41,9 +41,13 @@ import org.reactivestreams.Subscriber;
|
|||||||
import org.reactivestreams.Subscription;
|
import org.reactivestreams.Subscription;
|
||||||
|
|
||||||
import org.springframework.core.ParameterizedTypeReference;
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
|
import org.springframework.core.io.InputStreamResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.core.io.support.ResourceRegion;
|
||||||
import org.springframework.http.CacheControl;
|
import org.springframework.http.CacheControl;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.HttpRange;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.InvalidMediaTypeException;
|
import org.springframework.http.InvalidMediaTypeException;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
@@ -70,6 +74,9 @@ final class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T>
|
|||||||
private static final boolean reactiveStreamsPresent = ClassUtils.isPresent(
|
private static final boolean reactiveStreamsPresent = ClassUtils.isPresent(
|
||||||
"org.reactivestreams.Publisher", DefaultEntityResponseBuilder.class.getClassLoader());
|
"org.reactivestreams.Publisher", DefaultEntityResponseBuilder.class.getClassLoader());
|
||||||
|
|
||||||
|
private static final Type RESOURCE_REGION_LIST_TYPE =
|
||||||
|
new ParameterizedTypeReference<List<ResourceRegion>>() { }.getType();
|
||||||
|
|
||||||
|
|
||||||
private final T entity;
|
private final T entity;
|
||||||
|
|
||||||
@@ -245,6 +252,11 @@ final class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T>
|
|||||||
this.entityType = entityType;
|
this.entityType = entityType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static <T> boolean isResource(T entity) {
|
||||||
|
return !(entity instanceof InputStreamResource) &&
|
||||||
|
(entity instanceof Resource);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T entity() {
|
public T entity() {
|
||||||
return this.entity;
|
return this.entity;
|
||||||
@@ -267,13 +279,33 @@ final class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T>
|
|||||||
ServletServerHttpResponse serverResponse = new ServletServerHttpResponse(response);
|
ServletServerHttpResponse serverResponse = new ServletServerHttpResponse(response);
|
||||||
MediaType contentType = getContentType(response);
|
MediaType contentType = getContentType(response);
|
||||||
Class<?> entityClass = entity.getClass();
|
Class<?> entityClass = entity.getClass();
|
||||||
|
Type entityType = this.entityType;
|
||||||
|
|
||||||
|
if (entityClass != InputStreamResource.class && Resource.class.isAssignableFrom(entityClass)) {
|
||||||
|
serverResponse.getHeaders().set(HttpHeaders.ACCEPT_RANGES, "bytes");
|
||||||
|
String rangeHeader = request.getHeader(HttpHeaders.RANGE);
|
||||||
|
if (rangeHeader != null) {
|
||||||
|
Resource resource = (Resource) entity;
|
||||||
|
try {
|
||||||
|
List<HttpRange> httpRanges = HttpRange.parseRanges(rangeHeader);
|
||||||
|
serverResponse.getServletResponse().setStatus(HttpStatus.PARTIAL_CONTENT.value());
|
||||||
|
entity = HttpRange.toResourceRegions(httpRanges, resource);
|
||||||
|
entityClass = entity.getClass();
|
||||||
|
entityType = RESOURCE_REGION_LIST_TYPE;
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException ex) {
|
||||||
|
serverResponse.getHeaders().set(HttpHeaders.CONTENT_RANGE, "bytes */" + resource.contentLength());
|
||||||
|
serverResponse.getServletResponse().setStatus(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (HttpMessageConverter<?> messageConverter : context.messageConverters()) {
|
for (HttpMessageConverter<?> messageConverter : context.messageConverters()) {
|
||||||
if (messageConverter instanceof GenericHttpMessageConverter<?>) {
|
if (messageConverter instanceof GenericHttpMessageConverter<?>) {
|
||||||
GenericHttpMessageConverter<Object> genericMessageConverter =
|
GenericHttpMessageConverter<Object> genericMessageConverter =
|
||||||
(GenericHttpMessageConverter<Object>) messageConverter;
|
(GenericHttpMessageConverter<Object>) messageConverter;
|
||||||
if (genericMessageConverter.canWrite(this.entityType, entityClass, contentType)) {
|
if (genericMessageConverter.canWrite(entityType, entityClass, contentType)) {
|
||||||
genericMessageConverter.write(entity, this.entityType, contentType, serverResponse);
|
genericMessageConverter.write(entity, entityType, contentType, serverResponse);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,7 +17,9 @@
|
|||||||
package org.springframework.web.servlet.function;
|
package org.springframework.web.servlet.function;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -29,11 +31,13 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
import org.springframework.http.converter.ResourceHttpMessageConverter;
|
import org.springframework.http.converter.ResourceHttpMessageConverter;
|
||||||
|
import org.springframework.http.converter.ResourceRegionHttpMessageConverter;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||||
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
||||||
@@ -56,10 +60,11 @@ public class ResourceHandlerFunctionTests {
|
|||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void createContext() {
|
public void createContext() {
|
||||||
this.messageConverter = new ResourceHttpMessageConverter();
|
this.messageConverter = new ResourceHttpMessageConverter();
|
||||||
|
ResourceRegionHttpMessageConverter regionConverter = new ResourceRegionHttpMessageConverter();
|
||||||
this.context = new ServerResponse.Context() {
|
this.context = new ServerResponse.Context() {
|
||||||
@Override
|
@Override
|
||||||
public List<HttpMessageConverter<?>> messageConverters() {
|
public List<HttpMessageConverter<?>> messageConverters() {
|
||||||
return Collections.singletonList(messageConverter);
|
return Arrays.asList(messageConverter, regionConverter);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -73,8 +78,7 @@ public class ResourceHandlerFunctionTests {
|
|||||||
|
|
||||||
ServerResponse response = this.handlerFunction.handle(request);
|
ServerResponse response = this.handlerFunction.handle(request);
|
||||||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||||
boolean condition = response instanceof EntityResponse;
|
assertThat(response).isInstanceOf(EntityResponse.class);
|
||||||
assertThat(condition).isTrue();
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
EntityResponse<Resource> entityResponse = (EntityResponse<Resource>) response;
|
EntityResponse<Resource> entityResponse = (EntityResponse<Resource>) response;
|
||||||
assertThat(entityResponse.entity()).isEqualTo(this.resource);
|
assertThat(entityResponse.entity()).isEqualTo(this.resource);
|
||||||
@@ -91,6 +95,61 @@ public class ResourceHandlerFunctionTests {
|
|||||||
assertThat(servletResponse.getContentLength()).isEqualTo(this.resource.contentLength());
|
assertThat(servletResponse.getContentLength()).isEqualTo(this.resource.contentLength());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getRange() throws IOException, ServletException {
|
||||||
|
MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/");
|
||||||
|
servletRequest.addHeader("Range", "bytes=0-5");
|
||||||
|
ServerRequest request = new DefaultServerRequest(servletRequest, Collections.singletonList(messageConverter));
|
||||||
|
|
||||||
|
ServerResponse response = this.handlerFunction.handle(request);
|
||||||
|
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||||
|
assertThat(response).isInstanceOf(EntityResponse.class);
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
EntityResponse<Resource> entityResponse = (EntityResponse<Resource>) response;
|
||||||
|
assertThat(entityResponse.entity()).isEqualTo(this.resource);
|
||||||
|
|
||||||
|
MockHttpServletResponse servletResponse = new MockHttpServletResponse();
|
||||||
|
ModelAndView mav = response.writeTo(servletRequest, servletResponse, this.context);
|
||||||
|
assertThat(mav).isNull();
|
||||||
|
|
||||||
|
assertThat(servletResponse.getStatus()).isEqualTo(206);
|
||||||
|
byte[] expectedBytes = new byte[6];
|
||||||
|
try (InputStream is = this.resource.getInputStream()) {
|
||||||
|
is.read(expectedBytes);
|
||||||
|
}
|
||||||
|
byte[] actualBytes = servletResponse.getContentAsByteArray();
|
||||||
|
assertThat(actualBytes).isEqualTo(expectedBytes);
|
||||||
|
assertThat(servletResponse.getContentType()).isEqualTo(MediaType.TEXT_PLAIN_VALUE);
|
||||||
|
assertThat(servletResponse.getContentLength()).isEqualTo(6);
|
||||||
|
assertThat(servletResponse.getHeader(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getInvalidRange() throws IOException, ServletException {
|
||||||
|
MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/");
|
||||||
|
servletRequest.addHeader("Range", "bytes=0-10, 0-10, 0-10, 0-10, 0-10, 0-10");
|
||||||
|
ServerRequest request = new DefaultServerRequest(servletRequest, Collections.singletonList(messageConverter));
|
||||||
|
|
||||||
|
ServerResponse response = this.handlerFunction.handle(request);
|
||||||
|
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||||
|
assertThat(response).isInstanceOf(EntityResponse.class);
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
EntityResponse<Resource> entityResponse = (EntityResponse<Resource>) response;
|
||||||
|
assertThat(entityResponse.entity()).isEqualTo(this.resource);
|
||||||
|
|
||||||
|
MockHttpServletResponse servletResponse = new MockHttpServletResponse();
|
||||||
|
ModelAndView mav = response.writeTo(servletRequest, servletResponse, this.context);
|
||||||
|
assertThat(mav).isNull();
|
||||||
|
|
||||||
|
assertThat(servletResponse.getStatus()).isEqualTo(416);
|
||||||
|
byte[] expectedBytes = Files.readAllBytes(this.resource.getFile().toPath());
|
||||||
|
byte[] actualBytes = servletResponse.getContentAsByteArray();
|
||||||
|
assertThat(actualBytes).isEqualTo(expectedBytes);
|
||||||
|
assertThat(servletResponse.getContentType()).isEqualTo(MediaType.TEXT_PLAIN_VALUE);
|
||||||
|
assertThat(servletResponse.getContentLength()).isEqualTo(this.resource.contentLength());
|
||||||
|
assertThat(servletResponse.getHeader(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void head() throws IOException, ServletException {
|
public void head() throws IOException, ServletException {
|
||||||
MockHttpServletRequest servletRequest = new MockHttpServletRequest("HEAD", "/");
|
MockHttpServletRequest servletRequest = new MockHttpServletRequest("HEAD", "/");
|
||||||
@@ -98,8 +157,7 @@ public class ResourceHandlerFunctionTests {
|
|||||||
|
|
||||||
ServerResponse response = this.handlerFunction.handle(request);
|
ServerResponse response = this.handlerFunction.handle(request);
|
||||||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||||
boolean condition = response instanceof EntityResponse;
|
assertThat(response).isInstanceOf(EntityResponse.class);
|
||||||
assertThat(condition).isTrue();
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
EntityResponse<Resource> entityResponse = (EntityResponse<Resource>) response;
|
EntityResponse<Resource> entityResponse = (EntityResponse<Resource>) response;
|
||||||
assertThat(entityResponse.entity().getFilename()).isEqualTo(this.resource.getFilename());
|
assertThat(entityResponse.entity().getFilename()).isEqualTo(this.resource.getFilename());
|
||||||
@@ -136,4 +194,5 @@ public class ResourceHandlerFunctionTests {
|
|||||||
assertThat(actualBytes.length).isEqualTo(0);
|
assertThat(actualBytes.length).isEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user