use of cn.taketoday.core.ResolvableType in project today-infrastructure by TAKETODAY.
the class ConfigDataLoaders method getResourceType.
private Class<?> getResourceType(ConfigDataLoader<?> loader) {
ResolvableType resolvableType = ResolvableType.fromClass(loader.getClass());
Class<?> resourceType = resolvableType.as(ConfigDataLoader.class).resolveGeneric();
if (resourceType == null) {
throw new IllegalStateException("Cannot determine resource type for " + loader.getClass());
}
return resourceType;
}
use of cn.taketoday.core.ResolvableType in project today-infrastructure by TAKETODAY.
the class ServerSentEventHttpMessageReader method read.
@Override
public Flux<Object> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
LimitTracker limitTracker = new LimitTracker();
boolean shouldWrap = isServerSentEvent(elementType);
ResolvableType valueType = (shouldWrap ? elementType.getGeneric() : elementType);
return this.lineDecoder.decode(message.getBody(), STRING_TYPE, null, hints).doOnNext(limitTracker::afterLineParsed).bufferUntil(String::isEmpty).concatMap(lines -> {
Object event = buildEvent(lines, valueType, shouldWrap, hints);
return (event != null ? Mono.just(event) : Mono.empty());
});
}
use of cn.taketoday.core.ResolvableType in project today-infrastructure by TAKETODAY.
the class MultipartHttpMessageWriter method encodePart.
@SuppressWarnings("unchecked")
private <T> Flux<DataBuffer> encodePart(byte[] boundary, String name, T value, DataBufferFactory factory) {
MultipartHttpOutputMessage message = new MultipartHttpOutputMessage(factory);
HttpHeaders headers = message.getHeaders();
T body;
ResolvableType resolvableType = null;
if (value instanceof HttpEntity) {
HttpEntity<T> httpEntity = (HttpEntity<T>) value;
headers.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.fromClass(body.getClass());
}
if (!headers.containsKey(HttpHeaders.CONTENT_DISPOSITION)) {
if (body instanceof Resource) {
headers.setContentDispositionFormData(name, ((Resource) body).getName());
} else if (resolvableType.resolve() == Resource.class) {
body = (T) Mono.from((Publisher<?>) body).doOnNext(o -> headers.setContentDispositionFormData(name, ((Resource) o).getName()));
} else {
headers.setContentDispositionFormData(name, null);
}
}
MediaType contentType = headers.getContentType();
final ResolvableType finalBodyType = resolvableType;
Optional<HttpMessageWriter<?>> writer = this.partWriters.stream().filter(partWriter -> partWriter.canWrite(finalBodyType, contentType)).findFirst();
if (writer.isEmpty()) {
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, message, 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(message::getBody));
return Flux.concat(generateBoundaryLine(boundary, factory), partContent, generateNewLine(factory));
}
use of cn.taketoday.core.ResolvableType in project today-infrastructure by TAKETODAY.
the class ValueObjectBinderTests method bindToClassShouldBindWithGenerics.
@Test
void bindToClassShouldBindWithGenerics() {
// gh-19156
ResolvableType type = ResolvableType.fromClassWithGenerics(Map.class, String.class, String.class);
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.value.bar", "baz");
this.sources.add(source);
GenericValue<Map<String, String>> bean = this.binder.bind("foo", Bindable.<GenericValue<Map<String, String>>>of(ResolvableType.fromClassWithGenerics(GenericValue.class, type))).get();
assertThat(bean.getValue().get("bar")).isEqualTo("baz");
}
use of cn.taketoday.core.ResolvableType in project today-infrastructure by TAKETODAY.
the class MapBinderTests method bindToMapShouldRespectMapType.
@Test
void bindToMapShouldRespectMapType() {
this.sources.add(new MockConfigurationPropertySource("foo.bar", "1"));
ResolvableType type = ResolvableType.fromClassWithGenerics(HashMap.class, String.class, Integer.class);
Object defaultMap = this.binder.bind("foo", STRING_INTEGER_MAP).get();
Object customMap = this.binder.bind("foo", Bindable.of(type)).get();
assertThat(customMap).isExactlyInstanceOf(HashMap.class).isNotInstanceOf(defaultMap.getClass());
}
Aggregations