Refactor HTTP Range support with ResourceRegion

Prior to this commit, the `ResourceHttpMessageConverter` would support
all HTTP Range requests and `MethodProcessors` would "wrap" controller
handler return values with a `HttpRangeResource` to support that use
case in Controllers.

This commit refactors that support in several ways:
* a new ResourceRegion class has been introduced
* a new, separate, ResourceRegionHttpMessageConverter handles the HTTP
range use cases when serving static resources with the
ResourceHttpRequestHandler
* the support of HTTP range requests on Controller handlers has been
removed until a better solution is found

Issue: SPR-14221, SPR-13834
This commit is contained in:
Brian Clozel
2016-05-02 15:39:07 +02:00
parent 7737c3c7e5
commit 5ac31fb39d
16 changed files with 627 additions and 468 deletions

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2002-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.core.io;
import static org.mockito.Mockito.mock;
import org.junit.Test;
/**
* Unit tests for the {@link ResourceRegion} class.
*
* @author Brian Clozel
*/
public class ResourceRegionTests {
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWithNullResource() {
new ResourceRegion(null, 0, 1);
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionForNegativePosition() {
new ResourceRegion(mock(Resource.class), -1, 1);
}
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionForNegativeCount() {
new ResourceRegion(mock(Resource.class), 0, -1);
}
}

View File

@@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;
@@ -93,6 +94,15 @@ public class StreamUtilsTests {
verify(out, never()).close();
}
@Test
public void copyRange() throws Exception {
ByteArrayOutputStream out = spy(new ByteArrayOutputStream());
StreamUtils.copyRange(new ByteArrayInputStream(bytes), out, 0, 100);
byte[] range = Arrays.copyOfRange(bytes, 0, 101);
assertThat(out.toByteArray(), equalTo(range));
verify(out, never()).close();
}
@Test
public void nonClosingInputStream() throws Exception {
InputStream source = mock(InputStream.class);