Java Code Examples for org.springframework.util.MimeTypeUtils#generateMultipartBoundaryString()

The following examples show how to use org.springframework.util.MimeTypeUtils#generateMultipartBoundaryString() . 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: 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 2
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 3
Source File: ResourceRegionEncoderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void shouldEncodeMultipleResourceRegionsFileResource() {
	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 4
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 5
Source File: MultipartBatchHttpMessageConverter.java    From documentum-rest-client-java with Apache License 2.0 5 votes vote down vote up
@Override
public void write(MultiValueMap<String, HttpEntity<InputStream>> parts, MediaType contentType, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    String boundary = MimeTypeUtils.generateMultipartBoundaryString();
    Map<String, String> params = new HashMap<>(contentType.getParameters());
    params.put("boundary", boundary);
    MediaType mediaType = new MediaType(contentType, params);
    outputMessage.getHeaders().setContentType(mediaType);
    writeParts(outputMessage.getBody(), parts, boundary);
}
 
Example 6
Source File: ResourceRegionHttpMessageConverter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions,
		HttpOutputMessage outputMessage) throws IOException {

	Assert.notNull(resourceRegions, "Collection of ResourceRegion should not be null");
	HttpHeaders responseHeaders = outputMessage.getHeaders();

	MediaType contentType = responseHeaders.getContentType();
	String boundaryString = MimeTypeUtils.generateMultipartBoundaryString();
	responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString);
	OutputStream out = outputMessage.getBody();

	for (ResourceRegion region : resourceRegions) {
		long start = region.getPosition();
		long end = start + region.getCount() - 1;
		InputStream in = region.getResource().getInputStream();
		try {
			// Writing MIME header.
			println(out);
			print(out, "--" + boundaryString);
			println(out);
			if (contentType != null) {
				print(out, "Content-Type: " + contentType.toString());
				println(out);
			}
			Long resourceLength = region.getResource().contentLength();
			end = Math.min(end, resourceLength - 1);
			print(out, "Content-Range: bytes " + start + '-' + end + '/' + resourceLength);
			println(out);
			println(out);
			// Printing content
			StreamUtils.copyRange(in, out, start, end);
		}
		finally {
			try {
				in.close();
			}
			catch (IOException ex) {
				// ignore
			}
		}
	}

	println(out);
	print(out, "--" + boundaryString + "--");
}
 
Example 7
Source File: ResourceRegionHttpMessageConverter.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions,
		HttpOutputMessage outputMessage) throws IOException {

	Assert.notNull(resourceRegions, "Collection of ResourceRegion should not be null");
	HttpHeaders responseHeaders = outputMessage.getHeaders();

	MediaType contentType = responseHeaders.getContentType();
	String boundaryString = MimeTypeUtils.generateMultipartBoundaryString();
	responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString);
	OutputStream out = outputMessage.getBody();

	for (ResourceRegion region : resourceRegions) {
		long start = region.getPosition();
		long end = start + region.getCount() - 1;
		InputStream in = region.getResource().getInputStream();
		try {
			// Writing MIME header.
			println(out);
			print(out, "--" + boundaryString);
			println(out);
			if (contentType != null) {
				print(out, "Content-Type: " + contentType.toString());
				println(out);
			}
			Long resourceLength = region.getResource().contentLength();
			end = Math.min(end, resourceLength - 1);
			print(out, "Content-Range: bytes " + start + '-' + end + '/' + resourceLength);
			println(out);
			println(out);
			// Printing content
			StreamUtils.copyRange(in, out, start, end);
		}
		finally {
			try {
				in.close();
			}
			catch (IOException ex) {
				// ignore
			}
		}
	}

	println(out);
	print(out, "--" + boundaryString + "--");
}
 
Example 8
Source File: ResourceRegionHttpMessageConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions,
		HttpOutputMessage outputMessage) throws IOException {

	Assert.notNull(resourceRegions, "Collection of ResourceRegion should not be null");
	HttpHeaders responseHeaders = outputMessage.getHeaders();

	MediaType contentType = responseHeaders.getContentType();
	String boundaryString = MimeTypeUtils.generateMultipartBoundaryString();
	responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString);
	OutputStream out = outputMessage.getBody();

	for (ResourceRegion region : resourceRegions) {
		long start = region.getPosition();
		long end = start + region.getCount() - 1;
		InputStream in = region.getResource().getInputStream();
		try {
			// Writing MIME header.
			println(out);
			print(out, "--" + boundaryString);
			println(out);
			if (contentType != null) {
				print(out, "Content-Type: " + contentType.toString());
				println(out);
			}
			Long resourceLength = region.getResource().contentLength();
			end = Math.min(end, resourceLength - 1);
			print(out, "Content-Range: bytes " + start + '-' + end + '/' + resourceLength);
			println(out);
			println(out);
			// Printing content
			StreamUtils.copyRange(in, out, start, end);
		}
		finally {
			try {
				in.close();
			}
			catch (IOException ex) {
				// ignore
			}
		}
	}

	println(out);
	print(out, "--" + boundaryString + "--");
}
 
Example 9
Source File: RangeAwareResourceRegionHttpMessageConverter.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
protected void writeResourceRegionCollection(Collection<ResourceRegion> resourceRegions,
                                             HttpOutputMessage outputMessage) throws IOException {
    Assert.notNull(resourceRegions, "Collection of ResourceRegion should not be null");
    HttpHeaders responseHeaders = outputMessage.getHeaders();

    MediaType contentType = responseHeaders.getContentType();
    String boundaryString = MimeTypeUtils.generateMultipartBoundaryString();
    responseHeaders.set(HttpHeaders.CONTENT_TYPE, "multipart/byteranges; boundary=" + boundaryString);
    OutputStream out = outputMessage.getBody();

    for (ResourceRegion region : resourceRegions) {
        long start = region.getPosition();
        long end = start + region.getCount() - 1;
        InputStream in = null;
        try {
            // Writing MIME header.
            println(out);
            print(out, "--" + boundaryString);
            println(out);
            if (contentType != null) {
                print(out, "Content-Type: " + contentType.toString());
                println(out);
            }
            Long resourceLength = region.getResource().contentLength();
            end = Math.min(end, resourceLength - 1);
            print(out, "Content-Range: bytes " + start + '-' + end + '/' + resourceLength);
            println(out);
            println(out);

            // Printing content
            Resource resource = region.getResource();
            if (resource instanceof RangeAwareResource) {
                in = ((RangeAwareResource) resource).getInputStream(start, end);
                StreamUtils.copy(in, out);
            } else {
                in = resource.getInputStream();
                StreamUtils.copyRange(in, out, start, end);
            }
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    println(out);
    print(out, "--" + boundaryString + "--");
}