org.springframework.http.client.MultipartBodyBuilder Java Examples

The following examples show how to use org.springframework.http.client.MultipartBodyBuilder. 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: RestSteps.java    From cucumber-rest-steps with 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 #2
Source File: MultipartHttpMessageWriterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
Source File: MultipartHttpMessageWriterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 File: MultipartHttpMessageWriterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #5
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #6
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #7
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #8
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #9
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #10
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #11
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #12
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #13
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #14
Source File: MultipartIntegrationTests.java    From spring-analysis-note with 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 #15
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #16
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #17
Source File: RequestPartMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 #18
Source File: SynchronossPartHttpMessageReaderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 #19
Source File: MultipartHttpMessageWriterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());

	Map<String, Object> hints = Collections.emptyMap();
	this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block();
}
 
Example #20
Source File: GraphIntegrationTest.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
private MultiValueMap<String, HttpEntity<?>> generateCCBody() {
    MultipartBodyBuilder builder = new MultipartBodyBuilder();
    builder.part("verticesTopic", "initial-cc-vertices");
    builder.part("edgesTopic", "initial-cc-edges");
    builder.part("vertexFile", new ClassPathResource("vertices_simple.txt"));
    builder.part("edgeFile", new ClassPathResource("edges_simple.txt"));
    builder.part("vertexParser", VertexLongIdLongValueParser.class.getName());
    builder.part("edgeParser", EdgeLongIdLongValueParser.class.getName());
    builder.part("keySerializer", LongSerializer.class.getName());
    builder.part("vertexValueSerializer", LongSerializer.class.getName());
    builder.part("edgeValueSerializer", LongSerializer.class.getName());
    builder.part("numPartitions", "50");
    builder.part("replicationFactor", "1");
    return builder.build();
}
 
Example #21
Source File: GraphIntegrationTest.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
private MultiValueMap<String, HttpEntity<?>> generateSvdppBody() {
    MultipartBodyBuilder builder = new MultipartBodyBuilder();
    builder.part("edgesTopic", "initial-svdpp-edges");
    builder.part("edgeFile", new ClassPathResource("ratings_simple.txt"));
    builder.part("edgeParser", EdgeCfLongIdFloatValueParser.class.getName());
    builder.part("edgeValueSerializer", FloatSerializer.class.getName());
    builder.part("numPartitions", "50");
    builder.part("replicationFactor", "1");
    return builder.build();
}
 
Example #22
Source File: MultipartIntegrationTests.java    From java-technology-stack with 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 #23
Source File: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #24
Source File: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #25
Source File: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #26
Source File: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #27
Source File: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #28
Source File: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #29
Source File: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #30
Source File: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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();
}