@Override public Mono<Void> insert(ClientHttpRequest outputMessage, Context context) { HttpMessageWriter<MultiValueMap<String, HttpEntity<?>>> messageWriter = findWriter(context, MULTIPART_DATA_TYPE, MediaType.MULTIPART_FORM_DATA); MultiValueMap<String, HttpEntity<?>> body = this.builder.build(); return messageWriter.write(Mono.just(body), MULTIPART_DATA_TYPE, MediaType.MULTIPART_FORM_DATA, outputMessage, context.hints()); } }
private MultiValueMap<String, HttpEntity<?>> getMultipartBody() { MultipartBodyBuilder multipartBodyBuilder = new MultipartBodyBuilder(); multiParts.stream().forEach(multipart -> multipartBodyBuilder.part(multipart.getFileName(), multipart.getContentBody())); return multipartBodyBuilder.build(); }
private MultiValueMap<String, HttpEntity<?>> generateBody() { MultipartBodyBuilder builder = new MultipartBodyBuilder(); builder.part("fooPart", new ClassPathResource("org/springframework/http/codec/multipart/foo.txt")); builder.part("barPart", "bar"); return builder.build(); }
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(); }
@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(); }
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); }
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()); }
@Test // SPR-16601 public void publisherEntityAcceptedAsInput() { Publisher<String> publisher = Flux.just("foo", "bar", "baz"); MultipartBodyBuilder builder = new MultipartBodyBuilder(); builder.asyncPart("publisherClass", publisher, String.class).header("baz", "qux"); HttpEntity<?> entity = builder.build().getFirst("publisherClass"); assertNotNull(entity); assertEquals(PublisherEntity.class, entity.getClass()); // Now build a new MultipartBodyBuilder, as BodyInserters.fromMultipartData would do... builder = new MultipartBodyBuilder(); builder.part("publisherClass", entity); entity = builder.build().getFirst("publisherClass"); assertNotNull(entity); assertEquals(PublisherEntity.class, entity.getClass()); }
@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()); }
@Test // SPR-16376 public void customContentDisposition() throws IOException { Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); Flux<DataBuffer> buffers = DataBufferUtils.read(logo, new DefaultDataBufferFactory(), 1024); long contentLength = logo.contentLength(); MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); bodyBuilder.part("resource", logo) .headers(h -> h.setContentDispositionFormData("resource", "spring.jpg")); bodyBuilder.asyncPart("buffers", buffers, DataBuffer.class) .headers(h -> { h.setContentDispositionFormData("buffers", "buffers.jpg"); h.setContentType(MediaType.IMAGE_JPEG); h.setContentLength(contentLength); }); MultiValueMap<String, HttpEntity<?>> multipartData = bodyBuilder.build(); Map<String, Object> hints = Collections.emptyMap(); this.writer.write(Mono.just(multipartData), null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block(); MultiValueMap<String, Part> requestParts = parse(hints); assertEquals(2, requestParts.size()); Part part = requestParts.getFirst("resource"); assertTrue(part instanceof FilePart); assertEquals("spring.jpg", ((FilePart) part).filename()); assertEquals(logo.getFile().length(), part.headers().getContentLength()); part = requestParts.getFirst("buffers"); assertTrue(part instanceof FilePart); assertEquals("buffers.jpg", ((FilePart) part).filename()); assertEquals(logo.getFile().length(), part.headers().getContentLength()); }
bodyBuilder.part("json", new Foo("bar"), MediaType.APPLICATION_JSON_UTF8); bodyBuilder.asyncPart("publisher", publisher, String.class); Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
builder.asyncPart("publisherPtr", publisher, new ParameterizedTypeReference<String>() {}).header("baz", "qux"); MultiValueMap<String, HttpEntity<?>> result = builder.build();
private MultiValueMap<String, HttpEntity<?>> generateBody() { MultipartBodyBuilder builder = new MultipartBodyBuilder(); builder.part("verticesFile", new ClassPathResource("vertices_simple.txt")); builder.part("edgesFile", new ClassPathResource("edges_simple.txt")); builder.part("verticesTopic", "initial-vertices"); builder.part("edgesTopic", "initial-edges"); builder.part("useDouble", "false"); builder.part("numPartitions", "50"); builder.part("replicationFactor", "1"); return builder.build(); } }