Search in sources :

Example 1 with HttpMessageWriter

use of org.springframework.http.codec.HttpMessageWriter in project spring-integration by spring-projects.

the class WebFluxInboundEndpoint method writeResponseBody.

@SuppressWarnings("unchecked")
private Mono<Void> writeResponseBody(ServerWebExchange exchange, Object body) {
    ResolvableType bodyType = ResolvableType.forInstance(body);
    ReactiveAdapter adapter = this.adapterRegistry.getAdapter(bodyType.resolve(), body);
    Publisher<?> publisher;
    ResolvableType elementType;
    if (adapter != null) {
        publisher = adapter.toPublisher(body);
        ResolvableType genericType = bodyType.getGeneric(0);
        elementType = getElementType(adapter, genericType);
    } else {
        publisher = Mono.justOrEmpty(body);
        elementType = bodyType;
    }
    if (void.class == elementType.getRawClass() || Void.class == elementType.getRawClass()) {
        return Mono.from((Publisher<Void>) publisher);
    }
    List<MediaType> producibleMediaTypes = getProducibleMediaTypes(bodyType);
    MediaType bestMediaType = selectMediaType(exchange, () -> producibleMediaTypes);
    if (bestMediaType != null) {
        for (HttpMessageWriter<?> writer : this.codecConfigurer.getWriters()) {
            if (writer.canWrite(bodyType, bestMediaType)) {
                return ((HttpMessageWriter<Object>) writer).write(publisher, elementType, bestMediaType, exchange.getResponse(), Collections.emptyMap());
            }
        }
    } else {
        if (producibleMediaTypes.isEmpty()) {
            return Mono.error(new IllegalStateException("No HttpMessageWriters for response type: " + bodyType));
        }
    }
    return Mono.error(new NotAcceptableStatusException(producibleMediaTypes));
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) MediaType(org.springframework.http.MediaType) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 2 with HttpMessageWriter

use of org.springframework.http.codec.HttpMessageWriter in project spring-security by spring-projects.

the class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests method getBody.

private static String getBody(ClientRequest request) {
    final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
    messageWriters.add(new ResourceHttpMessageWriter());
    Jackson2JsonEncoder jsonEncoder = new Jackson2JsonEncoder();
    messageWriters.add(new EncoderHttpMessageWriter<>(jsonEncoder));
    messageWriters.add(new ServerSentEventHttpMessageWriter(jsonEncoder));
    messageWriters.add(new FormHttpMessageWriter());
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    messageWriters.add(new MultipartHttpMessageWriter(messageWriters));
    BodyInserter.Context context = new BodyInserter.Context() {

        @Override
        public List<HttpMessageWriter<?>> messageWriters() {
            return messageWriters;
        }

        @Override
        public Optional<ServerHttpRequest> serverRequest() {
            return Optional.empty();
        }

        @Override
        public Map<String, Object> hints() {
            return new HashMap<>();
        }
    };
    MockClientHttpRequest body = new MockClientHttpRequest(HttpMethod.GET, "/");
    request.body().insert(body, context).block();
    return body.getBodyAsString().block();
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) Context(reactor.util.context.Context) OAuth2AuthorizationContext(org.springframework.security.oauth2.client.OAuth2AuthorizationContext) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) HashMap(java.util.HashMap) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) ArrayList(java.util.ArrayList) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) BodyInserter(org.springframework.web.reactive.function.BodyInserter) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter)

Example 3 with HttpMessageWriter

use of org.springframework.http.codec.HttpMessageWriter in project spring-framework by spring-projects.

the class DefaultEntityResponseBuilderTests method bodyInserter.

