Here are the examples of the java api org.springframework.http.HttpRange taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
7 Examples
18
Source : VideoServiceImpl.java
with MIT License
from Recks11
with MIT License
from Recks11
@Override
public Mono<ResourceRegion> getRegion(String name, ServerRequest request) {
HttpHeaders headers = request.headers().asHttpHeaders();
HttpRange range = !headers.getRange().isEmpty() ? headers.getRange().get(0) : null;
AtomicInteger sizeInt = new AtomicInteger();
request.queryParam("partial").ifPresent(val -> sizeInt.set(Integer.parseInt(val)));
long chunkSize = getChunkSize(sizeInt.get());
Mono<UrlResource> resource = getResourceByName(name);
return resource.map(urlResource -> {
long contentLength = lengthOf(urlResource);
if (range != null) {
long start = range.getRangeStart(contentLength);
long end = range.getRangeEnd(contentLength);
long resourceLength = end - start + 1;
long rangeLength = min(chunkSize, resourceLength);
return new ResourceRegion(urlResource, start, rangeLength);
} else {
long rangeLength = min(chunkSize, contentLength);
return new ResourceRegion(urlResource, 0, rangeLength);
}
}).doOnError(throwable -> {
throw Exceptions.propagate(throwable);
});
}
9
Source : ResourceRegionHttpMessageConverterTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void partialContentMultipleByteRangesInRandomOrderAndOverlapping() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource body = new ClreplacedPathResource("byterangeresource.txt", getClreplaced());
List<HttpRange> rangeList = HttpRange.parseRanges("bytes=7-15,0-5,17-20,20-29");
List<ResourceRegion> regions = new ArrayList<>();
for (HttpRange range : rangeList) {
regions.add(range.toResourceRegion(body));
}
converter.write(regions, MediaType.TEXT_PLAIN, outputMessage);
HttpHeaders headers = outputMessage.getHeaders();
replacedertThat(headers.getContentType().toString()).startsWith("multipart/byteranges;boundary=");
String boundary = "--" + headers.getContentType().toString().substring(30);
String content = outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8);
String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true);
replacedertThat(ranges[0]).isEqualTo(boundary);
replacedertThat(ranges[1]).isEqualTo("Content-Type: text/plain");
replacedertThat(ranges[2]).isEqualTo("Content-Range: bytes 7-15/39");
replacedertThat(ranges[3]).isEqualTo("Framework");
replacedertThat(ranges[4]).isEqualTo(boundary);
replacedertThat(ranges[5]).isEqualTo("Content-Type: text/plain");
replacedertThat(ranges[6]).isEqualTo("Content-Range: bytes 0-5/39");
replacedertThat(ranges[7]).isEqualTo("Spring");
replacedertThat(ranges[8]).isEqualTo(boundary);
replacedertThat(ranges[9]).isEqualTo("Content-Type: text/plain");
replacedertThat(ranges[10]).isEqualTo("Content-Range: bytes 17-20/39");
replacedertThat(ranges[11]).isEqualTo("test");
replacedertThat(ranges[12]).isEqualTo(boundary);
replacedertThat(ranges[13]).isEqualTo("Content-Type: text/plain");
replacedertThat(ranges[14]).isEqualTo("Content-Range: bytes 20-29/39");
replacedertThat(ranges[15]).isEqualTo("t resource");
}
9
Source : ResourceRegionHttpMessageConverterTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void partialContentMultipleByteRanges() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource body = new ClreplacedPathResource("byterangeresource.txt", getClreplaced());
List<HttpRange> rangeList = HttpRange.parseRanges("bytes=0-5,7-15,17-20,22-38");
List<ResourceRegion> regions = new ArrayList<>();
for (HttpRange range : rangeList) {
regions.add(range.toResourceRegion(body));
}
converter.write(regions, MediaType.TEXT_PLAIN, outputMessage);
HttpHeaders headers = outputMessage.getHeaders();
replacedertThat(headers.getContentType().toString()).startsWith("multipart/byteranges;boundary=");
String boundary = "--" + headers.getContentType().toString().substring(30);
String content = outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8);
String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true);
replacedertThat(ranges[0]).isEqualTo(boundary);
replacedertThat(ranges[1]).isEqualTo("Content-Type: text/plain");
replacedertThat(ranges[2]).isEqualTo("Content-Range: bytes 0-5/39");
replacedertThat(ranges[3]).isEqualTo("Spring");
replacedertThat(ranges[4]).isEqualTo(boundary);
replacedertThat(ranges[5]).isEqualTo("Content-Type: text/plain");
replacedertThat(ranges[6]).isEqualTo("Content-Range: bytes 7-15/39");
replacedertThat(ranges[7]).isEqualTo("Framework");
replacedertThat(ranges[8]).isEqualTo(boundary);
replacedertThat(ranges[9]).isEqualTo("Content-Type: text/plain");
replacedertThat(ranges[10]).isEqualTo("Content-Range: bytes 17-20/39");
replacedertThat(ranges[11]).isEqualTo("test");
replacedertThat(ranges[12]).isEqualTo(boundary);
replacedertThat(ranges[13]).isEqualTo("Content-Type: text/plain");
replacedertThat(ranges[14]).isEqualTo("Content-Range: bytes 22-38/39");
replacedertThat(ranges[15]).isEqualTo("resource content.");
}
8
Source : ResourceRegionHttpMessageConverterTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void partialContentMultipleByteRanges() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource body = new ClreplacedPathResource("byterangeresource.txt", getClreplaced());
List<HttpRange> rangeList = HttpRange.parseRanges("bytes=0-5,7-15,17-20,22-38");
List<ResourceRegion> regions = new ArrayList<>();
for (HttpRange range : rangeList) {
regions.add(range.toResourceRegion(body));
}
converter.write(regions, MediaType.TEXT_PLAIN, outputMessage);
HttpHeaders headers = outputMessage.getHeaders();
replacedertThat(headers.getContentType().toString(), Matchers.startsWith("multipart/byteranges;boundary="));
String boundary = "--" + headers.getContentType().toString().substring(30);
String content = outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8);
String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true);
replacedertThat(ranges[0], is(boundary));
replacedertThat(ranges[1], is("Content-Type: text/plain"));
replacedertThat(ranges[2], is("Content-Range: bytes 0-5/39"));
replacedertThat(ranges[3], is("Spring"));
replacedertThat(ranges[4], is(boundary));
replacedertThat(ranges[5], is("Content-Type: text/plain"));
replacedertThat(ranges[6], is("Content-Range: bytes 7-15/39"));
replacedertThat(ranges[7], is("Framework"));
replacedertThat(ranges[8], is(boundary));
replacedertThat(ranges[9], is("Content-Type: text/plain"));
replacedertThat(ranges[10], is("Content-Range: bytes 17-20/39"));
replacedertThat(ranges[11], is("test"));
replacedertThat(ranges[12], is(boundary));
replacedertThat(ranges[13], is("Content-Type: text/plain"));
replacedertThat(ranges[14], is("Content-Range: bytes 22-38/39"));
replacedertThat(ranges[15], is("resource content."));
}
5
Source : ResourceRegionHttpMessageConverterTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
// SPR-15041
@Test
public void applicationOctetStreamDefaultContentType() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
ClreplacedPathResource body = Mockito.mock(ClreplacedPathResource.clreplaced);
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);
replacedertThat(outputMessage.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_OCTET_STREAM);
replacedertThat(outputMessage.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes 0-5/12");
replacedertThat(outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8)).isEqualTo("Spring");
}
4
Source : ResourceRegionHttpMessageConverterTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
// SPR-15041
@Test
public void applicationOctetStreamDefaultContentType() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
ClreplacedPathResource body = Mockito.mock(ClreplacedPathResource.clreplaced);
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);
replacedertThat(outputMessage.getHeaders().getContentType(), is(MediaType.APPLICATION_OCTET_STREAM));
replacedertThat(outputMessage.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE), is("bytes 0-5/12"));
replacedertThat(outputMessage.getBodyreplacedtring(StandardCharsets.UTF_8), is("Spring"));
}
2
Source : ResourceHttpRequestHandler.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
/**
* Write parts of the resource as indicated by the request {@code Range} header.
* @param request current servlet request
* @param response current servlet response
* @param resource the identified resource (never {@code null})
* @param contentType the content type
* @throws IOException in case of errors while writing the content
*/
protected void writePartialContent(HttpServletRequest request, HttpServletResponse response, Resource resource, MediaType contentType) throws IOException {
long length = resource.contentLength();
List<HttpRange> ranges;
try {
HttpHeaders headers = new ServletServerHttpRequest(request).getHeaders();
ranges = headers.getRange();
} catch (IllegalArgumentException ex) {
response.addHeader("Content-Range", "bytes */" + length);
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
return;
}
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
if (ranges.size() == 1) {
HttpRange range = ranges.get(0);
long start = range.getRangeStart(length);
long end = range.getRangeEnd(length);
long rangeLength = end - start + 1;
setHeaders(response, resource, contentType);
response.addHeader("Content-Range", "bytes " + start + "-" + end + "/" + length);
response.setContentLength((int) rangeLength);
InputStream in = resource.getInputStream();
try {
copyRange(in, response.getOutputStream(), start, end);
} finally {
try {
in.close();
} catch (IOException ex) {
// ignore
}
}
} else {
String boundaryString = MimeTypeUtils.generateMultipartBoundaryString();
response.setContentType("multipart/byteranges; boundary=" + boundaryString);
ServletOutputStream out = response.getOutputStream();
for (HttpRange range : ranges) {
long start = range.getRangeStart(length);
long end = range.getRangeEnd(length);
InputStream in = resource.getInputStream();
// Writing MIME header.
out.println();
out.println("--" + boundaryString);
if (contentType != null) {
out.println("Content-Type: " + contentType);
}
out.println("Content-Range: bytes " + start + "-" + end + "/" + length);
out.println();
// Printing content
copyRange(in, out, start, end);
}
out.println();
out.print("--" + boundaryString + "--");
}
}