Search in sources :

Example 6 with ResourceHttpMessageWriter

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

the class DefaultExchangeStrategiesBuilder method defaultWriters.

private void defaultWriters() {
    messageWriter(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
    messageWriter(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    messageWriter(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
    messageWriter(new ResourceHttpMessageWriter());
    messageWriter(new FormHttpMessageWriter());
    if (jaxb2Present) {
        messageWriter(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
    }
    if (jackson2Present) {
        messageWriter(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
    }
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder)

Example 7 with ResourceHttpMessageWriter

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

the class BodyInsertersTests method createContext.

@BeforeEach
public void createContext() {
    final List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
    messageWriters.add(new ResourceHttpMessageWriter());
    messageWriters.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
    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));
    this.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 hints;
        }
    };
    this.hints = new HashMap<>();
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) 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) Optional(java.util.Optional) ArrayList(java.util.ArrayList) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) List(java.util.List) ArrayList(java.util.ArrayList) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with ResourceHttpMessageWriter

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

the class BodyInsertersTests method ofResourceRange.

@Test
public void ofResourceRange() throws IOException {
    final int rangeStart = 10;
    Resource body = new ClassPathResource("response.txt", getClass());
    BodyInserter<Resource, ReactiveHttpOutputMessage> inserter = BodyInserters.fromResource(body);
    MockServerHttpRequest request = MockServerHttpRequest.get("/foo").range(HttpRange.createByteRange(rangeStart)).build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, new BodyInserter.Context() {

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

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

        @Override
        public Map<String, Object> hints() {
            return hints;
        }
    });
    StepVerifier.create(result).expectComplete().verify();
    byte[] allBytes = Files.readAllBytes(body.getFile().toPath());
    byte[] expectedBytes = new byte[allBytes.length - rangeStart];
    System.arraycopy(allBytes, rangeStart, expectedBytes, 0, expectedBytes.length);
    StepVerifier.create(response.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        DataBufferUtils.release(dataBuffer);
        assertThat(resultBytes).isEqualTo(expectedBytes);
    }).expectComplete().verify();
}
Also used : ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) Optional(java.util.Optional) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.jupiter.api.Test)

Example 9 with ResourceHttpMessageWriter

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

the class ResponseEntityResultHandlerTests method createHandler.

private ResponseEntityResultHandler createHandler(HttpMessageWriter<?>... writers) {
    List<HttpMessageWriter<?>> writerList;
    if (ObjectUtils.isEmpty(writers)) {
        writerList = new ArrayList<>();
        writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
        writerList.add(new ResourceHttpMessageWriter());
        writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    } else {
        writerList = Arrays.asList(writers);
    }
    RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
    return new ResponseEntityResultHandler(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 10 with ResourceHttpMessageWriter

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

the class ClientCodecConfigurerTests method clonedConfigurer.

@Test
public void clonedConfigurer() {
    ClientCodecConfigurer clone = this.configurer.clone();
    Jackson2JsonDecoder jackson2Decoder = new Jackson2JsonDecoder();
    clone.defaultCodecs().serverSentEventDecoder(jackson2Decoder);
    clone.defaultCodecs().multipartCodecs().encoder(new Jackson2SmileEncoder());
    clone.defaultCodecs().multipartCodecs().writer(new ResourceHttpMessageWriter());
    // Clone has the customizations
    Decoder<?> sseDecoder = findCodec(clone.getReaders(), ServerSentEventHttpMessageReader.class).getDecoder();
    List<HttpMessageWriter<?>> writers = findCodec(clone.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
    assertThat(sseDecoder).isSameAs(jackson2Decoder);
    assertThat(writers).hasSize(2);
    // Original does not have the customizations
    sseDecoder = findCodec(this.configurer.getReaders(), ServerSentEventHttpMessageReader.class).getDecoder();
    writers = findCodec(this.configurer.getWriters(), MultipartHttpMessageWriter.class).getPartWriters();
    assertThat(sseDecoder).isNotSameAs(jackson2Decoder);
    assertThat(writers).hasSize(12);
}
Also used : ServerSentEventHttpMessageReader(org.springframework.http.codec.ServerSentEventHttpMessageReader) 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) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) Jackson2SmileEncoder(org.springframework.http.codec.json.Jackson2SmileEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) Test(org.junit.jupiter.api.Test)

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