use of org.springframework.core.io.buffer.DataBufferUtils in project spring-data-mongodb by spring-projects.
the class ReactiveGridFsTemplateTests method getResourceShouldRetrieveContentByIdentity.
// DATAMONGO-1855
@Test
public void getResourceShouldRetrieveContentByIdentity() throws IOException {
byte[] content = StreamUtils.copyToByteArray(resource.getInputStream());
Flux<DataBuffer> source = DataBufferUtils.read(resource, new DefaultDataBufferFactory(), 256);
ObjectId reference = operations.store(source, "foo.xml", null, null).block();
operations.findOne(query(where("_id").is(reference))).flatMap(operations::getResource).flatMapMany(//
ReactiveGridFsResource::getDownloadStream).transform(//
DataBufferUtils::join).as(//
StepVerifier::create).consumeNextWith(dataBuffer -> {
byte[] actual = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(actual);
assertThat(actual).isEqualTo(content);
}).verifyComplete();
}
use of org.springframework.core.io.buffer.DataBufferUtils in project spring-framework by spring-projects.
the class EncoderHttpMessageWriter method write.
@Override
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
MediaType contentType = updateContentType(message, mediaType);
Flux<DataBuffer> body = this.encoder.encode(inputStream, message.bufferFactory(), elementType, contentType, hints);
if (inputStream instanceof Mono) {
return body.singleOrEmpty().switchIfEmpty(Mono.defer(() -> {
message.getHeaders().setContentLength(0);
return message.setComplete().then(Mono.empty());
})).flatMap(buffer -> {
Hints.touchDataBuffer(buffer, hints, logger);
message.getHeaders().setContentLength(buffer.readableByteCount());
return message.writeWith(Mono.just(buffer).doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release));
}).doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
}
if (isStreamingMediaType(contentType)) {
return message.writeAndFlushWith(body.map(buffer -> {
Hints.touchDataBuffer(buffer, hints, logger);
return Mono.just(buffer).doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
}));
}
if (logger.isDebugEnabled()) {
body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
}
return message.writeWith(body);
}
use of org.springframework.core.io.buffer.DataBufferUtils in project spring-framework by spring-projects.
the class ServerSentEventHttpMessageWriter method encode.
private Flux<Publisher<DataBuffer>> encode(Publisher<?> input, ResolvableType elementType, MediaType mediaType, DataBufferFactory factory, Map<String, Object> hints) {
ResolvableType dataType = (ServerSentEvent.class.isAssignableFrom(elementType.toClass()) ? elementType.getGeneric() : elementType);
return Flux.from(input).map(element -> {
ServerSentEvent<?> sse = (element instanceof ServerSentEvent ? (ServerSentEvent<?>) element : ServerSentEvent.builder().data(element).build());
StringBuilder sb = new StringBuilder();
String id = sse.id();
String event = sse.event();
Duration retry = sse.retry();
String comment = sse.comment();
Object data = sse.data();
if (id != null) {
writeField("id", id, sb);
}
if (event != null) {
writeField("event", event, sb);
}
if (retry != null) {
writeField("retry", retry.toMillis(), sb);
}
if (comment != null) {
sb.append(':').append(StringUtils.replace(comment, "\n", "\n:")).append('\n');
}
if (data != null) {
sb.append("data:");
}
Flux<DataBuffer> result;
if (data == null) {
result = Flux.just(encodeText(sb + "\n", mediaType, factory));
} else if (data instanceof String) {
data = StringUtils.replace((String) data, "\n", "\ndata:");
result = Flux.just(encodeText(sb + (String) data + "\n\n", mediaType, factory));
} else {
result = encodeEvent(sb, data, dataType, mediaType, factory, hints);
}
return result.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
});
}
use of org.springframework.core.io.buffer.DataBufferUtils in project spring-framework by spring-projects.
the class DefaultRSocketRequesterBuilder method getSetupPayload.
private Mono<Payload> getSetupPayload(MimeType dataMimeType, MimeType metaMimeType, RSocketStrategies strategies) {
Object data = this.setupData;
boolean hasMetadata = (this.setupRoute != null || !CollectionUtils.isEmpty(this.setupMetadata));
if (!hasMetadata && data == null) {
return Mono.just(EMPTY_SETUP_PAYLOAD);
}
Mono<DataBuffer> dataMono = Mono.empty();
if (data != null) {
ReactiveAdapter adapter = strategies.reactiveAdapterRegistry().getAdapter(data.getClass());
Assert.isTrue(adapter == null || !adapter.isMultiValue(), "Expected single value: " + data);
Mono<?> mono = (adapter != null ? Mono.from(adapter.toPublisher(data)) : Mono.just(data));
dataMono = mono.map(value -> {
ResolvableType type = ResolvableType.forClass(value.getClass());
Encoder<Object> encoder = strategies.encoder(type, dataMimeType);
Assert.notNull(encoder, () -> "No encoder for " + dataMimeType + ", " + type);
return encoder.encodeValue(value, strategies.dataBufferFactory(), type, dataMimeType, HINTS);
});
}
Mono<DataBuffer> metaMono = Mono.empty();
if (hasMetadata) {
metaMono = new MetadataEncoder(metaMimeType, strategies).metadataAndOrRoute(this.setupMetadata, this.setupRoute, this.setupRouteVars).encode();
}
Mono<DataBuffer> emptyBuffer = Mono.fromCallable(() -> strategies.dataBufferFactory().wrap(EMPTY_BYTE_ARRAY));
dataMono = dataMono.switchIfEmpty(emptyBuffer);
metaMono = metaMono.switchIfEmpty(emptyBuffer);
return Mono.zip(dataMono, metaMono).map(tuple -> PayloadUtils.createPayload(tuple.getT1(), tuple.getT2())).doOnDiscard(DataBuffer.class, DataBufferUtils::release).doOnDiscard(Payload.class, Payload::release);
}
use of org.springframework.core.io.buffer.DataBufferUtils in project spring-framework by spring-projects.
the class Jackson2JsonEncoderTests method encodeAsStreamWithCustomStreamingType.
// SPR-15727
@Test
public void encodeAsStreamWithCustomStreamingType() {
MediaType fooMediaType = new MediaType("application", "foo");
MediaType barMediaType = new MediaType("application", "bar");
this.encoder.setStreamingMediaTypes(Arrays.asList(fooMediaType, barMediaType));
Flux<Pojo> input = Flux.just(new Pojo("foo", "bar"), new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
testEncode(input, ResolvableType.forClass(Pojo.class), step -> step.consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}\n").andThen(DataBufferUtils::release)).consumeNextWith(expectString("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n").andThen(DataBufferUtils::release)).consumeNextWith(expectString("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n").andThen(DataBufferUtils::release)).verifyComplete(), barMediaType, null);
}
Aggregations