Search in sources :

Example 1 with ResourceHttpMessageWriter

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

the class WebFluxConfigurationSupport method addDefaultHttpMessageWriters.

/**
	 * Adds default converters that sub-classes can call from
	 * {@link #configureMessageWriters(List)}.
	 */
protected final void addDefaultHttpMessageWriters(List<HttpMessageWriter<?>> writers) {
    List<Encoder<?>> sseDataEncoders = new ArrayList<>();
    writers.add(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
    writers.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    writers.add(new EncoderHttpMessageWriter<>(new DataBufferEncoder()));
    writers.add(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
    writers.add(new ResourceHttpMessageWriter());
    if (jaxb2Present) {
        writers.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
    }
    if (jackson2Present) {
        Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
        writers.add(new Jackson2ServerHttpMessageWriter(encoder));
        sseDataEncoders.add(encoder);
        HttpMessageWriter<Object> writer = new ServerSentEventHttpMessageWriter(sseDataEncoders);
        writers.add(new Jackson2ServerHttpMessageWriter(writer));
    } else {
        writers.add(new ServerSentEventHttpMessageWriter(sseDataEncoders));
    }
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) ArrayList(java.util.ArrayList) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Jackson2ServerHttpMessageWriter(org.springframework.http.codec.Jackson2ServerHttpMessageWriter) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) DataBufferEncoder(org.springframework.core.codec.DataBufferEncoder) ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Encoder(org.springframework.core.codec.Encoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) DataBufferEncoder(org.springframework.core.codec.DataBufferEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder)

Example 2 with ResourceHttpMessageWriter

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

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

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

the class MessageWriterResultHandlerTests method initResultHandler.

private AbstractMessageWriterResultHandler initResultHandler(HttpMessageWriter<?>... writers) {
    List<HttpMessageWriter<?>> writerList;
    if (ObjectUtils.isEmpty(writers)) {
        writerList = new ArrayList<>();
        writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
        writerList.add(new ResourceHttpMessageWriter());
        writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
    } else {
        writerList = Arrays.asList(writers);
    }
    RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
    return new AbstractMessageWriterResultHandler(writerList, resolver) {
    };
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)

Example 5 with ResourceHttpMessageWriter

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

the class ResponseBodyResultHandlerTests method setup.

@BeforeEach
public void setup() throws Exception {
    List<HttpMessageWriter<?>> writerList = new ArrayList<>(5);
    writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    writerList.add(new ResourceHttpMessageWriter());
    writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
    writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
    RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
    this.resultHandler = new ResponseBodyResultHandler(writerList, resolver);
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ResourceHttpMessageWriter (org.springframework.http.codec.ResourceHttpMessageWriter)11 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)9 EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)8 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)8 Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)8 ArrayList (java.util.ArrayList)7 FormHttpMessageWriter (org.springframework.http.codec.FormHttpMessageWriter)6 Jaxb2XmlEncoder (org.springframework.http.codec.xml.Jaxb2XmlEncoder)6 ServerSentEventHttpMessageWriter (org.springframework.http.codec.ServerSentEventHttpMessageWriter)5 MultipartHttpMessageWriter (org.springframework.http.codec.multipart.MultipartHttpMessageWriter)5 HashMap (java.util.HashMap)4 ByteArrayEncoder (org.springframework.core.codec.ByteArrayEncoder)3 RequestedContentTypeResolver (org.springframework.web.reactive.accept.RequestedContentTypeResolver)3 RequestedContentTypeResolverBuilder (org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)3 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)2