Java Code Examples for org.springframework.http.client.MultipartBodyBuilder

The following examples show how to use org.springframework.http.client.MultipartBodyBuilder. These examples are extracted from open source projects. 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
@Test  // SPR-16402
public void singleSubscriberWithResource() throws IOException {
	UnicastProcessor<Resource> processor = UnicastProcessor.create();
	Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
	Mono.just(logo).subscribe(processor);

	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.asyncPart("logo", processor, Resource.class);

	Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());

	Map<String, Object> hints = Collections.emptyMap();
	this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block();

	MultiValueMap<String, Part> requestParts = parse(hints);
	assertEquals(1, requestParts.size());

	Part part = requestParts.getFirst("logo");
	assertEquals("logo", part.name());
	assertTrue(part instanceof FilePart);
	assertEquals("logo.jpg", ((FilePart) part).filename());
	assertEquals(MediaType.IMAGE_JPEG, part.headers().getContentType());
	assertEquals(logo.getFile().length(), part.headers().getContentLength());
}
 
Example 2
@Test // SPR-16402
public void singleSubscriberWithStrings() {
	UnicastProcessor<String> processor = UnicastProcessor.create();
	Flux.just("foo", "bar", "baz").subscribe(processor);

	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.asyncPart("name", processor, String.class);

	Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());

	this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, Collections.emptyMap())
			.block(Duration.ofSeconds(5));

	// Make sure body is consumed to avoid leak reports
	this.response.getBodyAsString().block(Duration.ofSeconds(5));
}
 
Example 3
@Test // SPR-16402
public void singleSubscriberWithResource() throws IOException {
	UnicastProcessor<Resource> processor = UnicastProcessor.create();
	Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
	Mono.just(logo).subscribe(processor);

	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.asyncPart("logo", processor, Resource.class);

	Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());

	Map<String, Object> hints = Collections.emptyMap();
	this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block();

	MultiValueMap<String, Part> requestParts = parse(hints);
	assertEquals(1, requestParts.size());

	Part part = requestParts.getFirst("logo");
	assertEquals("logo", part.name());
	assertTrue(part instanceof FilePart);
	assertEquals("logo.jpg", ((FilePart) part).filename());
	assertEquals(MediaType.IMAGE_JPEG, part.headers().getContentType());
	assertEquals(logo.getFile().length(), part.headers().getContentLength());
}
 
Example 4
Source Project: cucumber-rest-steps   Source File: RestSteps.java    License: MIT License 6 votes vote down vote up
private void callWithFile(String httpMethodString, String path, String file, Resource resource) {
  HttpMethod httpMethod = HttpMethod.valueOf(httpMethodString.toUpperCase());

  if (httpMethod.equals(HttpMethod.GET)) {
    throw new IllegalArgumentException("You can't submit a file in a GET call");
  }

  MultipartBodyBuilder builder = new MultipartBodyBuilder();
  builder.part(file, resource);

  responseSpec = webClient.method(httpMethod)
      .uri(path)
      .syncBody(builder.build())
      .exchange();
  expectBody = null;
}
 
Example 5
Source Project: spring-analysis-note   Source File: MultipartIntegrationTests.java    License: MIT License 5 votes vote down vote up
private MultiValueMap<String, HttpEntity<?>> generateBody() {
	MultipartBodyBuilder builder = new MultipartBodyBuilder();
	builder.part("fieldPart", "fieldValue");
	builder.part("fileParts", new ClassPathResource("foo.txt", MultipartHttpMessageReader.class));
	builder.part("fileParts", new ClassPathResource("logo.png", getClass()));
	builder.part("jsonPart", new Person("Jason"));
	return builder.build();
}
 
Example 6
@Test
public void person() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Person.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	Person actual = resolveArgument(param, bodyBuilder);

	assertEquals("Jones", actual.getName());
}
 
Example 7
@Test
public void listPerson() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(List.class, Person.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	bodyBuilder.part("name", new Person("James"));
	List<Person> actual = resolveArgument(param, bodyBuilder);

	assertEquals("Jones", actual.get(0).getName());
	assertEquals("James", actual.get(1).getName());
}
 
Example 8
@Test
public void monoPerson() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Mono.class, Person.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	Mono<Person> actual = resolveArgument(param, bodyBuilder);

	assertEquals("Jones", actual.block().getName());
}
 
