/** * Convert each {@code HttpRange} into a {@code ResourceRegion}, selecting the * appropriate segment of the given {@code Resource} using HTTP Range information. * @param ranges the list of ranges * @param resource the resource to select the regions from * @return the list of regions for the given resource * @throws IllegalArgumentException if the sum of all ranges exceeds the * resource length. * @since 4.3 */ public static List<ResourceRegion> toResourceRegions(List<HttpRange> ranges, Resource resource) { if (CollectionUtils.isEmpty(ranges)) { return Collections.emptyList(); } List<ResourceRegion> regions = new ArrayList<>(ranges.size()); for (HttpRange range : ranges) { regions.add(range.toResourceRegion(resource)); } if (ranges.size() > 1) { long length = getLengthFor(resource); long total = regions.stream().map(ResourceRegion::getCount).reduce(0L, (count, sum) -> sum + count); Assert.isTrue(total < length, () -> "The sum of all ranges (" + total + ") " + "should be less than the resource length (" + length + ")"); } return regions; }
/** * Convert each {@code HttpRange} into a {@code ResourceRegion}, selecting the * appropriate segment of the given {@code Resource} using HTTP Range information. * @param ranges the list of ranges * @param resource the resource to select the regions from * @return the list of regions for the given resource * @throws IllegalArgumentException if the sum of all ranges exceeds the * resource length. * @since 4.3 */ public static List<ResourceRegion> toResourceRegions(List<HttpRange> ranges, Resource resource) { if (CollectionUtils.isEmpty(ranges)) { return Collections.emptyList(); } List<ResourceRegion> regions = new ArrayList<>(ranges.size()); for (HttpRange range : ranges) { regions.add(range.toResourceRegion(resource)); } if (ranges.size() > 1) { long length = getLengthFor(resource); long total = regions.stream().map(ResourceRegion::getCount).reduce(0L, (count, sum) -> sum + count); Assert.isTrue(total < length, () -> "The sum of all ranges (" + total + ") " + "should be less than the resource length (" + length + ")"); } return regions; }
@Test(expected = IllegalArgumentException.class) public void toResourceRegionInputStreamResource() { InputStreamResource resource = mock(InputStreamResource.class); HttpRange range = HttpRange.createByteRange(0, 9); range.toResourceRegion(resource); }
@Test public void shouldWritePartialContentByteRange() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Resource body = new ClassPathResource("byterangeresource.txt", getClass()); ResourceRegion region = HttpRange.createByteRange(0, 5).toResourceRegion(body); converter.write(region, MediaType.TEXT_PLAIN, outputMessage); HttpHeaders headers = outputMessage.getHeaders(); assertThat(headers.getContentType(), is(MediaType.TEXT_PLAIN)); assertThat(headers.getContentLength(), is(6L)); assertThat(headers.get(HttpHeaders.CONTENT_RANGE).size(), is(1)); assertThat(headers.get(HttpHeaders.CONTENT_RANGE).get(0), is("bytes 0-5/39")); assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), is("Spring")); }
@Test public void shouldWritePartialContentByteRangeNoEnd() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Resource body = new ClassPathResource("byterangeresource.txt", getClass()); ResourceRegion region = HttpRange.createByteRange(7).toResourceRegion(body); converter.write(region, MediaType.TEXT_PLAIN, outputMessage); HttpHeaders headers = outputMessage.getHeaders(); assertThat(headers.getContentType(), is(MediaType.TEXT_PLAIN)); assertThat(headers.getContentLength(), is(32L)); assertThat(headers.get(HttpHeaders.CONTENT_RANGE).size(), is(1)); assertThat(headers.get(HttpHeaders.CONTENT_RANGE).get(0), is("bytes 7-38/39")); assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), is("Framework test resource content.")); }
List<ResourceRegion> regions = new ArrayList<>(); for(HttpRange range : rangeList) { regions.add(range.toResourceRegion(body));
@Test // SPR-15041 public void applicationOctetStreamDefaultContentType() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); ClassPathResource body = Mockito.mock(ClassPathResource.class); BDDMockito.given(body.getFilename()).willReturn("spring.dat"); BDDMockito.given(body.contentLength()).willReturn(12L); BDDMockito.given(body.getInputStream()).willReturn(new ByteArrayInputStream("Spring Framework".getBytes())); HttpRange range = HttpRange.createByteRange(0, 5); ResourceRegion resourceRegion = range.toResourceRegion(body); converter.write(Collections.singletonList(resourceRegion), null, outputMessage); assertThat(outputMessage.getHeaders().getContentType(), is(MediaType.APPLICATION_OCTET_STREAM)); assertThat(outputMessage.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE), is("bytes 0-5/12")); assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), is("Spring")); }
@Test(expected = IllegalArgumentException.class) public void toResourceRegionIllegalLength() { ByteArrayResource resource = mock(ByteArrayResource.class); given(resource.contentLength()).willReturn(-1L); HttpRange range = HttpRange.createByteRange(0, 9); range.toResourceRegion(resource); }
@Test(expected = IllegalArgumentException.class) @SuppressWarnings("unchecked") public void toResourceRegionExceptionLength() throws IOException { InputStreamResource resource = mock(InputStreamResource.class); given(resource.contentLength()).willThrow(IOException.class); HttpRange range = HttpRange.createByteRange(0, 9); range.toResourceRegion(resource); }
@Test public void toResourceRegion() { byte[] bytes = "Spring Framework".getBytes(StandardCharsets.UTF_8); ByteArrayResource resource = new ByteArrayResource(bytes); HttpRange range = HttpRange.createByteRange(0, 5); ResourceRegion region = range.toResourceRegion(resource); assertEquals(resource, region.getResource()); assertEquals(0L, region.getPosition()); assertEquals(6L, region.getCount()); }
/** * Convert each {@code HttpRange} into a {@code ResourceRegion}, selecting the * appropriate segment of the given {@code Resource} using HTTP Range information. * @param ranges the list of ranges * @param resource the resource to select the regions from * @return the list of regions for the given resource * @throws IllegalArgumentException if the sum of all ranges exceeds the * resource length. * @since 4.3 */ public static List<ResourceRegion> toResourceRegions(List<HttpRange> ranges, Resource resource) { if (CollectionUtils.isEmpty(ranges)) { return Collections.emptyList(); } List<ResourceRegion> regions = new ArrayList<>(ranges.size()); for (HttpRange range : ranges) { regions.add(range.toResourceRegion(resource)); } if (ranges.size() > 1) { long length = getLengthFor(resource); long total = regions.stream().map(ResourceRegion::getCount).reduce(0L, (count, sum) -> sum + count); Assert.isTrue(total < length, () -> "The sum of all ranges (" + total + ") " + "should be less than the resource length (" + length + ")"); } return regions; }
/** * Convert each {@code HttpRange} into a {@code ResourceRegion}, selecting the * appropriate segment of the given {@code Resource} using HTTP Range information. * @param ranges the list of ranges * @param resource the resource to select the regions from * @return the list of regions for the given resource * @throws IllegalArgumentException if the sum of all ranges exceeds the * resource length. * @since 4.3 */ public static List<ResourceRegion> toResourceRegions(List<HttpRange> ranges, Resource resource) { if (CollectionUtils.isEmpty(ranges)) { return Collections.emptyList(); } List<ResourceRegion> regions = new ArrayList<>(ranges.size()); for (HttpRange range : ranges) { regions.add(range.toResourceRegion(resource)); } if (ranges.size() > 1) { long length = getLengthFor(resource); long total = regions.stream().map(ResourceRegion::getCount).reduce(0L, (count, sum) -> sum + count); Assert.isTrue(total < length, () -> "The sum of all ranges (" + total + ") " + "should be less than the resource length (" + length + ")"); } return regions; }