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);
}
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;
}
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<>();
}
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();
}
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;
}
Aggregations