Search in sources :

Example 1 with EncoderHttpMessageWriter

use of org.springframework.http.codec.EncoderHttpMessageWriter 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 2 with EncoderHttpMessageWriter

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

the class ServerHttpResponseTests method monoResponseShouldNotLeakIfCancelled.

// gh-26232
@Test
void monoResponseShouldNotLeakIfCancelled() {
    LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse(bufferFactory);
    response.setWriteHandler(flux -> {
        throw AbortedException.beforeSend();
    });
    HttpMessageWriter<Object> messageWriter = new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder());
    Mono<Void> result = messageWriter.write(Mono.just(Collections.singletonMap("foo", "bar")), ResolvableType.forClass(Mono.class), ResolvableType.forClass(Map.class), null, request, response, Collections.emptyMap());
    StepVerifier.create(result).expectError(AbortedException.class).verify();
    bufferFactory.checkForLeaks();
}
Also used : LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) AbortedException(reactor.netty.channel.AbortedException) Mono(reactor.core.publisher.Mono) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Map(java.util.Map) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 3 with EncoderHttpMessageWriter

use of org.springframework.http.codec.EncoderHttpMessageWriter 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 EncoderHttpMessageWriter

use of org.springframework.http.codec.EncoderHttpMessageWriter 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 EncoderHttpMessageWriter

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

the class ResponseEntityResultHandlerTests method handleWithProducibleContentTypeShouldFailWithServerError.

// gh-23287
@Test
public void handleWithProducibleContentTypeShouldFailWithServerError() {
    ResponseEntity<String> value = ResponseEntity.ok().body("<foo/>");
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class));
    HandlerResult result = handlerResult(value, returnType);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
    Set<MediaType> mediaTypes = Collections.singleton(MediaType.APPLICATION_XML);
    exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes);
    ResponseEntityResultHandler resultHandler = new ResponseEntityResultHandler(Collections.singletonList(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly())), new RequestedContentTypeResolverBuilder().build());
    StepVerifier.create(resultHandler.handleResult(exchange, result)).consumeErrorWith(ex -> assertThat(ex).isInstanceOf(HttpMessageNotWritableException.class).hasMessageContaining("with preset Content-Type")).verify();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) Duration(java.time.Duration) MethodParameter(org.springframework.core.MethodParameter) URI(java.net.URI) ResolvableType(org.springframework.core.ResolvableType) PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE(org.springframework.web.reactive.HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Set(java.util.Set) Instant(java.time.Instant) HandlerResult(org.springframework.web.reactive.HandlerResult) Test(org.junit.jupiter.api.Test) List(java.util.List) Single(io.reactivex.rxjava3.core.Single) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) CompletableFuture(java.util.concurrent.CompletableFuture) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) ArrayList(java.util.ArrayList) MockServerHttpRequest.get(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest.get) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) ResolvableMethod.on(org.springframework.web.testfixture.method.ResolvableMethod.on) LinkedHashSet(java.util.LinkedHashSet) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ObjectUtils(org.springframework.util.ObjectUtils) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(org.springframework.http.HttpMethod) Completable(io.reactivex.rxjava3.core.Completable) Mono(reactor.core.publisher.Mono) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) ResponseEntity.notFound(org.springframework.http.ResponseEntity.notFound) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) ChronoUnit(java.time.temporal.ChronoUnit) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) ResolvableType.forClassWithGenerics(org.springframework.core.ResolvableType.forClassWithGenerics) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) ResponseEntity(org.springframework.http.ResponseEntity) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) MediaType(org.springframework.http.MediaType) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Aggregations

EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)18 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)15 ArrayList (java.util.ArrayList)11 Test (org.junit.jupiter.api.Test)11 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)11 Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)11 ResourceHttpMessageWriter (org.springframework.http.codec.ResourceHttpMessageWriter)10 Jaxb2XmlEncoder (org.springframework.http.codec.xml.Jaxb2XmlEncoder)7 List (java.util.List)6 RequestedContentTypeResolverBuilder (org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)5 ServerSentEventHttpMessageWriter (org.springframework.http.codec.ServerSentEventHttpMessageWriter)5 RequestedContentTypeResolver (org.springframework.web.reactive.accept.RequestedContentTypeResolver)5 Mono (reactor.core.publisher.Mono)5 URI (java.net.URI)4 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 MediaType (org.springframework.http.MediaType)4 FormHttpMessageWriter (org.springframework.http.codec.FormHttpMessageWriter)4