Search in sources :

Example 11 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class TypeDescriptor method map.

/**
	 * Create a new type descriptor from a {@link java.util.Map} type.
	 * <p>Useful for converting to typed Maps.
	 * <p>For example, a Map&lt;String, String&gt; could be converted to a Map&lt;Id, EmailAddress&gt;
	 * by converting to a targetType built with this method:
	 * The method call to construct such a TypeDescriptor would look something like:
	 * <pre class="code">
	 * map(Map.class, TypeDescriptor.valueOf(Id.class), TypeDescriptor.valueOf(EmailAddress.class));
	 * </pre>
	 * @param mapType the map type, which must implement {@link Map}
	 * @param keyTypeDescriptor a descriptor for the map's key type, used to convert map keys
	 * @param valueTypeDescriptor the map's value type, used to convert map values
	 * @return the map type descriptor
	 */
public static TypeDescriptor map(Class<?> mapType, TypeDescriptor keyTypeDescriptor, TypeDescriptor valueTypeDescriptor) {
    Assert.notNull(mapType, "Map type must not be null");
    if (!Map.class.isAssignableFrom(mapType)) {
        throw new IllegalArgumentException("Map type must be a [java.util.Map]");
    }
    ResolvableType key = (keyTypeDescriptor != null ? keyTypeDescriptor.resolvableType : null);
    ResolvableType value = (valueTypeDescriptor != null ? valueTypeDescriptor.resolvableType : null);
    return new TypeDescriptor(ResolvableType.forClassWithGenerics(mapType, key, value), null, null);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class MessageMethodArgumentResolver method getPayloadType.

private Class<?> getPayloadType(MethodParameter parameter) {
    Type genericParamType = parameter.getGenericParameterType();
    ResolvableType resolvableType = ResolvableType.forType(genericParamType).as(Message.class);
    return resolvableType.getGeneric(0).resolve(Object.class);
}
Also used : Type(java.lang.reflect.Type) ResolvableType(org.springframework.core.ResolvableType) ResolvableType(org.springframework.core.ResolvableType)

Example 13 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageReader method read.

@Override
public Flux<Object> read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
    boolean hasSseWrapper = ServerSentEvent.class.isAssignableFrom(elementType.getRawClass());
    ResolvableType dataType = (hasSseWrapper ? elementType.getGeneric(0) : elementType);
    return Flux.from(inputMessage.getBody()).concatMap(ServerSentEventHttpMessageReader::splitOnNewline).map(buffer -> {
        CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());
        DataBufferUtils.release(buffer);
        return charBuffer.toString();
    }).bufferUntil(line -> line.equals("\n")).concatMap(rawLines -> {
        String[] lines = rawLines.stream().collect(joining()).split("\\r?\\n");
        ServerSentEvent<Object> event = buildEvent(lines, dataType, hints);
        return (hasSseWrapper ? Mono.just(event) : Mono.justOrEmpty(event.data()));
    }).cast(Object.class);
}
Also used : Decoder(org.springframework.core.codec.Decoder) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) CharBuffer(java.nio.CharBuffer) StringDecoder(org.springframework.core.codec.StringDecoder) MediaType(org.springframework.http.MediaType) Mono(reactor.core.publisher.Mono) MimeTypeUtils(org.springframework.util.MimeTypeUtils) DataBuffer(org.springframework.core.io.buffer.DataBuffer) IntPredicate(java.util.function.IntPredicate) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) CodecException(org.springframework.core.codec.CodecException) Flux(reactor.core.publisher.Flux) List(java.util.List) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Duration(java.time.Duration) Map(java.util.Map) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) Assert(org.springframework.util.Assert) CharBuffer(java.nio.CharBuffer) ResolvableType(org.springframework.core.ResolvableType)

Example 14 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageWriter method applyEncoder.

