Search in sources :

Example 1 with ResourceHttpMessageWriter

use of cn.taketoday.http.codec.ResourceHttpMessageWriter in project today-framework by TAKETODAY.

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(10);
}
Also used : ServerSentEventHttpMessageReader(cn.taketoday.http.codec.ServerSentEventHttpMessageReader) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) PartEventHttpMessageWriter(cn.taketoday.http.codec.multipart.PartEventHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) ClientCodecConfigurer(cn.taketoday.http.codec.ClientCodecConfigurer) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) Jackson2SmileEncoder(cn.taketoday.http.codec.json.Jackson2SmileEncoder) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) Test(org.junit.jupiter.api.Test)

Example 2 with ResourceHttpMessageWriter

use of cn.taketoday.http.codec.ResourceHttpMessageWriter in project today-framework by TAKETODAY.

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(cn.taketoday.core.codec.ByteBufferEncoder) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) ByteArrayEncoder(cn.taketoday.core.codec.ByteArrayEncoder) DataBufferEncoder(cn.taketoday.core.codec.DataBufferEncoder) ArrayList(java.util.ArrayList) NettyByteBufEncoder(cn.taketoday.core.codec.NettyByteBufEncoder) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter)

Example 3 with ResourceHttpMessageWriter

use of cn.taketoday.http.codec.ResourceHttpMessageWriter in project today-framework by TAKETODAY.

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(cn.taketoday.core.codec.ByteBufferEncoder) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Optional(java.util.Optional) ArrayList(java.util.ArrayList) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) List(java.util.List) ArrayList(java.util.ArrayList) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(cn.taketoday.core.MultiValueMap) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ResourceHttpMessageWriter

use of cn.taketoday.http.codec.ResourceHttpMessageWriter in project today-framework by TAKETODAY.

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(cn.taketoday.http.ReactiveHttpOutputMessage) Optional(java.util.Optional) Resource(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) MockServerHttpResponse(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse) ClassPathResource(cn.taketoday.core.io.ClassPathResource) MockServerHttpRequest(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) List(java.util.List) ArrayList(java.util.ArrayList) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 5 with ResourceHttpMessageWriter

use of cn.taketoday.http.codec.ResourceHttpMessageWriter in project today-infrastructure by TAKETODAY.

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(cn.taketoday.core.codec.ByteBufferEncoder) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) ByteArrayEncoder(cn.taketoday.core.codec.ByteArrayEncoder) DataBufferEncoder(cn.taketoday.core.codec.DataBufferEncoder) ArrayList(java.util.ArrayList) NettyByteBufEncoder(cn.taketoday.core.codec.NettyByteBufEncoder) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter)

Aggregations

ResourceHttpMessageWriter (cn.taketoday.http.codec.ResourceHttpMessageWriter)8 EncoderHttpMessageWriter (cn.taketoday.http.codec.EncoderHttpMessageWriter)6 FormHttpMessageWriter (cn.taketoday.http.codec.FormHttpMessageWriter)6 HttpMessageWriter (cn.taketoday.http.codec.HttpMessageWriter)6 MultipartHttpMessageWriter (cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter)6 ArrayList (java.util.ArrayList)6 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)4 MultiValueMap (cn.taketoday.core.MultiValueMap)4 ByteBufferEncoder (cn.taketoday.core.codec.ByteBufferEncoder)4 ServerSentEventHttpMessageWriter (cn.taketoday.http.codec.ServerSentEventHttpMessageWriter)4 ProtobufHttpMessageWriter (cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Test (org.junit.jupiter.api.Test)4 ByteArrayEncoder (cn.taketoday.core.codec.ByteArrayEncoder)2 DataBufferEncoder (cn.taketoday.core.codec.DataBufferEncoder)2 NettyByteBufEncoder (cn.taketoday.core.codec.NettyByteBufEncoder)2 ClassPathResource (cn.taketoday.core.io.ClassPathResource)2