Example 9
@Test
public void fluxPerson() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Flux.class, Person.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	bodyBuilder.part("name", new Person("James"));
	Flux<Person> actual = resolveArgument(param, bodyBuilder);

	List<Person> persons = actual.collectList().block();
	assertEquals("Jones", persons.get(0).getName());
	assertEquals("James", persons.get(1).getName());
}
 
Example 10
@Test
public void part() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Part.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	Part actual = resolveArgument(param, bodyBuilder);

	DataBuffer buffer = DataBufferUtils.join(actual.content()).block();
	assertEquals("{\"name\":\"Jones\"}", DataBufferTestUtils.dumpString(buffer, StandardCharsets.UTF_8));
}
 
Example 11
@Test
public void listPart() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(List.class, Part.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	bodyBuilder.part("name", new Person("James"));
	List<Part> actual = resolveArgument(param, bodyBuilder);

	assertEquals("{\"name\":\"Jones\"}", partToUtf8String(actual.get(0)));
	assertEquals("{\"name\":\"James\"}", partToUtf8String(actual.get(1)));
}
 
Example 12
@Test
public void fluxPart() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Flux.class, Part.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	bodyBuilder.part("name", new Person("James"));
	Flux<Part> actual = resolveArgument(param, bodyBuilder);

	List<Part> parts = actual.collectList().block();
	assertEquals("{\"name\":\"Jones\"}", partToUtf8String(parts.get(0)));
	assertEquals("{\"name\":\"James\"}", partToUtf8String(parts.get(1)));
}
 
Example 13
@Test
public void personRequired() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Person.class);
	ServerWebExchange exchange = createExchange(new MultipartBodyBuilder());
	Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange);

	StepVerifier.create(result).expectError(ServerWebInputException.class).verify();
}
 
Example 14
@Test
public void personNotRequired() {
	MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(Person.class);
	ServerWebExchange exchange = createExchange(new MultipartBodyBuilder());
	Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange);

	StepVerifier.create(result).verifyComplete();
}
 
Example 15
@Test
public void partRequired() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Part.class);
	ServerWebExchange exchange = createExchange(new MultipartBodyBuilder());
	Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange);

	StepVerifier.create(result).expectError(ServerWebInputException.class).verify();
}
 
Example 16
@Test
public void partNotRequired() {
	MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(Part.class);
	ServerWebExchange exchange = createExchange(new MultipartBodyBuilder());
	Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange);

	StepVerifier.create(result).verifyComplete();
}
 
Example 17
@SuppressWarnings("unchecked")
private <T> T resolveArgument(MethodParameter param, MultipartBodyBuilder builder) {
	ServerWebExchange exchange = createExchange(builder);
	Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange);
	Object value = result.block(Duration.ofSeconds(5));

	assertNotNull(value);
	assertTrue(param.getParameterType().isAssignableFrom(value.getClass()));
	return (T) value;
}
 
Example 18
private ServerWebExchange createExchange(MultipartBodyBuilder builder) {
	MockClientHttpRequest clientRequest = new MockClientHttpRequest(HttpMethod.POST, "/");
	this.writer.write(Mono.just(builder.build()), forClass(MultiValueMap.class),
			MediaType.MULTIPART_FORM_DATA, clientRequest, Collections.emptyMap()).block();

	MockServerHttpRequest serverRequest = MockServerHttpRequest.post("/")
			.contentType(clientRequest.getHeaders().getContentType())
			.body(clientRequest.getBody());

	return MockServerWebExchange.from(serverRequest);
}
 
Example 19
@Test // gh-22107
public void cancelWithMultipartContent() {
	MultipartBodyBuilder builder = new MultipartBodyBuilder();
	builder.part("part1", "value1");
	builder.part("part2", "value2");

	List<HttpMessageWriter<?>> writers = ClientCodecConfigurer.create().getWriters();
	MultipartHttpMessageWriter writer = new MultipartHttpMessageWriter(writers);
	CancellingOutputMessage outputMessage = new CancellingOutputMessage(this.bufferFactory);

	writer.write(Mono.just(builder.build()), null, MediaType.MULTIPART_FORM_DATA,
			outputMessage, Collections.emptyMap()).block(Duration.ofSeconds(5));
}
 
