Search in sources :

Example 6 with NettyDataBufferFactory

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

the class ReactorHttpHandlerAdapter method apply.

@Override
public Mono<Void> apply(HttpServerRequest reactorRequest, HttpServerResponse reactorResponse) {
    NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(reactorResponse.alloc());
    try {
        ReactorServerHttpRequest request = new ReactorServerHttpRequest(reactorRequest, bufferFactory);
        ServerHttpResponse response = new ReactorServerHttpResponse(reactorResponse, bufferFactory);
        if (request.getMethod() == HttpMethod.HEAD) {
            response = new HttpHeadResponseDecorator(response);
        }
        return this.httpHandler.handle(request, response).doOnError(ex -> logger.trace(request.getLogPrefix() + "Failed to complete: " + ex.getMessage())).doOnSuccess(aVoid -> logger.trace(request.getLogPrefix() + "Handling completed"));
    } catch (URISyntaxException ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to get request URI: " + ex.getMessage());
        }
        reactorResponse.status(HttpResponseStatus.BAD_REQUEST);
        return Mono.empty();
    }
}
Also used : HttpLogging(org.springframework.http.HttpLogging) HttpServerRequest(reactor.netty.http.server.HttpServerRequest) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) HttpMethod(org.springframework.http.HttpMethod) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Log(org.apache.commons.logging.Log) Mono(reactor.core.publisher.Mono) Assert(org.springframework.util.Assert) URISyntaxException(java.net.URISyntaxException) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory)

Example 7 with NettyDataBufferFactory

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

the class ReactorNettyRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
    ServerHttpResponse response = exchange.getResponse();
    HttpServerResponse reactorResponse = ServerHttpResponseDecorator.getNativeResponse(response);
    HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
    NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory();
    URI uri = exchange.getRequest().getURI();
    // Trigger WebFlux preCommit actions and upgrade
    return response.setComplete().then(Mono.defer(() -> {
        WebsocketServerSpec spec = buildSpec(subProtocol);
        return reactorResponse.sendWebsocket((in, out) -> {
            ReactorNettyWebSocketSession session = new ReactorNettyWebSocketSession(in, out, handshakeInfo, bufferFactory, spec.maxFramePayloadLength());
            return handler.handle(session).checkpoint(uri + " [ReactorNettyRequestUpgradeStrategy]");
        }, spec);
    }));
}
Also used : HttpServerResponse(reactor.netty.http.server.HttpServerResponse) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) RequestUpgradeStrategy(org.springframework.web.reactive.socket.server.RequestUpgradeStrategy) Mono(reactor.core.publisher.Mono) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) Supplier(java.util.function.Supplier) WebsocketServerSpec(reactor.netty.http.server.WebsocketServerSpec) ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpResponseDecorator(org.springframework.http.server.reactive.ServerHttpResponseDecorator) Nullable(org.springframework.lang.Nullable) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) Assert(org.springframework.util.Assert) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) WebsocketServerSpec(reactor.netty.http.server.WebsocketServerSpec) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) URI(java.net.URI) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 8 with NettyDataBufferFactory

use of org.springframework.core.io.buffer.NettyDataBufferFactory 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)

Example 9 with NettyDataBufferFactory

use of org.springframework.core.io.buffer.NettyDataBufferFactory 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 10 with NettyDataBufferFactory

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

the class DefaultRSocketRequesterBuilderTests method frameDecoderMatchesDataBufferFactory.

@Test
public void frameDecoderMatchesDataBufferFactory() throws Exception {
    testPayloadDecoder(new NettyDataBufferFactory(ByteBufAllocator.DEFAULT), PayloadDecoder.ZERO_COPY);
    testPayloadDecoder(DefaultDataBufferFactory.sharedInstance, PayloadDecoder.DEFAULT);
}
Also used : NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) Test(org.junit.jupiter.api.Test)

Aggregations

NettyDataBufferFactory (org.springframework.core.io.buffer.NettyDataBufferFactory)15 Mono (reactor.core.publisher.Mono)6 ByteBuf (io.netty.buffer.ByteBuf)5 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)5 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)4 URI (java.net.URI)3 Log (org.apache.commons.logging.Log)3 Test (org.junit.jupiter.api.Test)3 Assert (org.springframework.util.Assert)3 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)2 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DataBuffer (org.springframework.core.io.buffer.DataBuffer)2 WebSocketHandler (org.springframework.web.reactive.socket.WebSocketHandler)2 ReactorNettyWebSocketSession (org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession)2 RxNettyWebSocketSession (org.springframework.web.reactive.socket.adapter.RxNettyWebSocketSession)2 HttpServerResponse (reactor.netty.http.server.HttpServerResponse)2 Observable (rx.Observable)2