Search in sources :

Example 11 with Payload

use of io.rsocket.Payload in project spring-framework by spring-projects.

the class PayloadUtilsTests method createWithNettyBuffers.

@Test
public void createWithNettyBuffers() {
    NettyDataBuffer data = createNettyDataBuffer("sample data");
    NettyDataBuffer metadata = createNettyDataBuffer("sample metadata");
    Payload payload = PayloadUtils.createPayload(data, metadata);
    try {
        assertThat(payload).isInstanceOf(ByteBufPayload.class);
        assertThat(payload.data()).isSameAs(data.getNativeBuffer());
        assertThat(payload.metadata()).isSameAs(metadata.getNativeBuffer());
    } finally {
        payload.release();
    }
}
Also used : NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) Payload(io.rsocket.Payload) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) Test(org.junit.jupiter.api.Test)

Example 12 with Payload

use of io.rsocket.Payload in project spring-framework by spring-projects.

the class PayloadUtilsTests method createWithNettyBuffer.

@Test
public void createWithNettyBuffer() {
    NettyDataBuffer data = createNettyDataBuffer("sample data");
    Payload payload = PayloadUtils.createPayload(data);
    try {
        assertThat(payload).isInstanceOf(ByteBufPayload.class);
        assertThat(payload.data()).isSameAs(data.getNativeBuffer());
    } finally {
        payload.release();
    }
}
Also used : NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) Payload(io.rsocket.Payload) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) Test(org.junit.jupiter.api.Test)

Example 13 with Payload

use of io.rsocket.Payload in project spring-framework by spring-projects.

the class PayloadUtilsTests method createWithDefaultBuffer.

@Test
public void createWithDefaultBuffer() {
    DataBuffer data = createDefaultDataBuffer("sample data");
    Payload payload = PayloadUtils.createPayload(data);
    assertThat(payload).isInstanceOf(DefaultPayload.class);
    assertThat(payload.getDataUtf8()).isEqualTo(data.toString(UTF_8));
}
Also used : Payload(io.rsocket.Payload) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) Test(org.junit.jupiter.api.Test)

Example 14 with Payload

use of io.rsocket.Payload 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);
}
Also used : ClientTransport(io.rsocket.transport.ClientTransport) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) Decoder(org.springframework.core.codec.Decoder) LoadbalanceTarget(io.rsocket.loadbalance.LoadbalanceTarget) PayloadDecoder(io.rsocket.frame.decoder.PayloadDecoder) WebsocketClientTransport(io.rsocket.transport.netty.client.WebsocketClientTransport) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) MimeType(org.springframework.util.MimeType) WellKnownMimeType(io.rsocket.metadata.WellKnownMimeType) Map(java.util.Map) DefaultPayload(io.rsocket.util.DefaultPayload) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) Nullable(org.springframework.lang.Nullable) URI(java.net.URI) ResolvableType(org.springframework.core.ResolvableType) Encoder(org.springframework.core.codec.Encoder) ReactiveAdapter(org.springframework.core.ReactiveAdapter) RSocketConnector(io.rsocket.core.RSocketConnector) StringDecoder(org.springframework.core.codec.StringDecoder) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) MimeTypeUtils(org.springframework.util.MimeTypeUtils) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Consumer(java.util.function.Consumer) TcpClientTransport(io.rsocket.transport.netty.client.TcpClientTransport) List(java.util.List) LoadbalanceStrategy(io.rsocket.loadbalance.LoadbalanceStrategy) Payload(io.rsocket.Payload) LoadbalanceRSocketClient(io.rsocket.loadbalance.LoadbalanceRSocketClient) CollectionUtils(org.springframework.util.CollectionUtils) RSocketClient(io.rsocket.core.RSocketClient) Collections(java.util.Collections) Assert(org.springframework.util.Assert) Encoder(org.springframework.core.codec.Encoder) DefaultPayload(io.rsocket.util.DefaultPayload) Payload(io.rsocket.Payload) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapter(org.springframework.core.ReactiveAdapter) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 15 with Payload

use of io.rsocket.Payload in project spring-framework by spring-projects.

the class PayloadUtilsTests method retainAndReleaseWithNettyFactory.

@Test
public void retainAndReleaseWithNettyFactory() {
    Payload payload = ByteBufPayload.create("sample data");
    DataBuffer buffer = PayloadUtils.retainDataAndReleasePayload(payload, this.nettyBufferFactory);
    try {
        assertThat(buffer).isInstanceOf(NettyDataBuffer.class);
        assertThat(((NettyDataBuffer) buffer).getNativeBuffer().refCnt()).isEqualTo(1);
        assertThat(payload.refCnt()).isEqualTo(0);
    } finally {
        DataBufferUtils.release(buffer);
    }
}
Also used : Payload(io.rsocket.Payload) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

Payload (io.rsocket.Payload)26 Test (org.junit.jupiter.api.Test)23 DefaultPayload (io.rsocket.util.DefaultPayload)16 ByteBufPayload (io.rsocket.util.ByteBufPayload)14 DataBuffer (org.springframework.core.io.buffer.DataBuffer)12 NettyDataBuffer (org.springframework.core.io.buffer.NettyDataBuffer)9 RSocket (io.rsocket.RSocket)7 RSocketProxy (io.rsocket.util.RSocketProxy)6 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)6 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)5 MimeType (org.springframework.util.MimeType)5 WellKnownMimeType (io.rsocket.metadata.WellKnownMimeType)4 Publisher (org.reactivestreams.Publisher)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 RSocketConnector (io.rsocket.core.RSocketConnector)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Flux (reactor.core.publisher.Flux)2 Mono (reactor.core.publisher.Mono)2