Search in sources :

Example 16 with ResourceRegion

use of org.springframework.core.io.support.ResourceRegion in project spring-framework by spring-projects.

the class ResourceRegionEncoderTests method cancelWithoutDemandForMultipleResourceRegions.

// gh-22107
@Test
void cancelWithoutDemandForMultipleResourceRegions() {
    Resource resource = new ClassPathResource("ResourceRegionEncoderTests.txt", getClass());
    Flux<ResourceRegion> regions = Flux.just(new ResourceRegion(resource, 0, 6), new ResourceRegion(resource, 7, 9), new ResourceRegion(resource, 17, 4), new ResourceRegion(resource, 22, 17));
    String boundary = MimeTypeUtils.generateMultipartBoundaryString();
    Flux<DataBuffer> flux = this.encoder.encode(regions, this.bufferFactory, ResolvableType.forClass(ResourceRegion.class), MimeType.valueOf("text/plain"), Collections.singletonMap(ResourceRegionEncoder.BOUNDARY_STRING_HINT, boundary));
    ZeroDemandSubscriber subscriber = new ZeroDemandSubscriber();
    flux.subscribe(subscriber);
    subscriber.cancel();
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 17 with ResourceRegion

use of org.springframework.core.io.support.ResourceRegion in project spring-framework by spring-projects.

the class ResourceRegionEncoderTests method shouldEncodeResourceRegionFileResource.

@Test
void shouldEncodeResourceRegionFileResource() throws Exception {
    ResourceRegion region = new ResourceRegion(new ClassPathResource("ResourceRegionEncoderTests.txt", getClass()), 0, 6);
    Flux<DataBuffer> result = this.encoder.encode(Mono.just(region), this.bufferFactory, ResolvableType.forClass(ResourceRegion.class), MimeTypeUtils.APPLICATION_OCTET_STREAM, Collections.emptyMap());
    StepVerifier.create(result).consumeNextWith(stringConsumer("Spring")).expectComplete().verify();
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) ClassPathResource(org.springframework.core.io.ClassPathResource) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 18 with ResourceRegion

use of org.springframework.core.io.support.ResourceRegion 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 19 with ResourceRegion

use of org.springframework.core.io.support.ResourceRegion 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 20 with ResourceRegion

use of org.springframework.core.io.support.ResourceRegion 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)

Aggregations

ResourceRegion (org.springframework.core.io.support.ResourceRegion)20 Test (org.junit.jupiter.api.Test)12 ClassPathResource (org.springframework.core.io.ClassPathResource)10 Resource (org.springframework.core.io.Resource)10 DataBuffer (org.springframework.core.io.buffer.DataBuffer)8 HttpHeaders (org.springframework.http.HttpHeaders)7 HttpRange (org.springframework.http.HttpRange)5 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)5 MediaType (org.springframework.http.MediaType)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ByteArrayResource (org.springframework.core.io.ByteArrayResource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OptionalLong (java.util.OptionalLong)1