@SuppressWarnings("unchecked")
private <T> Flux<DataBuffer> applyEncoder(Object data, DataBufferFactory bufferFactory, Map<String, Object> hints) {
    ResolvableType elementType = ResolvableType.forClass(data.getClass());
    Optional<Encoder<?>> encoder = dataEncoders.stream().filter(e -> e.canEncode(elementType, MimeTypeUtils.APPLICATION_JSON)).findFirst();
    return ((Encoder<T>) encoder.orElseThrow(() -> new CodecException("No suitable encoder found!"))).encode(Mono.just((T) data), bufferFactory, elementType, MimeTypeUtils.APPLICATION_JSON, hints).concatWith(encodeString("\n", bufferFactory));
}
Also used : Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) MimeTypeUtils(org.springframework.util.MimeTypeUtils) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) ArrayList(java.util.ArrayList) CodecException(org.springframework.core.codec.CodecException) Flux(reactor.core.publisher.Flux) List(java.util.List) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Map(java.util.Map) Optional(java.util.Optional) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) Encoder(org.springframework.core.codec.Encoder) Assert(org.springframework.util.Assert) Encoder(org.springframework.core.codec.Encoder) CodecException(org.springframework.core.codec.CodecException) ResolvableType(org.springframework.core.ResolvableType)

Example 15 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class Jackson2JsonEncoder method encode.

@Override
public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
    Assert.notNull(inputStream, "'inputStream' must not be null");
    Assert.notNull(bufferFactory, "'bufferFactory' must not be null");
    Assert.notNull(elementType, "'elementType' must not be null");
    if (inputStream instanceof Mono) {
        return Flux.from(inputStream).map(value -> encodeValue(value, bufferFactory, elementType, hints));
    } else if (APPLICATION_STREAM_JSON.isCompatibleWith(mimeType)) {
        return Flux.from(inputStream).map(value -> {
            DataBuffer buffer = encodeValue(value, bufferFactory, elementType, hints);
            buffer.write(new byte[] { '\n' });
            return buffer;
        });
    }
    ResolvableType listType = ResolvableType.forClassWithGenerics(List.class, elementType);
    return Flux.from(inputStream).collectList().map(list -> encodeValue(list, bufferFactory, listType, hints)).flux();
}
Also used : SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) CodecException(org.springframework.core.codec.CodecException) MimeType(org.springframework.util.MimeType) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) Map(java.util.Map) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) JavaType(com.fasterxml.jackson.databind.JavaType) ResolvableType(org.springframework.core.ResolvableType) Encoder(org.springframework.core.codec.Encoder) OutputStream(java.io.OutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Publisher(org.reactivestreams.Publisher) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) DefaultIndenter(com.fasterxml.jackson.core.util.DefaultIndenter) DataBuffer(org.springframework.core.io.buffer.DataBuffer) APPLICATION_STREAM_JSON(org.springframework.http.MediaType.APPLICATION_STREAM_JSON) Flux(reactor.core.publisher.Flux) List(java.util.List) Jackson2ObjectMapperBuilder(org.springframework.http.converter.json.Jackson2ObjectMapperBuilder) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) PrettyPrinter(com.fasterxml.jackson.core.PrettyPrinter) Assert(org.springframework.util.Assert) Mono(reactor.core.publisher.Mono) ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

ResolvableType (org.springframework.core.ResolvableType)96 Test (org.junit.Test)66 MethodParameter (org.springframework.core.MethodParameter)20 DataBuffer (org.springframework.core.io.buffer.DataBuffer)15 ServerWebExchange (org.springframework.web.server.ServerWebExchange)14 List (java.util.List)11 MediaType (org.springframework.http.MediaType)10 Flux (reactor.core.publisher.Flux)10 Mono (reactor.core.publisher.Mono)10 Map (java.util.Map)9 Assert (org.springframework.util.Assert)7 Collections (java.util.Collections)6 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)6 ArrayList (java.util.ArrayList)5 ReactiveAdapter (org.springframework.core.ReactiveAdapter)5 Pojo (org.springframework.http.codec.Pojo)5 ServerWebInputException (org.springframework.web.server.ServerWebInputException)5 Publisher (org.reactivestreams.Publisher)4 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)4 Single (rx.Single)4