Search in sources :

Example 6 with HandshakeInfo

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

the class UndertowWebSocketClient method handleChannel.

private void handleChannel(URI url, WebSocketHandler handler, MonoProcessor<Void> completion, DefaultNegotiation negotiation, WebSocketChannel channel) {
    HandshakeInfo info = afterHandshake(url, negotiation.getResponseHeaders());
    UndertowWebSocketSession session = new UndertowWebSocketSession(channel, info, bufferFactory, completion);
    UndertowWebSocketHandlerAdapter adapter = new UndertowWebSocketHandlerAdapter(session);
    channel.getReceiveSetter().set(adapter);
    channel.resumeReceives();
    handler.handle(session).subscribe(session);
}
Also used : UndertowWebSocketSession(org.springframework.web.reactive.socket.adapter.UndertowWebSocketSession) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) UndertowWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.UndertowWebSocketHandlerAdapter)

Example 7 with HandshakeInfo

use of org.springframework.web.reactive.socket.HandshakeInfo in project spring-cloud-gateway by spring-cloud.

the class WebSocketIntegrationTests method subProtocol.

@Test
public void subProtocol() throws Exception {
    String protocol = "echo-v1";
    String protocol2 = "echo-v2";
    AtomicReference<HandshakeInfo> infoRef = new AtomicReference<>();
    MonoProcessor<Object> output = MonoProcessor.create();
    client.execute(getUrl("/sub-protocol"), new WebSocketHandler() {

        @Override
        public List<String> getSubProtocols() {
            return Arrays.asList(protocol, protocol2);
        }

        @Override
        public Mono<Void> handle(WebSocketSession session) {
            infoRef.set(session.getHandshakeInfo());
            return session.receive().map(WebSocketMessage::getPayloadAsText).subscribeWith(output).then();
        }
    }).block(Duration.ofMillis(5000));
    HandshakeInfo info = infoRef.get();
    assertThat(info.getHeaders().getFirst("Upgrade")).isEqualToIgnoringCase("websocket");
    assertThat(info.getHeaders().getFirst("Sec-WebSocket-Protocol")).isEqualTo(protocol);
    assertThat(info.getSubProtocol()).as("Wrong protocol accepted").isEqualTo(protocol);
    assertThat(output.block(Duration.ofSeconds(5))).as("Wrong protocol detected on the server side").isEqualTo(protocol);
}
Also used : WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) Test(org.junit.Test)

Example 8 with HandshakeInfo

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

the class HandshakeWebSocketServiceTests method sessionAttributePredicate.

@Test
public void sessionAttributePredicate() {
    MockWebSession session = new MockWebSession();
    session.getAttributes().put("a1", "v1");
    session.getAttributes().put("a2", "v2");
    session.getAttributes().put("a3", "v3");
    session.getAttributes().put("a4", "v4");
    session.getAttributes().put("a5", "v5");
    MockServerHttpRequest request = initHandshakeRequest();
    MockServerWebExchange exchange = MockServerWebExchange.builder(request).session(session).build();
    TestRequestUpgradeStrategy upgradeStrategy = new TestRequestUpgradeStrategy();
    HandshakeWebSocketService service = new HandshakeWebSocketService(upgradeStrategy);
    service.setSessionAttributePredicate(name -> Arrays.asList("a1", "a3", "a5").contains(name));
    service.handleRequest(exchange, mock(WebSocketHandler.class)).block();
    HandshakeInfo info = upgradeStrategy.handshakeInfo;
    assertThat(info).isNotNull();
    Map<String, Object> attributes = info.getAttributes();
    assertThat(attributes).hasSize(3).containsEntry("a1", "v1").containsEntry("a3", "v3").containsEntry("a5", "v5");
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockWebSession(org.springframework.web.testfixture.server.MockWebSession) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) Test(org.junit.jupiter.api.Test)

Example 9 with HandshakeInfo

use of org.springframework.web.reactive.socket.HandshakeInfo 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 10 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, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
    HttpServerExchange httpExchange = ServerHttpRequestDecorator.getNativeRequest(exchange.getRequest());
    Set<String> protocols = (subProtocol != null ? Collections.singleton(subProtocol) : Collections.emptySet());
    Hybi13Handshake handshake = new Hybi13Handshake(protocols, false);
    List<Handshake> handshakes = Collections.singletonList(handshake);
    HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
    DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory();
    // Trigger WebFlux preCommit actions and upgrade
    return exchange.getResponse().setComplete().then(Mono.deferContextual(contextView -> {
        DefaultCallback callback = new DefaultCallback(handshakeInfo, ContextWebSocketHandler.decorate(handler, contextView), bufferFactory);
        try {
            new WebSocketProtocolHandshakeHandler(handshakes, callback).handleRequest(httpExchange);
        } catch (Exception ex) {
            return Mono.error(ex);
        }
        return Mono.empty();
    }));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) HttpServerExchange(io.undertow.server.HttpServerExchange) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) RequestUpgradeStrategy(org.springframework.web.reactive.socket.server.RequestUpgradeStrategy) Set(java.util.Set) Mono(reactor.core.publisher.Mono) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) Supplier(java.util.function.Supplier) UndertowWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.UndertowWebSocketHandlerAdapter) ServerWebExchange(org.springframework.web.server.ServerWebExchange) List(java.util.List) UndertowWebSocketSession(org.springframework.web.reactive.socket.adapter.UndertowWebSocketSession) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Handshake(io.undertow.websockets.core.protocol.Handshake) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) ServerHttpRequestDecorator(org.springframework.http.server.reactive.ServerHttpRequestDecorator) Nullable(org.springframework.lang.Nullable) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) ContextWebSocketHandler(org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler) Collections(java.util.Collections) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) Handshake(io.undertow.websockets.core.protocol.Handshake)

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