Search in sources :

Example 26 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-security by spring-projects.

the class HttpStatusServerAccessDeniedHandler method handle.

@Override
public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException ex) {
    return Mono.defer(() -> Mono.just(exchange.getResponse())).flatMap((response) -> {
        response.setStatusCode(this.httpStatus);
        response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
        DataBufferFactory dataBufferFactory = response.bufferFactory();
        DataBuffer buffer = dataBufferFactory.wrap(ex.getMessage().getBytes(Charset.defaultCharset()));
        return response.writeWith(Mono.just(buffer)).doOnError((error) -> DataBufferUtils.release(buffer));
    });
}
Also used : DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 27 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-security by spring-projects.

the class AuthenticationPayloadInterceptorTests method createRequestPayload.

private Payload createRequestPayload() {
    UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("user", "password");
    BasicAuthenticationEncoder encoder = new BasicAuthenticationEncoder();
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    ResolvableType elementType = ResolvableType.forClass(UsernamePasswordMetadata.class);
    MimeType mimeType = UsernamePasswordMetadata.BASIC_AUTHENTICATION_MIME_TYPE;
    Map<String, Object> hints = null;
    DataBuffer dataBuffer = encoder.encodeValue(credentials, factory, elementType, mimeType, hints);
    ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
    CompositeByteBuf metadata = allocator.compositeBuffer();
    CompositeMetadataCodec.encodeAndAddMetadata(metadata, allocator, mimeType.toString(), NettyDataBufferFactory.toByteBuf(dataBuffer));
    return DefaultPayload.create(allocator.buffer(), metadata);
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) UsernamePasswordMetadata(org.springframework.security.rsocket.metadata.UsernamePasswordMetadata) BasicAuthenticationEncoder(org.springframework.security.rsocket.metadata.BasicAuthenticationEncoder) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ResolvableType(org.springframework.core.ResolvableType) MimeType(org.springframework.util.MimeType) WellKnownMimeType(io.rsocket.metadata.WellKnownMimeType) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 28 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-security by spring-projects.

the class BasicAuthenticationEncoder method encodeValue.

@Override
public DataBuffer encodeValue(UsernamePasswordMetadata credentials, DataBufferFactory bufferFactory, ResolvableType valueType, MimeType mimeType, Map<String, Object> hints) {
    String username = credentials.getUsername();
    String password = credentials.getPassword();
    byte[] usernameBytes = username.getBytes(StandardCharsets.UTF_8);
    byte[] usernameBytesLengthBytes = ByteBuffer.allocate(4).putInt(usernameBytes.length).array();
    DataBuffer metadata = bufferFactory.allocateBuffer();
    boolean release = true;
    try {
        metadata.write(usernameBytesLengthBytes);
        metadata.write(usernameBytes);
        metadata.write(password.getBytes(StandardCharsets.UTF_8));
        release = false;
        return metadata;
    } finally {
        if (release) {
            DataBufferUtils.release(metadata);
        }
    }
}
Also used : DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 29 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class ChannelSendOperatorTests method errorFromWriteFunctionWhileItemCached.

// gh-22720
@Test
public void errorFromWriteFunctionWhileItemCached() {
    // 1. First item received
    // 2. writeFunction applied and writeCompletionBarrier subscribed to it
    // 3. writeFunction fails, e.g. to flush status and headers, before request(n) from server
    LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
    ChannelSendOperator<DataBuffer> operator = new ChannelSendOperator<>(Flux.create(sink -> {
        DataBuffer dataBuffer = bufferFactory.allocateBuffer();
        dataBuffer.write("foo", StandardCharsets.UTF_8);
        sink.next(dataBuffer);
    }), publisher -> {
        publisher.subscribe(new ZeroDemandSubscriber());
        return Mono.error(new IllegalStateException("err"));
    });
    StepVerifier.create(operator).expectErrorMessage("err").verify(Duration.ofSeconds(5));
    bufferFactory.checkForLeaks();
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Publisher(org.reactivestreams.Publisher) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) Signal(reactor.core.publisher.Signal) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) BaseSubscriber(reactor.core.publisher.BaseSubscriber) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) Subscriber(org.reactivestreams.Subscriber) LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 30 with DataBuffer

use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.

the class ChannelSendOperatorTests method cancelWhileItemCached.

// gh-22720
@Test
public void cancelWhileItemCached() {
    LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
    ChannelSendOperator<DataBuffer> operator = new ChannelSendOperator<>(Mono.fromCallable(() -> {
        DataBuffer dataBuffer = bufferFactory.allocateBuffer();
        dataBuffer.write("foo", StandardCharsets.UTF_8);
        return dataBuffer;
    }), publisher -> {
        ZeroDemandSubscriber subscriber = new ZeroDemandSubscriber();
        publisher.subscribe(subscriber);
        return Mono.never();
    });
    BaseSubscriber<Void> subscriber = new BaseSubscriber<>() {
    };
    operator.subscribe(subscriber);
    subscriber.cancel();
    bufferFactory.checkForLeaks();
}
Also used : BaseSubscriber(reactor.core.publisher.BaseSubscriber) LeakAwareDataBufferFactory(org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

DataBuffer (org.springframework.core.io.buffer.DataBuffer)230 Test (org.junit.jupiter.api.Test)111 Mono (reactor.core.publisher.Mono)55 Flux (reactor.core.publisher.Flux)52 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)49 ResolvableType (org.springframework.core.ResolvableType)41 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)36 StepVerifier (reactor.test.StepVerifier)36 List (java.util.List)34 Test (org.junit.Test)30 DataBufferUtils (org.springframework.core.io.buffer.DataBufferUtils)29 IOException (java.io.IOException)28 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)27 Map (java.util.Map)25 NettyDataBuffer (org.springframework.core.io.buffer.NettyDataBuffer)25 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)24 HttpHeaders (org.springframework.http.HttpHeaders)24 MediaType (org.springframework.http.MediaType)24 ByteBuffer (java.nio.ByteBuffer)23