org.springframework.core.ResolvableTypeProvider Java Examples
The following examples show how to use
org.springframework.core.ResolvableTypeProvider.
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: MultipartHttpMessageWriter.java From spring-analysis-note with MIT License | 4 votes |
@SuppressWarnings("unchecked") private <T> Flux<DataBuffer> encodePart(byte[] boundary, String name, T value, DataBufferFactory bufferFactory) { MultipartHttpOutputMessage outputMessage = new MultipartHttpOutputMessage(bufferFactory, getCharset()); HttpHeaders outputHeaders = outputMessage.getHeaders(); T body; ResolvableType resolvableType = null; if (value instanceof HttpEntity) { HttpEntity<T> httpEntity = (HttpEntity<T>) value; outputHeaders.putAll(httpEntity.getHeaders()); body = httpEntity.getBody(); Assert.state(body != null, "MultipartHttpMessageWriter only supports HttpEntity with body"); if (httpEntity instanceof ResolvableTypeProvider) { resolvableType = ((ResolvableTypeProvider) httpEntity).getResolvableType(); } } else { body = value; } if (resolvableType == null) { resolvableType = ResolvableType.forClass(body.getClass()); } if (!outputHeaders.containsKey(HttpHeaders.CONTENT_DISPOSITION)) { if (body instanceof Resource) { outputHeaders.setContentDispositionFormData(name, ((Resource) body).getFilename()); } else if (resolvableType.resolve() == Resource.class) { body = (T) Mono.from((Publisher<?>) body).doOnNext(o -> outputHeaders .setContentDispositionFormData(name, ((Resource) o).getFilename())); } else { outputHeaders.setContentDispositionFormData(name, null); } } MediaType contentType = outputHeaders.getContentType(); final ResolvableType finalBodyType = resolvableType; Optional<HttpMessageWriter<?>> writer = this.partWriters.stream() .filter(partWriter -> partWriter.canWrite(finalBodyType, contentType)) .findFirst(); if (!writer.isPresent()) { return Flux.error(new CodecException("No suitable writer found for part: " + name)); } Publisher<T> bodyPublisher = body instanceof Publisher ? (Publisher<T>) body : Mono.just(body); // The writer will call MultipartHttpOutputMessage#write which doesn't actually write // but only stores the body Flux and returns Mono.empty(). Mono<Void> partContentReady = ((HttpMessageWriter<T>) writer.get()) .write(bodyPublisher, resolvableType, contentType, outputMessage, DEFAULT_HINTS); // After partContentReady, we can access the part content from MultipartHttpOutputMessage // and use it for writing to the actual request body Flux<DataBuffer> partContent = partContentReady.thenMany(Flux.defer(outputMessage::getBody)); return Flux.concat( generateBoundaryLine(boundary, bufferFactory), partContent, generateNewLine(bufferFactory)); }