Search in sources :

Example 56 with ResolvableType

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

the class ResourceRegionEncoderTests method canEncode.

@Test
public void canEncode() {
    ResolvableType resourceRegion = ResolvableType.forClass(ResourceRegion.class);
    MimeType allMimeType = MimeType.valueOf("*/*");
    assertFalse(this.encoder.canEncode(ResolvableType.forClass(Resource.class), MimeTypeUtils.APPLICATION_OCTET_STREAM));
    assertFalse(this.encoder.canEncode(ResolvableType.forClass(Resource.class), allMimeType));
    assertTrue(this.encoder.canEncode(resourceRegion, MimeTypeUtils.APPLICATION_OCTET_STREAM));
    assertTrue(this.encoder.canEncode(resourceRegion, allMimeType));
}
Also used : ResolvableType(org.springframework.core.ResolvableType) MimeType(org.springframework.util.MimeType) Test(org.junit.Test)

Example 57 with ResolvableType

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

the class AbstractJackson2Codec method resolveVariable.

private ResolvableType resolveVariable(TypeVariable<?> typeVariable, ResolvableType contextType) {
    ResolvableType resolvedType;
    if (contextType.hasGenerics()) {
        resolvedType = ResolvableType.forType(typeVariable, contextType);
        if (resolvedType.resolve() != null) {
            return resolvedType;
        }
    }
    resolvedType = resolveVariable(typeVariable, contextType.getSuperType());
    if (resolvedType.resolve() != null) {
        return resolvedType;
    }
    for (ResolvableType ifc : contextType.getInterfaces()) {
        resolvedType = resolveVariable(typeVariable, ifc);
        if (resolvedType.resolve() != null) {
            return resolvedType;
        }
    }
    return ResolvableType.NONE;
}
Also used : ResolvableType(org.springframework.core.ResolvableType)

Example 58 with ResolvableType

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

the class FormHttpMessageWriter method write.

@Override
public Mono<Void> write(Publisher<? extends MultiValueMap<String, String>> inputStream, ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    if (contentType == null) {
        contentType = MediaType.APPLICATION_FORM_URLENCODED;
        outputMessage.getHeaders().setContentType(contentType);
    }
    Charset charset = getMediaTypeCharset(contentType);
    return Flux.from(inputStream).single().map(form -> generateForm(form, charset)).then(value -> {
        ByteBuffer byteBuffer = charset.encode(value);
        DataBuffer buffer = outputMessage.bufferFactory().wrap(byteBuffer);
        outputMessage.getHeaders().setContentLength(byteBuffer.remaining());
        return outputMessage.writeWith(Mono.just(buffer));
    });
}
Also used : Iterator(java.util.Iterator) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) Flux(reactor.core.publisher.Flux) URLEncoder(java.net.URLEncoder) List(java.util.List) Charset(java.nio.charset.Charset) Map(java.util.Map) ResolvableType(org.springframework.core.ResolvableType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) Assert(org.springframework.util.Assert) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 59 with ResolvableType

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

the class Jackson2JsonDecoderTests method decodePojoWithError.

@Test
public void decodePojoWithError() throws Exception {
    Flux<DataBuffer> source = Flux.just(stringBuffer("{\"foo\":}"));
    ResolvableType elementType = ResolvableType.forClass(Pojo.class);
    Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null, Collections.emptyMap());
    StepVerifier.create(flux).verifyError(CodecException.class);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 60 with ResolvableType

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

the class Jackson2JsonDecoderTests method decodeToFlux.

@Test
public void decodeToFlux() throws Exception {
    Flux<DataBuffer> source = Flux.just(stringBuffer("[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"));
    ResolvableType elementType = ResolvableType.forClass(Pojo.class);
    Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null, Collections.emptyMap());
    StepVerifier.create(flux).expectNext(new Pojo("f1", "b1")).expectNext(new Pojo("f2", "b2")).expectComplete().verify();
}
Also used : Pojo(org.springframework.http.codec.Pojo) ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

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