Search in sources :

Example 1 with HandshakeInfo

use of org.springframework.web.reactive.socket.HandshakeInfo in project spring-framework by spring-projects.

the class RxNettyRequestUpgradeStrategy method getHandshakeInfo.

private HandshakeInfo getHandshakeInfo(ServerWebExchange exchange, Optional<String> protocol) {
    ServerHttpRequest request = exchange.getRequest();
    Mono<Principal> principal = exchange.getPrincipal();
    return new HandshakeInfo(request.getURI(), request.getHeaders(), principal, protocol);
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Principal(java.security.Principal) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 2 with HandshakeInfo

use of org.springframework.web.reactive.socket.HandshakeInfo 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 HandshakeInfo

use of org.springframework.web.reactive.socket.HandshakeInfo in project spring-framework by spring-projects.

the class UndertowRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
    ServerHttpRequest request = exchange.getRequest();
    Assert.isInstanceOf(UndertowServerHttpRequest.class, request, "UndertowServerHttpRequest required");
    HttpServerExchange httpExchange = ((UndertowServerHttpRequest) request).getUndertowExchange();
    Set<String> protocols = subProtocol.map(Collections::singleton).orElse(Collections.emptySet());
    Hybi13Handshake handshake = new Hybi13Handshake(protocols, false);
    List<Handshake> handshakes = Collections.singletonList(handshake);
    URI url = request.getURI();
    HttpHeaders headers = request.getHeaders();
    Mono<Principal> principal = exchange.getPrincipal();
    HandshakeInfo info = new HandshakeInfo(url, headers, principal, subProtocol);
    DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory();
    try {
        DefaultCallback callback = new DefaultCallback(info, handler, bufferFactory);
        new WebSocketProtocolHandshakeHandler(handshakes, callback).handleRequest(httpExchange);
    } catch (Exception ex) {
        return Mono.error(ex);
    }
    return Mono.empty();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) UndertowServerHttpRequest(org.springframework.http.server.reactive.UndertowServerHttpRequest) URI(java.net.URI) HttpServerExchange(io.undertow.server.HttpServerExchange) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) UndertowServerHttpRequest(org.springframework.http.server.reactive.UndertowServerHttpRequest) Principal(java.security.Principal) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) Handshake(io.undertow.websockets.core.protocol.Handshake)

Example 4 with HandshakeInfo

use of org.springframework.web.reactive.socket.HandshakeInfo 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 5 with HandshakeInfo

use of org.springframework.web.reactive.socket.HandshakeInfo in project spring-framework by spring-projects.

the class StandardWebSocketClient method createEndpoint.

private StandardWebSocketHandlerAdapter createEndpoint(URI url, WebSocketHandler handler, MonoProcessor<Void> completion, DefaultConfigurator configurator) {
    return new StandardWebSocketHandlerAdapter(handler, session -> {
        HttpHeaders responseHeaders = configurator.getResponseHeaders();
        HandshakeInfo info = afterHandshake(url, responseHeaders);
        return new StandardWebSocketSession(session, info, this.bufferFactory, completion);
    });
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) StandardWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter) StandardWebSocketSession(org.springframework.web.reactive.socket.adapter.StandardWebSocketSession) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Aggregations

HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)25 HttpHeaders (org.springframework.http.HttpHeaders)9 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)9 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)7 WebSocketHandler (org.springframework.web.reactive.socket.WebSocketHandler)7 Principal (java.security.Principal)6 Mono (reactor.core.publisher.Mono)6 URI (java.net.URI)5 Supplier (java.util.function.Supplier)5 NettyDataBufferFactory (org.springframework.core.io.buffer.NettyDataBufferFactory)5 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)5 Nullable (org.springframework.lang.Nullable)5 ServletServerHttpRequest (org.springframework.http.server.reactive.ServletServerHttpRequest)4 Collections (java.util.Collections)3 ServerHttpRequestDecorator (org.springframework.http.server.reactive.ServerHttpRequestDecorator)3 ServerHttpResponseDecorator (org.springframework.http.server.reactive.ServerHttpResponseDecorator)3 Assert (org.springframework.util.Assert)3 ContextWebSocketHandler (org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler)3 JettyWebSocketHandlerAdapter (org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter)3 JettyWebSocketSession (org.springframework.web.reactive.socket.adapter.JettyWebSocketSession)3