org.springframework.core.io.support.ResourceRegion Java Examples

The following examples show how to use org.springframework.core.io.support.ResourceRegion. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpRange.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Turn a {@code Resource} into a {@link ResourceRegion} using the range
 * information contained in the current {@code HttpRange}.
 * @param resource the {@code Resource} to select the region from
 * @return the selected region of the given {@code Resource}
 * @since 4.3
 */
public ResourceRegion toResourceRegion(Resource resource) {
	// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
	// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
	Assert.isTrue(resource.getClass() != InputStreamResource.class,
			"Cannot convert an InputStreamResource to a ResourceRegion");
	try {
		long contentLength = resource.contentLength();
		Assert.isTrue(contentLength > 0, "Resource content length should be > 0");
		long start = getRangeStart(contentLength);
		long end = getRangeEnd(contentLength);
		return new ResourceRegion(resource, start, end - start + 1);
	}
	catch (IOException ex) {
		throw new IllegalArgumentException("Failed to convert Resource to ResourceRegion", ex);
	}
}
 
Example #2
Source File: ResourceRegionHttpMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	if (object instanceof ResourceRegion) {
		writeResourceRegion((ResourceRegion) object, outputMessage);
	}
	else {
		Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
		if (regions.size() == 1) {
			writeResourceRegion(regions.iterator().next(), outputMessage);
		}
		else {
			writeResourceRegionCollection((Collection<ResourceRegion>) object, outputMessage);
		}
	}
}
 
Example #3
Source File: ResourceRegionHttpMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public boolean canWrite(@Nullable Type type, @Nullable Class<?> clazz, @Nullable MediaType mediaType) {
	if (!(type instanceof ParameterizedType)) {
		return (type instanceof Class && ResourceRegion.class.isAssignableFrom((Class<?>) type));
	}

	ParameterizedType parameterizedType = (ParameterizedType) type;
	if (!(parameterizedType.getRawType() instanceof Class)) {
		return false;
	}
	Class<?> rawType = (Class<?>) parameterizedType.getRawType();
	if (!(Collection.class.isAssignableFrom(rawType))) {
		return false;
	}
	if (parameterizedType.getActualTypeArguments().length != 1) {
		return false;
	}
	Type typeArgument = parameterizedType.getActualTypeArguments()[0];
	if (!(typeArgument instanceof Class)) {
		return false;
	}

	Class<?> typeArgumentClass = (Class<?>) typeArgument;
	return ResourceRegion.class.isAssignableFrom(typeArgumentClass);
}
 
Example #4
Source File: RangeAwareResourceRegionHttpMessageConverter.java    From engine with GNU General Public License v3.0 6 votes vote down vote up
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
    Assert.notNull(region, "ResourceRegion must not be null");
    HttpHeaders responseHeaders = outputMessage.getHeaders();

    long start = region.getPosition();
    long end = start + region.getCount() - 1;
    Long resourceLength = region.getResource().contentLength();
    end = Math.min(end, resourceLength - 1);
    long rangeLength = end - start + 1;
    responseHeaders.add("Content-Range", "bytes " + start + '-' + end + '/' + resourceLength);
    responseHeaders.setContentLength(rangeLength);

    InputStream in = null;
    try {
        Resource resource = region.getResource();
        if (resource instanceof RangeAwareResource) {
            in = ((RangeAwareResource) resource).getInputStream(start, end);
            StreamUtils.copy(in, outputMessage.getBody());
        } else {
            in = resource.getInputStream();
            StreamUtils.copyRange(in, outputMessage.getBody(), start, end);
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}
 
Example #5
Source File: ResourceRegionEncoderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test // gh-22107
public void cancelWithoutDemandForSingleResourceRegion() {
	Resource resource = new ClassPathResource("ResourceRegionEncoderTests.txt", getClass());
	Mono<ResourceRegion> regions = Mono.just(new ResourceRegion(resource, 0, 6));
	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();
}
 
Example #6
Source File: ResourceRegionEncoderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test // gh-22107
public 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();
}
 
Example #7
Source File: ResourceRegionHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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"));
}
 
Example #8
Source File: HttpRangeTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void toResourceRegionsValidations() {
	byte[] bytes = "12345".getBytes(StandardCharsets.UTF_8);
	ByteArrayResource resource = new ByteArrayResource(bytes);

	// 1. Below length
	List<HttpRange> ranges = HttpRange.parseRanges("bytes=0-1,2-3");
	List<ResourceRegion> regions = HttpRange.toResourceRegions(ranges, resource);
	assertEquals(2, regions.size());

	// 2. At length
	ranges = HttpRange.parseRanges("bytes=0-1,2-4");
	try {
		HttpRange.toResourceRegions(ranges, resource);
		fail();
	}
	catch (IllegalArgumentException ex) {
		// Expected..
	}
}
 
