Search in sources :

Example 6 with HttpRange

use of org.springframework.http.HttpRange in project spring-framework by spring-projects.

the class DefaultServerRequestTests method header.

@Test
void header() {
    HttpHeaders httpHeaders = new HttpHeaders();
    List<MediaType> accept = Collections.singletonList(MediaType.APPLICATION_JSON);
    httpHeaders.setAccept(accept);
    List<Charset> acceptCharset = Collections.singletonList(UTF_8);
    httpHeaders.setAcceptCharset(acceptCharset);
    long contentLength = 42L;
    httpHeaders.setContentLength(contentLength);
    MediaType contentType = MediaType.TEXT_PLAIN;
    httpHeaders.setContentType(contentType);
    InetSocketAddress host = InetSocketAddress.createUnresolved("localhost", 80);
    httpHeaders.setHost(host);
    List<HttpRange> range = Collections.singletonList(HttpRange.createByteRange(0, 42));
    httpHeaders.setRange(range);
    MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("GET", "/", true);
    httpHeaders.forEach(servletRequest::addHeader);
    servletRequest.setContentType(MediaType.TEXT_PLAIN_VALUE);
    DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
    ServerRequest.Headers headers = request.headers();
    assertThat(headers.accept()).isEqualTo(accept);
    assertThat(headers.acceptCharset()).isEqualTo(acceptCharset);
    assertThat(headers.contentLength()).isEqualTo(OptionalLong.of(contentLength));
    assertThat(headers.contentType()).isEqualTo(Optional.of(contentType));
    assertThat(headers.header(HttpHeaders.CONTENT_TYPE)).containsExactly(MediaType.TEXT_PLAIN_VALUE);
    assertThat(headers.firstHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.TEXT_PLAIN_VALUE);
    assertThat(headers.asHttpHeaders()).isEqualTo(httpHeaders);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) InetSocketAddress(java.net.InetSocketAddress) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Charset(java.nio.charset.Charset) MediaType(org.springframework.http.MediaType) HttpRange(org.springframework.http.HttpRange) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with HttpRange

use of org.springframework.http.HttpRange in project spring-framework by spring-projects.

the class ResourceRegionHttpMessageConverterTests method partialContentMultipleByteRanges.

@Test
public void partialContentMultipleByteRanges() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    Resource body = new ClassPathResource("byterangeresource.txt", getClass());
    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();
    assertThat(headers.getContentType().toString()).startsWith("multipart/byteranges;boundary=");
    String boundary = "--" + headers.getContentType().toString().substring(30);
    String content = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
    String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true);
    assertThat(ranges[0]).isEqualTo(boundary);
    assertThat(ranges[1]).isEqualTo("Content-Type: text/plain");
    assertThat(ranges[2]).isEqualTo("Content-Range: bytes 0-5/39");
    assertThat(ranges[3]).isEqualTo("Spring");
    assertThat(ranges[4]).isEqualTo(boundary);
    assertThat(ranges[5]).isEqualTo("Content-Type: text/plain");
    assertThat(ranges[6]).isEqualTo("Content-Range: bytes 7-15/39");
    assertThat(ranges[7]).isEqualTo("Framework");
    assertThat(ranges[8]).isEqualTo(boundary);
    assertThat(ranges[9]).isEqualTo("Content-Type: text/plain");
    assertThat(ranges[10]).isEqualTo("Content-Range: bytes 17-20/39");
    assertThat(ranges[11]).isEqualTo("test");
    assertThat(ranges[12]).isEqualTo(boundary);
    assertThat(ranges[13]).isEqualTo("Content-Type: text/plain");
    assertThat(ranges[14]).isEqualTo("Content-Range: bytes 22-38/39");
    assertThat(ranges[15]).isEqualTo("resource content.");
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) HttpHeaders(org.springframework.http.HttpHeaders) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ArrayList(java.util.ArrayList) ClassPathResource(org.springframework.core.io.ClassPathResource) HttpRange(org.springframework.http.HttpRange) Test(org.junit.jupiter.api.Test)

Example 8 with HttpRange

use of org.springframework.http.HttpRange in project spring-framework by spring-projects.

the class ResourceRegionHttpMessageConverterTests method applicationOctetStreamDefaultContentType.

// SPR-15041
@Test
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()).isEqualTo(MediaType.APPLICATION_OCTET_STREAM);
    assertThat(outputMessage.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes 0-5/12");
    assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo("Spring");
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) HttpRange(org.springframework.http.HttpRange) Test(org.junit.jupiter.api.Test)

Example 9 with HttpRange

use of org.springframework.http.HttpRange in project spring-framework by spring-projects.

