Search in sources :

Example 16 with ResolvableType

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;
}
Also used : ResolvableType(cn.taketoday.core.ResolvableType)

Example 17 with ResolvableType

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());
    });
}
Also used : ResolvableType(cn.taketoday.core.ResolvableType)

Example 18 with ResolvableType

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));
}
Also used : Arrays(java.util.Arrays) Assert(cn.taketoday.lang.Assert) ResolvableTypeProvider(cn.taketoday.core.ResolvableTypeProvider) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Resource(cn.taketoday.core.io.Resource) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CodecException(cn.taketoday.core.codec.CodecException) LogFormatUtils(cn.taketoday.util.LogFormatUtils) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HttpHeaders(cn.taketoday.http.HttpHeaders) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) Hints(cn.taketoday.core.codec.Hints) Map(java.util.Map) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) Publisher(org.reactivestreams.Publisher) ResolvableType(cn.taketoday.core.ResolvableType) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) Mono(reactor.core.publisher.Mono) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Flux(reactor.core.publisher.Flux) List(java.util.List) Nullable(cn.taketoday.lang.Nullable) Optional(java.util.Optional) HttpEntity(cn.taketoday.http.HttpEntity) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) HttpHeaders(cn.taketoday.http.HttpHeaders) HttpEntity(cn.taketoday.http.HttpEntity) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Resource(cn.taketoday.core.io.Resource) Publisher(org.reactivestreams.Publisher) MediaType(cn.taketoday.http.MediaType) CodecException(cn.taketoday.core.codec.CodecException) ResolvableType(cn.taketoday.core.ResolvableType) ResolvableTypeProvider(cn.taketoday.core.ResolvableTypeProvider) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Example 19 with ResolvableType

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");
}
Also used : MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) ResolvableType(cn.taketoday.core.ResolvableType) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 20 with ResolvableType

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());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) ResolvableType(cn.taketoday.core.ResolvableType) Test(org.junit.jupiter.api.Test)

Aggregations

ResolvableType (cn.taketoday.core.ResolvableType)159 Test (org.junit.jupiter.api.Test)106 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)30 Map (java.util.Map)24 Pojo (cn.taketoday.http.codec.Pojo)20 List (java.util.List)16 MediaType (cn.taketoday.http.MediaType)15 MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)14 Flux (reactor.core.publisher.Flux)12 Mono (reactor.core.publisher.Mono)10 Resource (cn.taketoday.core.io.Resource)8 Nullable (cn.taketoday.lang.Nullable)8 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 MultiValueMap (cn.taketoday.core.MultiValueMap)6 ReactiveHttpOutputMessage (cn.taketoday.http.ReactiveHttpOutputMessage)6 ServerSentEvent (cn.taketoday.http.codec.ServerSentEvent)6 MimeType (cn.taketoday.util.MimeType)6 Arrays (java.util.Arrays)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5