Search in sources :

Example 1 with CompositeMetadata

use of io.rsocket.metadata.CompositeMetadata 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();
}
Also used : CompositeMetadata(io.rsocket.metadata.CompositeMetadata) MimeType(org.springframework.util.MimeType) WellKnownMimeType(io.rsocket.metadata.WellKnownMimeType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) Test(org.junit.jupiter.api.Test)

Example 2 with CompositeMetadata

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

the class MetadataEncoderTests method defaultDataBufferFactory.

@Test
public void defaultDataBufferFactory() {
    DefaultDataBufferFactory bufferFactory = DefaultDataBufferFactory.sharedInstance;
    RSocketStrategies strategies = RSocketStrategies.builder().dataBufferFactory(bufferFactory).build();
    DataBuffer buffer = new MetadataEncoder(COMPOSITE_METADATA, strategies).route("toA").encode().block();
    ByteBuf byteBuf = new NettyDataBufferFactory(ByteBufAllocator.DEFAULT).wrap(buffer.asByteBuffer()).getNativeBuffer();
    CompositeMetadata entries = new CompositeMetadata(byteBuf, 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()).isFalse();
}
Also used : CompositeMetadata(io.rsocket.metadata.CompositeMetadata) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ByteBuf(io.netty.buffer.ByteBuf) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) Test(org.junit.jupiter.api.Test)

Example 3 with CompositeMetadata

use of io.rsocket.metadata.CompositeMetadata in project spring-security by spring-projects.

the class BearerPayloadExchangeConverter method convert.

@Override
public Mono<Authentication> convert(PayloadExchange exchange) {
    ByteBuf metadata = exchange.getPayload().metadata();
    CompositeMetadata compositeMetadata = new CompositeMetadata(metadata, false);
    for (CompositeMetadata.Entry entry : compositeMetadata) {
        if (BEARER_MIME_TYPE_VALUE.equals(entry.getMimeType())) {
            ByteBuf content = entry.getContent();
            String token = content.toString(StandardCharsets.UTF_8);
            return Mono.just(new BearerTokenAuthenticationToken(token));
        }
    }
    return Mono.empty();
}
Also used : CompositeMetadata(io.rsocket.metadata.CompositeMetadata) ByteBuf(io.netty.buffer.ByteBuf) BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken)

Aggregations

CompositeMetadata (io.rsocket.metadata.CompositeMetadata)3 ByteBuf (io.netty.buffer.ByteBuf)2 Test (org.junit.jupiter.api.Test)2 DataBuffer (org.springframework.core.io.buffer.DataBuffer)2 NettyDataBuffer (org.springframework.core.io.buffer.NettyDataBuffer)2 WellKnownMimeType (io.rsocket.metadata.WellKnownMimeType)1 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)1 NettyDataBufferFactory (org.springframework.core.io.buffer.NettyDataBufferFactory)1 BearerTokenAuthenticationToken (org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken)1 MimeType (org.springframework.util.MimeType)1