Search in sources :

Example 1 with Payload

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

the class PayloadInterceptorRSocketTests method metadataPushWhenSecurityContextThenDelegateContext.

@Test
public void metadataPushWhenSecurityContextThenDelegateContext() {
    TestingAuthenticationToken authentication = new TestingAuthenticationToken("user", "password");
    given(this.interceptor.intercept(any(), any())).willAnswer(withAuthenticated(authentication));
    given(this.delegate.metadataPush(any())).willReturn(this.voidResult.mono());
    RSocket assertAuthentication = new RSocketProxy(this.delegate) {

        @Override
        public Mono<Void> metadataPush(Payload payload) {
            return assertAuthentication(authentication).flatMap((a) -> super.metadataPush(payload));
        }
    };
    PayloadInterceptorRSocket interceptor = new PayloadInterceptorRSocket(assertAuthentication, Arrays.asList(this.interceptor), this.metadataMimeType, this.dataMimeType);
    StepVerifier.create(interceptor.metadataPush(this.payload)).verifyComplete();
    verify(this.interceptor).intercept(this.exchange.capture(), any());
    assertThat(this.exchange.getValue().getPayload()).isEqualTo(this.payload);
    verify(this.delegate).metadataPush(this.payload);
    this.voidResult.assertWasSubscribed();
}
Also used : RSocketProxy(io.rsocket.util.RSocketProxy) RSocket(io.rsocket.RSocket) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) Payload(io.rsocket.Payload) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 2 with Payload

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

the class PayloadInterceptorRSocketTests method fireAndForgetWhenSecurityContextThenDelegateContext.

@Test
public void fireAndForgetWhenSecurityContextThenDelegateContext() {
    TestingAuthenticationToken authentication = new TestingAuthenticationToken("user", "password");
    given(this.interceptor.intercept(any(), any())).willAnswer(withAuthenticated(authentication));
    given(this.delegate.fireAndForget(any())).willReturn(Mono.empty());
    RSocket assertAuthentication = new RSocketProxy(this.delegate) {

        @Override
        public Mono<Void> fireAndForget(Payload payload) {
            return assertAuthentication(authentication).flatMap((a) -> super.fireAndForget(payload));
        }
    };
    PayloadInterceptorRSocket interceptor = new PayloadInterceptorRSocket(assertAuthentication, Arrays.asList(this.interceptor), this.metadataMimeType, this.dataMimeType);
    interceptor.fireAndForget(this.payload).block();
    verify(this.interceptor).intercept(this.exchange.capture(), any());
    assertThat(this.exchange.getValue().getPayload()).isEqualTo(this.payload);
    verify(this.delegate).fireAndForget(this.payload);
}
Also used : RSocketProxy(io.rsocket.util.RSocketProxy) RSocket(io.rsocket.RSocket) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) Payload(io.rsocket.Payload) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 3 with Payload

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

the class PayloadInterceptorRSocketTests method requestChannelWhenInterceptorCompletesThenAllPayloadsRetained.