@Test
public void bodyInserter() {
    String body = "foo";
    Publisher<String> publisher = Mono.just(body);
    Mono<EntityResponse<Publisher<String>>> result = EntityResponse.fromPublisher(publisher, String.class).build();
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost"));
    ServerResponse.Context context = new ServerResponse.Context() {

        @Override
        public List<HttpMessageWriter<?>> messageWriters() {
            return Collections.singletonList(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
        }

        @Override
        public List<ViewResolver> viewResolvers() {
            return Collections.emptyList();
        }
    };
    StepVerifier.create(result).consumeNextWith(response -> {
        StepVerifier.create(response.entity()).expectNext(body).expectComplete().verify();
        response.writeTo(exchange, context);
    }).expectComplete().verify();
    assertThat(exchange.getResponse().getBody()).isNotNull();
}
Also used : EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ViewResolver(org.springframework.web.reactive.result.view.ViewResolver) Test(org.junit.jupiter.api.Test)

Example 4 with HttpMessageWriter

use of org.springframework.http.codec.HttpMessageWriter in project spring-framework by spring-projects.

the class BaseDefaultCodecs method getBaseTypedWriters.

/**
 * Return "base" typed writers only, i.e. common to client and server.
 */
final List<HttpMessageWriter<?>> getBaseTypedWriters() {
    if (!this.registerDefaults) {
        return Collections.emptyList();
    }
    List<HttpMessageWriter<?>> writers = new ArrayList<>();
    addCodec(writers, new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
    addCodec(writers, new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    addCodec(writers, new EncoderHttpMessageWriter<>(new DataBufferEncoder()));
    if (nettyByteBufPresent) {
        addCodec(writers, new EncoderHttpMessageWriter<>(new NettyByteBufEncoder()));
    }
    addCodec(writers, new ResourceHttpMessageWriter());
    addCodec(writers, new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
    if (protobufPresent) {
        addCodec(writers, new ProtobufHttpMessageWriter(this.protobufEncoder != null ? (ProtobufEncoder) this.protobufEncoder : new ProtobufEncoder()));
    }
    return writers;
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ProtobufEncoder(org.springframework.http.codec.protobuf.ProtobufEncoder) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ProtobufHttpMessageWriter(org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) DataBufferEncoder(org.springframework.core.codec.DataBufferEncoder) ArrayList(java.util.ArrayList) NettyByteBufEncoder(org.springframework.core.codec.NettyByteBufEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter)

Example 5 with HttpMessageWriter

use of org.springframework.http.codec.HttpMessageWriter in project spring-framework by spring-projects.

the class ClientCodecConfigurerTests method cloneShouldNotBeImpactedByChangesToOriginal.

@Test
public void cloneShouldNotBeImpactedByChangesToOriginal() {
    ClientCodecConfigurer clone = this.configurer.clone();
    this.configurer.registerDefaults(false);
    this.configurer.customCodecs().register(new Jackson2JsonEncoder());
    List<HttpMessageWriter<?>> writers = findCodec(clone.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
    assertThat(writers).hasSize(12);
}
Also used : FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ProtobufHttpMessageWriter(org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ClientCodecConfigurer(org.springframework.http.codec.ClientCodecConfigurer) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Aggregations

HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)27 EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)23 ResourceHttpMessageWriter (org.springframework.http.codec.ResourceHttpMessageWriter)15 Test (org.junit.jupiter.api.Test)14 FormHttpMessageWriter (org.springframework.http.codec.FormHttpMessageWriter)11 Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)11 MultipartHttpMessageWriter (org.springframework.http.codec.multipart.MultipartHttpMessageWriter)11 ArrayList (java.util.ArrayList)10 ProtobufHttpMessageWriter (org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter)8 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)7 RequestedContentTypeResolver (org.springframework.web.reactive.accept.RequestedContentTypeResolver)6 List (java.util.List)5 MediaType (org.springframework.http.MediaType)5 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)5 ServerSentEventHttpMessageWriter (org.springframework.http.codec.ServerSentEventHttpMessageWriter)5 Jaxb2XmlEncoder (org.springframework.http.codec.xml.Jaxb2XmlEncoder)5 ResolvableType (org.springframework.core.ResolvableType)4 BodyInserter (org.springframework.web.reactive.function.BodyInserter)4 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3