Search in sources :

Example 1 with NettyDataBufferFactory

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

the class RxNettyHttpHandlerAdapter method handle.

@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> nativeRequest, HttpServerResponse<ByteBuf> nativeResponse) {
    Channel channel = nativeResponse.unsafeNettyChannel();
    NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(channel.alloc());
    InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
    RxNettyServerHttpRequest request = new RxNettyServerHttpRequest(nativeRequest, bufferFactory, remoteAddress);
    RxNettyServerHttpResponse response = new RxNettyServerHttpResponse(nativeResponse, bufferFactory);
    Publisher<Void> result = this.httpHandler.handle(request, response).otherwise(ex -> {
        logger.error("Could not complete request", ex);
        nativeResponse.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
        return Mono.empty();
    }).doOnSuccess(aVoid -> logger.debug("Successfully completed request"));
    return RxReactiveStreams.toObservable(result);
}
Also used : HttpServerResponse(io.reactivex.netty.protocol.http.server.HttpServerResponse) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) Publisher(org.reactivestreams.Publisher) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Mono(reactor.core.publisher.Mono) InetSocketAddress(java.net.InetSocketAddress) Observable(rx.Observable) Channel(io.netty.channel.Channel) RxReactiveStreams(rx.RxReactiveStreams) ByteBuf(io.netty.buffer.ByteBuf) HttpServerRequest(io.reactivex.netty.protocol.http.server.HttpServerRequest) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) RequestHandler(io.reactivex.netty.protocol.http.server.RequestHandler) Assert(org.springframework.util.Assert) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory)

Example 2 with NettyDataBufferFactory

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

the class RxNettyRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
    RxNettyServerHttpResponse response = (RxNettyServerHttpResponse) exchange.getResponse();
    HttpServerResponse<?> rxNettyResponse = response.getRxNettyResponse();
    HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
    NettyDataBufferFactory factory = (NettyDataBufferFactory) response.bufferFactory();
    WebSocketHandshaker handshaker = rxNettyResponse.acceptWebSocketUpgrade(conn -> {
        RxNettyWebSocketSession session = new RxNettyWebSocketSession(conn, info, factory);
        String name = HttpHandlerNames.WsServerDecoder.getName();
        session.aggregateFrames(rxNettyResponse.unsafeNettyChannel(), name);
        return RxReactiveStreams.toObservable(handler.handle(session));
    });
    if (subProtocol.isPresent()) {
        handshaker = handshaker.subprotocol(subProtocol.get());
    } else {
        // TODO: https://github.com/reactor/reactor-netty/issues/20
        handshaker = handshaker.subprotocol(new String[0]);
    }
    return Mono.from(RxReactiveStreams.toPublisher(handshaker));
}
Also used : WebSocketHandshaker(io.reactivex.netty.protocol.http.ws.server.WebSocketHandshaker) RxNettyServerHttpResponse(org.springframework.http.server.reactive.RxNettyServerHttpResponse) RxNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.RxNettyWebSocketSession) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 3 with NettyDataBufferFactory

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

the class RxNettyWebSocketClient method executeInternal.

@SuppressWarnings("cast")
private Observable<Void> executeInternal(URI url, HttpHeaders headers, WebSocketHandler handler) {
    String[] protocols = beforeHandshake(url, headers, handler);
    return createRequest(url, headers, protocols).flatMap(response -> {
        Observable<WebSocketConnection> conn = response.getWebSocketConnection();
        return (Observable<Tuple2<WebSocketResponse<ByteBuf>, WebSocketConnection>>) Observable.zip(Observable.just(response), conn, Tuples::of);
    }).flatMap(tuple -> {
        WebSocketResponse<ByteBuf> response = tuple.getT1();
        WebSocketConnection conn = tuple.getT2();
        HandshakeInfo info = afterHandshake(url, toHttpHeaders(response));
        ByteBufAllocator allocator = response.unsafeNettyChannel().alloc();
        NettyDataBufferFactory factory = new NettyDataBufferFactory(allocator);
        RxNettyWebSocketSession session = new RxNettyWebSocketSession(conn, info, factory);
        session.aggregateFrames(response.unsafeNettyChannel(), WsClientDecoder.getName());
        return RxReactiveStreams.toObservable(handler.handle(session));
    });
}
Also used : NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Tuples(reactor.util.function.Tuples) Tuple2(reactor.util.function.Tuple2) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Observable(rx.Observable) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) HttpClient(io.reactivex.netty.protocol.http.client.HttpClient) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) HttpClientRequest(io.reactivex.netty.protocol.http.client.HttpClientRequest) RxEventLoopProvider(io.reactivex.netty.threads.RxEventLoopProvider) WebSocketResponse(io.reactivex.netty.protocol.http.ws.client.WebSocketResponse) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) HttpHeaders(org.springframework.http.HttpHeaders) ObjectUtils(org.springframework.util.ObjectUtils) RxNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.RxNettyWebSocketSession) WebSocketConnection(io.reactivex.netty.protocol.http.ws.WebSocketConnection) Mono(reactor.core.publisher.Mono) WebSocketRequest(io.reactivex.netty.protocol.http.ws.client.WebSocketRequest) List(java.util.List) RxReactiveStreams(rx.RxReactiveStreams) WsClientDecoder(io.reactivex.netty.protocol.http.HttpHandlerNames.WsClientDecoder) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) WebSocketConnection(io.reactivex.netty.protocol.http.ws.WebSocketConnection) RxNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.RxNettyWebSocketSession) ByteBuf(io.netty.buffer.ByteBuf) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) Observable(rx.Observable) Tuple2(reactor.util.function.Tuple2) Tuples(reactor.util.function.Tuples) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 4 with NettyDataBufferFactory

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

the class BearerTokenAuthenticationEncoder method encodeValue.

@Override
public DataBuffer encodeValue(BearerTokenMetadata credentials, DataBufferFactory bufferFactory, ResolvableType valueType, MimeType mimeType, Map<String, Object> hints) {
    String token = credentials.getToken();
    NettyDataBufferFactory factory = nettyFactory(bufferFactory);
    ByteBufAllocator allocator = factory.getByteBufAllocator();
    ByteBuf simpleAuthentication = AuthMetadataCodec.encodeBearerMetadata(allocator, token.toCharArray());
    return factory.wrap(simpleAuthentication);
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) ByteBuf(io.netty.buffer.ByteBuf)

Example 5 with NettyDataBufferFactory

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

the class SimpleAuthenticationEncoder 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();
    NettyDataBufferFactory factory = nettyFactory(bufferFactory);
    ByteBufAllocator allocator = factory.getByteBufAllocator();
    ByteBuf simpleAuthentication = AuthMetadataCodec.encodeSimpleMetadata(allocator, username.toCharArray(), password.toCharArray());
    return factory.wrap(simpleAuthentication);
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) ByteBuf(io.netty.buffer.ByteBuf)

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