Example #9
Source File: ResourceRegionHttpMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
	Assert.notNull(region, "ResourceRegion must not be null");
	HttpHeaders responseHeaders = outputMessage.getHeaders();

	long start = region.getPosition();
	long end = start + region.getCount() - 1;
	Long resourceLength = region.getResource().contentLength();
	end = Math.min(end, resourceLength - 1);
	long rangeLength = end - start + 1;
	responseHeaders.add("Content-Range", "bytes " + start + '-' + end + '/' + resourceLength);
	responseHeaders.setContentLength(rangeLength);

	InputStream in = region.getResource().getInputStream();
	try {
		StreamUtils.copyRange(in, outputMessage.getBody(), start, end);
	}
	finally {
		try {
			in.close();
		}
		catch (IOException ex) {
			// ignore
		}
	}
}
 
Example #10
Source File: ResourceHttpMessageWriter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region,
		ReactiveHttpOutputMessage message, Map<String, Object> hints) {

	if (message instanceof ZeroCopyHttpOutputMessage && resource.isFile()) {
		try {
			File file = resource.getFile();
			long pos = region != null ? region.getPosition() : 0;
			long count = region != null ? region.getCount() : file.length();
			if (logger.isDebugEnabled()) {
				String formatted = region != null ? "region " + pos + "-" + (count) + " of " : "";
				logger.debug(Hints.getLogPrefix(hints) + "Zero-copy " + formatted + "[" + resource + "]");
			}
			return Optional.of(((ZeroCopyHttpOutputMessage) message).writeWith(file, pos, count));
		}
		catch (IOException ex) {
			// should not happen
		}
	}
	return Optional.empty();
}
 
Example #11
Source File: ResourceRegionHttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) {
	if (!(type instanceof ParameterizedType)) {
		return ResourceRegion.class.isAssignableFrom((Class<?>) type);
	}
	ParameterizedType parameterizedType = (ParameterizedType) type;
	if (!(parameterizedType.getRawType() instanceof Class)) {
		return false;
	}
	Class<?> rawType = (Class<?>) parameterizedType.getRawType();
	if (!(Collection.class.isAssignableFrom(rawType))) {
		return false;
	}
	if (parameterizedType.getActualTypeArguments().length != 1) {
		return false;
	}
	Type typeArgument = parameterizedType.getActualTypeArguments()[0];
	if (!(typeArgument instanceof Class)) {
		return false;
	}
	Class<?> typeArgumentClass = (Class<?>) typeArgument;
	return typeArgumentClass.isAssignableFrom(ResourceRegion.class);
}
 
Example #12
Source File: HttpRange.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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 = 0;
		for (ResourceRegion region : regions) {
			total += region.getCount();
		}
		if (total >= length) {
			throw new IllegalArgumentException("The sum of all ranges (" + total +
					") should be less than the resource length (" + length + ")");
		}
	}
	return regions;
}
 
Example #13
Source File: ResourceRegionHttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	if (object instanceof ResourceRegion) {
		writeResourceRegion((ResourceRegion) object, outputMessage);
	}
	else {
		Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
		if (regions.size() == 1) {
			writeResourceRegion(regions.iterator().next(), outputMessage);
		}
		else {
			writeResourceRegionCollection((Collection<ResourceRegion>) object, outputMessage);
		}
	}
}
 
Example #14
Source File: ResourceHttpMessageWriter.java    From java-technology-stack with MIT License 6 votes vote down vote up
private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region,
		ReactiveHttpOutputMessage message, Map<String, Object> hints) {

	if (message instanceof ZeroCopyHttpOutputMessage && resource.isFile()) {
		try {
			File file = resource.getFile();
			long pos = region != null ? region.getPosition() : 0;
			long count = region != null ? region.getCount() : file.length();
			if (logger.isDebugEnabled()) {
				String formatted = region != null ? "region " + pos + "-" + (count) + " of " : "";
				logger.debug(Hints.getLogPrefix(hints) + "Zero-copy " + formatted + "[" + resource + "]");
			}
			return Optional.of(((ZeroCopyHttpOutputMessage) message).writeWith(file, pos, count));
		}
		catch (IOException ex) {
			// should not happen
		}
	}
	return Optional.empty();
}
 
