use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class WebHttpHandlerBuilderTests method writeToResponse.
private static Mono<Void> writeToResponse(ServerWebExchange exchange, String value) {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
return exchange.getResponse().writeWith(Flux.just(buffer));
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class MessagingRSocket method handleAndReply.
@SuppressWarnings("deprecation")
private Flux<Payload> handleAndReply(Payload firstPayload, FrameType frameType, Flux<Payload> payloads) {
AtomicReference<Flux<Payload>> responseRef = new AtomicReference<>();
MessageHeaders headers = createHeaders(firstPayload, frameType, responseRef);
AtomicBoolean read = new AtomicBoolean();
Flux<DataBuffer> buffers = payloads.map(this::retainDataAndReleasePayload).doOnSubscribe(s -> read.set(true));
Message<Flux<DataBuffer>> message = MessageBuilder.createMessage(buffers, headers);
return Mono.defer(() -> this.messageHandler.handleMessage(message)).doFinally(s -> {
// Subscription should have happened by now due to ChannelSendOperator
if (!read.get()) {
firstPayload.release();
}
}).thenMany(Flux.defer(() -> responseRef.get() != null ? responseRef.get() : Mono.error(new IllegalStateException("Expected response"))));
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class MessagingRSocket method handle.
private Mono<Void> handle(Payload payload, FrameType frameType) {
MessageHeaders headers = createHeaders(payload, frameType, null);
DataBuffer dataBuffer = retainDataAndReleasePayload(payload);
int refCount = refCount(dataBuffer);
Message<?> message = MessageBuilder.createMessage(dataBuffer, headers);
return Mono.defer(() -> this.messageHandler.handleMessage(message)).doFinally(s -> {
if (refCount(dataBuffer) == refCount) {
DataBufferUtils.release(dataBuffer);
}
});
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class PayloadMethodArgumentResolver method resolveArgument.
/**
* Decode the content of the given message payload through a compatible
* {@link Decoder}.
*
* <p>Validation is applied if the method argument is annotated with
* {@code @jakarta.validation.Valid} or
* {@link org.springframework.validation.annotation.Validated}. Validation
* failure results in an {@link MethodArgumentNotValidException}.
* @param parameter the target method argument that we are decoding to
* @param message the message from which the content was extracted
* @return a Mono with the result of argument resolution
* @see #extractContent(MethodParameter, Message)
* @see #getMimeType(Message)
*/
@Override
public final Mono<Object> resolveArgument(MethodParameter parameter, Message<?> message) {
Payload ann = parameter.getParameterAnnotation(Payload.class);
if (ann != null && StringUtils.hasText(ann.expression())) {
throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver");
}
MimeType mimeType = getMimeType(message);
mimeType = mimeType != null ? mimeType : MimeTypeUtils.APPLICATION_OCTET_STREAM;
Flux<DataBuffer> content = extractContent(parameter, message);
return decodeContent(parameter, message, ann == null || ann.required(), content, mimeType);
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class MetadataEncoderTests method compositeMetadata.
@Test
public void compositeMetadata() {
Mono<String> asyncMeta1 = Mono.delay(Duration.ofMillis(1)).map(aLong -> "Async Metadata 1");
Mono<String> asyncMeta2 = Mono.delay(Duration.ofMillis(1)).map(aLong -> "Async Metadata 2");
DataBuffer buffer = new MetadataEncoder(COMPOSITE_METADATA, this.strategies).route("toA").metadata("My metadata", MimeTypeUtils.TEXT_PLAIN).metadata(asyncMeta1, new MimeType("text", "x.test.metadata1")).metadata(Unpooled.wrappedBuffer("Raw data".getBytes(UTF_8)), MimeTypeUtils.APPLICATION_OCTET_STREAM).metadata(asyncMeta2, new MimeType("text", "x.test.metadata2")).encode().block();
CompositeMetadata entries = new CompositeMetadata(((NettyDataBuffer) buffer).getNativeBuffer(), false);
Iterator<CompositeMetadata.Entry> iterator = entries.iterator();
assertThat(iterator.hasNext()).isTrue();
CompositeMetadata.Entry entry = iterator.next();
assertThat(entry.getMimeType()).isEqualTo(WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString());
assertRoute("toA", entry.getContent());
assertThat(iterator.hasNext()).isTrue();
entry = iterator.next();
assertThat(entry.getMimeType()).isEqualTo(MimeTypeUtils.TEXT_PLAIN_VALUE);
assertThat(entry.getContent().toString(UTF_8)).isEqualTo("My metadata");
assertThat(iterator.hasNext()).isTrue();
entry = iterator.next();
assertThat(entry.getMimeType()).isEqualTo("text/x.test.metadata1");
assertThat(entry.getContent().toString(UTF_8)).isEqualTo("Async Metadata 1");
assertThat(iterator.hasNext()).isTrue();
entry = iterator.next();
assertThat(entry.getMimeType()).isEqualTo(MimeTypeUtils.APPLICATION_OCTET_STREAM_VALUE);
assertThat(entry.getContent().toString(UTF_8)).isEqualTo("Raw data");
assertThat(iterator.hasNext()).isTrue();
entry = iterator.next();
assertThat(entry.getMimeType()).isEqualTo("text/x.test.metadata2");
assertThat(entry.getContent().toString(UTF_8)).isEqualTo("Async Metadata 2");
assertThat(iterator.hasNext()).isFalse();
}
Aggregations