Support byte ranges in ResourceHttpRequestHandler

This commit introduces support for HTTP byte ranges in the
ResourceHttpRequestHandler. This support consists of a number of
changes:

- Parsing of HTTP Range headers in HttpHeaders, using a new HttpRange
  class and inner ByteRange/SuffixByteRange subclasses.
- MIME boundary generation moved from FormHttpMessageConverter to
  MimeTypeUtils.
- writePartialContent() method introduced in ResourceHttpRequestHandler,
  handling the byte range logic
- Additional partial content tests added to
  ResourceHttpRequestHandlerTests.

Issue: SPR-10805
This commit is contained in:
Arjen Poutsma
2015-03-04 11:55:00 +01:00
committed by Rossen Stoyanchev
parent 0e7eecfe34
commit da48739628
8 changed files with 640 additions and 35 deletions

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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
* 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,
@@ -30,11 +30,11 @@ import java.util.Locale;
import java.util.TimeZone;
import org.hamcrest.Matchers;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
*/
@@ -266,4 +266,16 @@ public class HttpHeadersTests {
assertThat(headers.getAllow(), Matchers.emptyCollectionOf(HttpMethod.class));
}
@Test
public void range() {
List<HttpRange> ranges = new ArrayList<>();
ranges.add(HttpRange.createByteRange(0, 499));
ranges.add(HttpRange.createByteRange(9500));
ranges.add(HttpRange.createSuffixRange(500));
headers.setRange(ranges);
assertEquals("Invalid Range header", ranges, headers.getRange());
assertEquals("Invalid Range header", "bytes=0-499, 9500-, -500", headers.getFirst("Range"));
}
}