Example #15
Source File: ResourceRegionHttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
	Assert.notNull(region, "ResourceRegion must not be null");
	HttpHeaders responseHeaders = outputMessage.getHeaders();

	long start = region.getPosition();
	long end = start + region.getCount() - 1;
	Long resourceLength = region.getResource().contentLength();
	end = Math.min(end, resourceLength - 1);
	long rangeLength = end - start + 1;
	responseHeaders.add("Content-Range", "bytes " + start + '-' + end + '/' + resourceLength);
	responseHeaders.setContentLength(rangeLength);

	InputStream in = region.getResource().getInputStream();
	try {
		StreamUtils.copyRange(in, outputMessage.getBody(), start, end);
	}
	finally {
		try {
			in.close();
		}
		catch (IOException ex) {
			// ignore
		}
	}
}
 
Example #16
Source File: ResourceRegionHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
	Assert.notNull(region, "ResourceRegion must not be null");
	HttpHeaders responseHeaders = outputMessage.getHeaders();

	long start = region.getPosition();
	long end = start + region.getCount() - 1;
	Long resourceLength = region.getResource().contentLength();
	end = Math.min(end, resourceLength - 1);
	long rangeLength = end - start + 1;
	responseHeaders.add("Content-Range", "bytes " + start + '-' + end + '/' + resourceLength);
	responseHeaders.setContentLength(rangeLength);

	InputStream in = region.getResource().getInputStream();
	try {
		StreamUtils.copyRange(in, outputMessage.getBody(), start, end);
	}
	finally {
		try {
			in.close();
		}
		catch (IOException ex) {
			// ignore
		}
	}
}
 
Example #17
Source File: ResourceRegionHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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"));
}
 
Example #18
Source File: ResourceRegionHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected void writeInternal(Object object, @Nullable Type type, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	if (object instanceof ResourceRegion) {
		writeResourceRegion((ResourceRegion) object, outputMessage);
	}
	else {
		Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
		if (regions.size() == 1) {
			writeResourceRegion(regions.iterator().next(), outputMessage);
		}
		else {
			writeResourceRegionCollection((Collection<ResourceRegion>) object, outputMessage);
		}
	}
}
 
Example #19
Source File: HttpRangeTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void toResourceRegionsValidations() {
	byte[] bytes = "12345".getBytes(StandardCharsets.UTF_8);
	ByteArrayResource resource = new ByteArrayResource(bytes);

	// 1. Below length
	List<HttpRange> ranges = HttpRange.parseRanges("bytes=0-1,2-3");
	List<ResourceRegion> regions = HttpRange.toResourceRegions(ranges, resource);
	assertEquals(2, regions.size());

	// 2. At length
	ranges = HttpRange.parseRanges("bytes=0-1,2-4");
	try {
		HttpRange.toResourceRegions(ranges, resource);
		fail();
	}
	catch (IllegalArgumentException ex) {
		// Expected..
	}
}
 
Example #20
Source File: HttpRange.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example #21
Source File: ResourceRegionHttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected MediaType getDefaultContentType(Object object) {
	if (jafPresent) {
		if (object instanceof ResourceRegion) {
			return ActivationMediaTypeFactory.getMediaType(((ResourceRegion) object).getResource());
		}
		else {
			Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
			if (!regions.isEmpty()) {
				return ActivationMediaTypeFactory.getMediaType(regions.iterator().next().getResource());
			}
		}
	}
	return MediaType.APPLICATION_OCTET_STREAM;
}
 
Example #22
Source File: ResourceRegionHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public boolean canWrite(@Nullable Type type, @Nullable Class<?> clazz, @Nullable MediaType mediaType) {
	if (!(type instanceof ParameterizedType)) {
		return (type instanceof Class && ResourceRegion.class.isAssignableFrom((Class<?>) type));
	}

	ParameterizedType parameterizedType = (ParameterizedType) type;
	if (!(parameterizedType.getRawType() instanceof Class)) {
		return false;
	}
	Class<?> rawType = (Class<?>) parameterizedType.getRawType();
	if (!(Collection.class.isAssignableFrom(rawType))) {
		return false;
	}
	if (parameterizedType.getActualTypeArguments().length != 1) {
		return false;
	}
	Type typeArgument = parameterizedType.getActualTypeArguments()[0];
	if (!(typeArgument instanceof Class)) {
		return false;
	}

	Class<?> typeArgumentClass = (Class<?>) typeArgument;
	return ResourceRegion.class.isAssignableFrom(typeArgumentClass);
}
 
Example #23
Source File: ResourceHttpMessageWriter.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Mono<Void> writeSingleRegion(ResourceRegion region, ReactiveHttpOutputMessage message,
		Map<String, Object> hints) {

	return zeroCopy(region.getResource(), region, message, hints)
			.orElseGet(() -> {
				Publisher<? extends ResourceRegion> input = Mono.just(region);
				MediaType mediaType = message.getHeaders().getContentType();
				return encodeAndWriteRegions(input, mediaType, message, hints);
			});
}
 