Example 20
private ServerHttpRequest generateMultipartRequest() {

		MultipartBodyBuilder partsBuilder = new MultipartBodyBuilder();
		partsBuilder.part("fooPart", new ClassPathResource("org/springframework/http/codec/multipart/foo.txt"));
		partsBuilder.part("barPart", "bar");

		MockClientHttpRequest outputMessage = new MockClientHttpRequest(HttpMethod.POST, "/");
		new MultipartHttpMessageWriter()
				.write(Mono.just(partsBuilder.build()), null, MediaType.MULTIPART_FORM_DATA, outputMessage, null)
				.block(Duration.ofSeconds(5));

		return MockServerHttpRequest.post("/")
				.contentType(outputMessage.getHeaders().getContentType())
				.body(outputMessage.getBody());
	}
 
Example 21
Source Project: java-technology-stack   Source File: MultipartIntegrationTests.java    License: MIT License 5 votes vote down vote up
private MultiValueMap<String, HttpEntity<?>> generateBody() {
	MultipartBodyBuilder builder = new MultipartBodyBuilder();
	builder.part("fieldPart", "fieldValue");
	builder.part("fileParts", new ClassPathResource("foo.txt", MultipartHttpMessageReader.class));
	builder.part("fileParts", new ClassPathResource("logo.png", getClass()));
	builder.part("jsonPart", new Person("Jason"));
	return builder.build();
}
 
Example 22
@Test
public void person() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Person.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	Person actual = resolveArgument(param, bodyBuilder);

	assertEquals("Jones", actual.getName());
}
 
Example 23
@Test
public void listPerson() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(List.class, Person.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	bodyBuilder.part("name", new Person("James"));
	List<Person> actual = resolveArgument(param, bodyBuilder);

	assertEquals("Jones", actual.get(0).getName());
	assertEquals("James", actual.get(1).getName());
}
 
Example 24
@Test
public void monoPerson() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Mono.class, Person.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	Mono<Person> actual = resolveArgument(param, bodyBuilder);

	assertEquals("Jones", actual.block().getName());
}
 
Example 25
@Test
public void fluxPerson() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Flux.class, Person.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	bodyBuilder.part("name", new Person("James"));
	Flux<Person> actual = resolveArgument(param, bodyBuilder);

	List<Person> persons = actual.collectList().block();
	assertEquals("Jones", persons.get(0).getName());
	assertEquals("James", persons.get(1).getName());
}
 
Example 26
@Test
public void part() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Part.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	Part actual = resolveArgument(param, bodyBuilder);

	DataBuffer buffer = DataBufferUtils.join(actual.content()).block();
	assertEquals("{\"name\":\"Jones\"}", DataBufferTestUtils.dumpString(buffer, StandardCharsets.UTF_8));
}
 
Example 27
@Test
public void listPart() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(List.class, Part.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	bodyBuilder.part("name", new Person("James"));
	List<Part> actual = resolveArgument(param, bodyBuilder);

	assertEquals("{\"name\":\"Jones\"}", partToUtf8String(actual.get(0)));
	assertEquals("{\"name\":\"James\"}", partToUtf8String(actual.get(1)));
}
 
Example 28
@Test
public void fluxPart() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Flux.class, Part.class);
	MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
	bodyBuilder.part("name", new Person("Jones"));
	bodyBuilder.part("name", new Person("James"));
	Flux<Part> actual = resolveArgument(param, bodyBuilder);

	List<Part> parts = actual.collectList().block();
	assertEquals("{\"name\":\"Jones\"}", partToUtf8String(parts.get(0)));
	assertEquals("{\"name\":\"James\"}", partToUtf8String(parts.get(1)));
}
 
Example 29
@Test
public void personRequired() {
	MethodParameter param = this.testMethod.annot(requestPart()).arg(Person.class);
	ServerWebExchange exchange = createExchange(new MultipartBodyBuilder());
	Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange);

	StepVerifier.create(result).expectError(ServerWebInputException.class).verify();
}
 
Example 30
@Test
public void personNotRequired() {
	MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(Person.class);
	ServerWebExchange exchange = createExchange(new MultipartBodyBuilder());
	Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange);

	StepVerifier.create(result).verifyComplete();
}