the class ResourceRegionHttpMessageConverterTests method partialContentMultipleByteRangesInRandomOrderAndOverlapping.

@Test
public void partialContentMultipleByteRangesInRandomOrderAndOverlapping() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    Resource body = new ClassPathResource("byterangeresource.txt", getClass());
    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();
    assertThat(headers.getContentType().toString()).startsWith("multipart/byteranges;boundary=");
    String boundary = "--" + headers.getContentType().toString().substring(30);
    String content = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
    String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true);
    assertThat(ranges[0]).isEqualTo(boundary);
    assertThat(ranges[1]).isEqualTo("Content-Type: text/plain");
    assertThat(ranges[2]).isEqualTo("Content-Range: bytes 7-15/39");
    assertThat(ranges[3]).isEqualTo("Framework");
    assertThat(ranges[4]).isEqualTo(boundary);
    assertThat(ranges[5]).isEqualTo("Content-Type: text/plain");
    assertThat(ranges[6]).isEqualTo("Content-Range: bytes 0-5/39");
    assertThat(ranges[7]).isEqualTo("Spring");
    assertThat(ranges[8]).isEqualTo(boundary);
    assertThat(ranges[9]).isEqualTo("Content-Type: text/plain");
    assertThat(ranges[10]).isEqualTo("Content-Range: bytes 17-20/39");
    assertThat(ranges[11]).isEqualTo("test");
    assertThat(ranges[12]).isEqualTo(boundary);
    assertThat(ranges[13]).isEqualTo("Content-Type: text/plain");
    assertThat(ranges[14]).isEqualTo("Content-Range: bytes 20-29/39");
    assertThat(ranges[15]).isEqualTo("t resource");
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) HttpHeaders(org.springframework.http.HttpHeaders) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ArrayList(java.util.ArrayList) ClassPathResource(org.springframework.core.io.ClassPathResource) HttpRange(org.springframework.http.HttpRange) Test(org.junit.jupiter.api.Test)

Example 10 with HttpRange

use of org.springframework.http.HttpRange in project OsmAnd-tools by osmandapp.

the class DownloadIndexController method writePartially.

private void writePartially(Resource res, HttpHeaders headers, HttpServletResponse resp) throws IOException {
    long contentLength = res.contentLength();
    List<HttpRange> ranges;
    try {
        ranges = headers.getRange();
    } catch (IllegalArgumentException ex) {
        resp.addHeader("Content-Range", "bytes */" + contentLength);
        resp.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
        LOGGER.error(ex.getMessage(), ex);
        return;
    }
    resp.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
    resp.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + res.getFilename() + "\"");
    if (ranges.size() == 1) {
        HttpRange range = ranges.get(0);
        long start = range.getRangeStart(contentLength);
        long end = range.getRangeEnd(contentLength);
        long rangeLength = end - start + 1;
        resp.setContentLengthLong(rangeLength);
        resp.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        resp.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
        resp.addHeader(HttpHeaders.CONTENT_RANGE, String.format("bytes %d-%d/%d", start, end, rangeLength));
        try (InputStream in = res.getInputStream()) {
            copyRange(in, resp.getOutputStream(), start, end);
        }
    } else {
        String boundaryString = MimeTypeUtils.generateMultipartBoundaryString();
        resp.setContentType("multipart/byteranges; boundary=" + boundaryString);
        ServletOutputStream out = resp.getOutputStream();
        for (HttpRange range : ranges) {
            long start = range.getRangeStart(contentLength);
            long end = range.getRangeEnd(contentLength);
            InputStream in = res.getInputStream();
            out.println();
            out.println("--" + boundaryString);
            out.println("Content-Type: " + MediaType.APPLICATION_OCTET_STREAM_VALUE);
            out.println("Content-Range: bytes " + start + "-" + end + "/" + contentLength);
            out.println();
            copyRange(in, out, start, end);
        }
        out.println();
        out.print("--" + boundaryString + "--");
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) HttpRange(org.springframework.http.HttpRange)

Aggregations

HttpRange (org.springframework.http.HttpRange)10 Test (org.junit.jupiter.api.Test)6 MediaType (org.springframework.http.MediaType)6 ResourceRegion (org.springframework.core.io.support.ResourceRegion)5 HttpHeaders (org.springframework.http.HttpHeaders)5 Resource (org.springframework.core.io.Resource)4 InetSocketAddress (java.net.InetSocketAddress)3 ArrayList (java.util.ArrayList)3 ClassPathResource (org.springframework.core.io.ClassPathResource)3 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)3 Charset (java.nio.charset.Charset)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 ResolvableType (org.springframework.core.ResolvableType)1