@Override public <T, P extends Publisher<T>> MultipartInserter withPublisher( String name, P publisher, ParameterizedTypeReference<T> typeReference) { this.builder.asyncPart(name, publisher, typeReference); return this; }
@Override public <T, P extends Publisher<T>> MultipartInserter withPublisher( String name, P publisher, Class<T> elementClass) { this.builder.asyncPart(name, publisher, elementClass); return this; }
@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(); }
@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-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-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("utf8", utf8); 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());