// gh-9345
@Test
public void requestChannelWhenInterceptorCompletesThenAllPayloadsRetained() {
    ExecutorService executors = Executors.newSingleThreadExecutor();
    Payload payload = ByteBufPayload.create("data");
    Payload payloadTwo = ByteBufPayload.create("moredata");
    Payload payloadThree = ByteBufPayload.create("stillmoredata");
    Context ctx = Context.empty();
    Flux<Payload> payloads = this.payloadResult.flux();
    given(this.interceptor.intercept(any(), any())).willReturn(Mono.empty()).willReturn(Mono.error(() -> new AccessDeniedException("Access Denied")));
    given(this.delegate.requestChannel(any())).willAnswer((invocation) -> {
        Flux<Payload> input = invocation.getArgument(0);
        return Flux.from(input).switchOnFirst((signal, innerFlux) -> innerFlux.map(Payload::getDataUtf8).transform((data) -> Flux.<String>create((emitter) -> {
            Runnable run = () -> data.subscribe(new CoreSubscriber<String>() {

                @Override
                public void onSubscribe(Subscription s) {
                    s.request(3);
                }

                @Override
                public void onNext(String s) {
                    emitter.next(s);
                }

                @Override
                public void onError(Throwable t) {
                    emitter.error(t);
                }

                @Override
                public void onComplete() {
                    emitter.complete();
                }
            });
            executors.execute(run);
        })).map(DefaultPayload::create));
    });
    PayloadInterceptorRSocket interceptor = new PayloadInterceptorRSocket(this.delegate, Arrays.asList(this.interceptor), this.metadataMimeType, this.dataMimeType, ctx);
    StepVerifier.create(interceptor.requestChannel(payloads).doOnDiscard(Payload.class, Payload::release)).then(() -> this.payloadResult.assertSubscribers()).then(() -> this.payloadResult.emit(payload, payloadTwo, payloadThree)).assertNext((next) -> assertThat(next.getDataUtf8()).isEqualTo(payload.getDataUtf8())).verifyError(AccessDeniedException.class);
    verify(this.interceptor, times(2)).intercept(this.exchange.capture(), any());
    assertThat(this.exchange.getValue().getPayload()).isEqualTo(payloadTwo);
    verify(this.delegate).requestChannel(any());
}
Also used : Context(reactor.util.context.Context) SecurityContext(org.springframework.security.core.context.SecurityContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) PayloadInterceptor(org.springframework.security.rsocket.api.PayloadInterceptor) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Captor(org.mockito.Captor) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) MimeType(org.springframework.util.MimeType) WellKnownMimeType(io.rsocket.metadata.WellKnownMimeType) Answer(org.mockito.stubbing.Answer) CoreSubscriber(reactor.core.CoreSubscriber) ArgumentCaptor(org.mockito.ArgumentCaptor) ReactiveSecurityContextHolder(org.springframework.security.core.context.ReactiveSecurityContextHolder) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RSocket(io.rsocket.RSocket) BDDMockito.given(org.mockito.BDDMockito.given) DefaultPayload(io.rsocket.util.DefaultPayload) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ExecutorService(java.util.concurrent.ExecutorService) ByteBufPayload(io.rsocket.util.ByteBufPayload) RSocketProxy(io.rsocket.util.RSocketProxy) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) PayloadInterceptorChain(org.springframework.security.rsocket.api.PayloadInterceptorChain) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) MediaType(org.springframework.http.MediaType) Mono(reactor.core.publisher.Mono) PayloadExchangeType(org.springframework.security.rsocket.api.PayloadExchangeType) Mockito.times(org.mockito.Mockito.times) AccessDeniedException(org.springframework.security.access.AccessDeniedException) MimeTypeUtils(org.springframework.util.MimeTypeUtils) Executors(java.util.concurrent.Executors) PublisherProbe(reactor.test.publisher.PublisherProbe) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) PayloadExchange(org.springframework.security.rsocket.api.PayloadExchange) Flux(reactor.core.publisher.Flux) List(java.util.List) Payload(io.rsocket.Payload) SecurityContext(org.springframework.security.core.context.SecurityContext) Subscription(org.reactivestreams.Subscription) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Authentication(org.springframework.security.core.Authentication) Collections(java.util.Collections) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ExecutorService(java.util.concurrent.ExecutorService) CoreSubscriber(reactor.core.CoreSubscriber) DefaultPayload(io.rsocket.util.DefaultPayload) ByteBufPayload(io.rsocket.util.ByteBufPayload) Payload(io.rsocket.Payload) Subscription(org.reactivestreams.Subscription) Test(org.junit.jupiter.api.Test)

Example 4 with Payload

use of io.rsocket.Payload 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"))));
}
Also used : PayloadUtils(org.springframework.messaging.rsocket.PayloadUtils) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DestinationPatternsMessageCondition(org.springframework.messaging.handler.DestinationPatternsMessageCondition) AtomicReference(java.util.concurrent.atomic.AtomicReference) MetadataExtractor(org.springframework.messaging.rsocket.MetadataExtractor) RouteMatcher(org.springframework.util.RouteMatcher) HandlerMethodReturnValueHandler(org.springframework.messaging.handler.invocation.reactive.HandlerMethodReturnValueHandler) MimeType(org.springframework.util.MimeType) RSocket(io.rsocket.RSocket) Map(java.util.Map) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) Nullable(org.springframework.lang.Nullable) Message(org.springframework.messaging.Message) RSocketStrategies(org.springframework.messaging.rsocket.RSocketStrategies) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) RSocketRequester(org.springframework.messaging.rsocket.RSocketRequester) FrameType(io.rsocket.frame.FrameType) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) MessageHeaders(org.springframework.messaging.MessageHeaders) Flux(reactor.core.publisher.Flux) Payload(io.rsocket.Payload) ReactiveMessageHandler(org.springframework.messaging.ReactiveMessageHandler) ConnectionSetupPayload(io.rsocket.ConnectionSetupPayload) MessageBuilder(org.springframework.messaging.support.MessageBuilder) Assert(org.springframework.util.Assert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Flux(reactor.core.publisher.Flux) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageHeaders(org.springframework.messaging.MessageHeaders) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 5 with Payload

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

the class DefaultRSocketRequesterBuilder method initConnector.

@SuppressWarnings("deprecation")
private RSocketConnector initConnector(List<RSocketConnectorConfigurer> connectorConfigurers, MimeType metaMimeType, MimeType dataMimeType, RSocketStrategies rsocketStrategies) {
    RSocketConnector connector = RSocketConnector.create();
    connectorConfigurers.forEach(c -> c.configure(connector));
    if (rsocketStrategies.dataBufferFactory() instanceof NettyDataBufferFactory) {
        connector.payloadDecoder(PayloadDecoder.ZERO_COPY);
    }
    connector.metadataMimeType(metaMimeType.toString());
    connector.dataMimeType(dataMimeType.toString());
    Mono<Payload> setupPayloadMono = getSetupPayload(dataMimeType, metaMimeType, rsocketStrategies);
    if (setupPayloadMono != EMPTY_SETUP_PAYLOAD) {
        connector.setupPayload(setupPayloadMono);
    }
    return connector;
}
Also used : RSocketConnector(io.rsocket.core.RSocketConnector) DefaultPayload(io.rsocket.util.DefaultPayload) Payload(io.rsocket.Payload) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory)

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