use of cn.taketoday.http.ReactiveHttpOutputMessage in project today-framework by TAKETODAY.
the class BodyInsertersTests method ofPublisher.
@Test
public void ofPublisher() {
Flux<String> body = Flux.just("foo");
BodyInserter<Flux<String>, ReactiveHttpOutputMessage> inserter = BodyInserters.fromPublisher(body, String.class);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context);
StepVerifier.create(result).expectComplete().verify();
StepVerifier.create(response.getBody()).consumeNextWith(buf -> {
String actual = buf.toString(UTF_8);
assertThat(actual).isEqualTo("foo");
}).expectComplete().verify();
}
use of cn.taketoday.http.ReactiveHttpOutputMessage in project today-framework by TAKETODAY.
the class BodyInsertersTests method ofProducerWithFlux.
@Test
public void ofProducerWithFlux() {
Flux<String> body = Flux.just("foo");
BodyInserter<?, ReactiveHttpOutputMessage> inserter = BodyInserters.fromProducer(body, String.class);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context);
StepVerifier.create(result).expectComplete().verify();
StepVerifier.create(response.getBody()).consumeNextWith(buf -> {
String actual = buf.toString(UTF_8);
assertThat(actual).isEqualTo("foo");
}).expectComplete().verify();
}
use of cn.taketoday.http.ReactiveHttpOutputMessage 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.ReactiveHttpOutputMessage in project today-infrastructure by TAKETODAY.
the class MultipartHttpMessageWriter method writeMultipart.
private Mono<Void> writeMultipart(MultiValueMap<String, ?> map, ReactiveHttpOutputMessage outputMessage, @Nullable MediaType mediaType, Map<String, Object> hints) {
byte[] boundary = generateMultipartBoundary();
mediaType = getMultipartMediaType(mediaType, boundary);
outputMessage.getHeaders().setContentType(mediaType);
if (isDebugEnabled) {
LogFormatUtils.traceDebug(logger, traceOn -> Hints.getLogPrefix(hints) + "Encoding " + (isEnableLoggingRequestDetails() ? LogFormatUtils.formatValue(map, !traceOn) : "parts " + map.keySet() + " (content masked)"));
}
DataBufferFactory bufferFactory = outputMessage.bufferFactory();
Flux<DataBuffer> body = Flux.fromIterable(map.entrySet()).concatMap(entry -> encodePartValues(boundary, entry.getKey(), entry.getValue(), bufferFactory)).concatWith(generateLastLine(boundary, bufferFactory)).doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
if (isDebugEnabled) {
body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
}
return outputMessage.writeWith(body);
}
use of cn.taketoday.http.ReactiveHttpOutputMessage in project today-infrastructure by TAKETODAY.
the class ResourceHttpMessageWriter method writeResource.
private Mono<Void> writeResource(Resource resource, ResolvableType type, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
HttpHeaders headers = message.getHeaders();
MediaType resourceMediaType = getResourceMediaType(mediaType, resource, hints);
headers.setContentType(resourceMediaType);
if (headers.getContentLength() < 0) {
long length = lengthOf(resource);
if (length != -1) {
headers.setContentLength(length);
}
}
return zeroCopy(resource, null, message, hints).orElseGet(() -> {
Mono<Resource> input = Mono.just(resource);
DataBufferFactory factory = message.bufferFactory();
Flux<DataBuffer> body = this.encoder.encode(input, factory, type, resourceMediaType, hints);
if (logger.isDebugEnabled()) {
body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
}
return message.writeWith(body);
});
}
Aggregations