Example #24
Source File: HttpRange.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Turn a {@code Resource} into a {@link ResourceRegion} using the range
 * information contained in the current {@code HttpRange}.
 * @param resource the {@code Resource} to select the region from
 * @return the selected region of the given {@code Resource}
 * @since 4.3
 */
public ResourceRegion toResourceRegion(Resource resource) {
	// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
	// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
	Assert.isTrue(resource.getClass() != InputStreamResource.class,
			"Cannot convert an InputStreamResource to a ResourceRegion");
	long contentLength = getLengthFor(resource);
	long start = getRangeStart(contentLength);
	long end = getRangeEnd(contentLength);
	return new ResourceRegion(resource, start, end - start + 1);
}
 
Example #25
Source File: ResourceRegionHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void canWriteResourceCollection() {
	Type resourceRegionList = new ParameterizedTypeReference<List<ResourceRegion>>() {}.getType();
	assertTrue(converter.canWrite(resourceRegionList, null, MediaType.APPLICATION_OCTET_STREAM));
	assertTrue(converter.canWrite(resourceRegionList, null, MediaType.ALL));

	assertFalse(converter.canWrite(List.class, MediaType.APPLICATION_OCTET_STREAM));
	assertFalse(converter.canWrite(List.class, MediaType.ALL));
	Type resourceObjectList = new ParameterizedTypeReference<List<Object>>() {}.getType();
	assertFalse(converter.canWrite(resourceObjectList, null, MediaType.ALL));
}
 
Example #26
Source File: HttpRange.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 * @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<ResourceRegion>(ranges.size());
	for (HttpRange range : ranges) {
		regions.add(range.toResourceRegion(resource));
	}
	return regions;
}
 
Example #27
Source File: ResourceRegionHttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected MediaType getDefaultContentType(Object object) {
	Resource resource = null;
	if (object instanceof ResourceRegion) {
		resource = ((ResourceRegion) object).getResource();
	}
	else {
		Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
		if (!regions.isEmpty()) {
			resource = regions.iterator().next().getResource();
		}
	}
	return MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM);
}
 
Example #28
Source File: ResourceRegionEncoderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void shouldEncodeMultipleResourceRegionsFileResource() throws Exception {
	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> result = this.encoder.encode(regions, this.bufferFactory,
			ResolvableType.forClass(ResourceRegion.class),
			MimeType.valueOf("text/plain"),
			Collections.singletonMap(ResourceRegionEncoder.BOUNDARY_STRING_HINT, boundary)
	);

	StepVerifier.create(result)
			.consumeNextWith(stringConsumer("\r\n--" + boundary + "\r\n"))
			.consumeNextWith(stringConsumer("Content-Type: text/plain\r\n"))
			.consumeNextWith(stringConsumer("Content-Range: bytes 0-5/39\r\n\r\n"))
			.consumeNextWith(stringConsumer("Spring"))
			.consumeNextWith(stringConsumer("\r\n--" + boundary + "\r\n"))
			.consumeNextWith(stringConsumer("Content-Type: text/plain\r\n"))
			.consumeNextWith(stringConsumer("Content-Range: bytes 7-15/39\r\n\r\n"))
			.consumeNextWith(stringConsumer("Framework"))
			.consumeNextWith(stringConsumer("\r\n--" + boundary + "\r\n"))
			.consumeNextWith(stringConsumer("Content-Type: text/plain\r\n"))
			.consumeNextWith(stringConsumer("Content-Range: bytes 17-20/39\r\n\r\n"))
			.consumeNextWith(stringConsumer("test"))
			.consumeNextWith(stringConsumer("\r\n--" + boundary + "\r\n"))
			.consumeNextWith(stringConsumer("Content-Type: text/plain\r\n"))
			.consumeNextWith(stringConsumer("Content-Range: bytes 22-38/39\r\n\r\n"))
			.consumeNextWith(stringConsumer("resource content."))
			.consumeNextWith(stringConsumer("\r\n--" + boundary + "--"))
			.expectComplete()
			.verify();
}
 
Example #29
Source File: ResourceHttpMessageWriter.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Mono<Void> encodeAndWriteRegions(Publisher<? extends ResourceRegion> publisher,
		@Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {

	Flux<DataBuffer> body = this.regionEncoder.encode(
			publisher, message.bufferFactory(), REGION_TYPE, mediaType, hints);

	return message.writeWith(body);
}
 
Example #30
Source File: ResourceRegionEncoderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public 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();
}