Search in sources :

Example 21 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, Optional<String> subProtocol) {
    ReactorServerHttpResponse response = (ReactorServerHttpResponse) exchange.getResponse();
    HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
    NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory();
    return response.getReactorResponse().sendWebsocket(subProtocol.orElse(null), (in, out) -> handler.handle(new ReactorNettyWebSocketSession(in, out, info, bufferFactory)));
}
Also used : ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) ReactorServerHttpResponse(org.springframework.http.server.reactive.ReactorServerHttpResponse) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 22 with HandshakeInfo

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

the class ReactorNettyRequestUpgradeStrategy 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 23 with HandshakeInfo

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

the class JettyRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    HttpServletRequest servletRequest = ServerHttpRequestDecorator.getNativeRequest(request);
    HttpServletResponse servletResponse = ServerHttpResponseDecorator.getNativeResponse(response);
    ServletContext servletContext = servletRequest.getServletContext();
    HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
    DataBufferFactory factory = response.bufferFactory();
    // Trigger WebFlux preCommit actions before upgrade
    return exchange.getResponse().setComplete().then(Mono.deferContextual(contextView -> {
        JettyWebSocketHandlerAdapter adapter = new JettyWebSocketHandlerAdapter(ContextWebSocketHandler.decorate(handler, contextView), session -> new JettyWebSocketSession(session, handshakeInfo, factory));
        JettyWebSocketCreator webSocketCreator = (upgradeRequest, upgradeResponse) -> {
            if (subProtocol != null) {
                upgradeResponse.setAcceptedSubProtocol(subProtocol);
            }
            return adapter;
        };
        JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
        try {
            container.upgrade(webSocketCreator, servletRequest, servletResponse);
        } catch (Exception ex) {
            return Mono.error(ex);
        }
        return Mono.empty();
    }));
}
Also used : JettyWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) 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) Supplier(java.util.function.Supplier) ServerWebExchange(org.springframework.web.server.ServerWebExchange) JettyWebSocketCreator(org.eclipse.jetty.websocket.server.JettyWebSocketCreator) JettyWebSocketServerContainer(org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) ServerHttpRequestDecorator(org.springframework.http.server.reactive.ServerHttpRequestDecorator) ServerHttpResponseDecorator(org.springframework.http.server.reactive.ServerHttpResponseDecorator) Nullable(org.springframework.lang.Nullable) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) ContextWebSocketHandler(org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler) ServletContext(jakarta.servlet.ServletContext) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) JettyWebSocketSession(org.springframework.web.reactive.socket.adapter.JettyWebSocketSession) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) JettyWebSocketSession(org.springframework.web.reactive.socket.adapter.JettyWebSocketSession) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) JettyWebSocketCreator(org.eclipse.jetty.websocket.server.JettyWebSocketCreator) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) JettyWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) JettyWebSocketServerContainer(org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer) ServletContext(jakarta.servlet.ServletContext) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 24 with HandshakeInfo

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

the class ReactorNettyWebSocketClient method execute.

@Override
public Mono<Void> execute(URI url, HttpHeaders requestHeaders, WebSocketHandler handler) {
    String protocols = StringUtils.collectionToCommaDelimitedString(handler.getSubProtocols());
    return getHttpClient().headers(nettyHeaders -> setNettyHeaders(requestHeaders, nettyHeaders)).websocket(buildSpec(protocols)).uri(url.toString()).handle((inbound, outbound) -> {
        HttpHeaders responseHeaders = toHttpHeaders(inbound);
        String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol");
        HandshakeInfo info = new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol);
        NettyDataBufferFactory factory = new NettyDataBufferFactory(outbound.alloc());
        WebSocketSession session = new ReactorNettyWebSocketSession(inbound, outbound, info, factory, getMaxFramePayloadLength());
        if (logger.isDebugEnabled()) {
            logger.debug("Started session '" + session.getId() + "' for " + url);
        }
        return handler.handle(session).checkpoint(url + " [ReactorNettyWebSocketClient]");
    }).doOnRequest(n -> {
        if (logger.isDebugEnabled()) {
            logger.debug("Connecting to " + url);
        }
    }).next();
}
Also used : NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) WebsocketClientSpec(reactor.netty.http.client.WebsocketClientSpec) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) HttpHeaders(org.springframework.http.HttpHeaders) Mono(reactor.core.publisher.Mono) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) Supplier(java.util.function.Supplier) WebsocketInbound(reactor.netty.http.websocket.WebsocketInbound) Log(org.apache.commons.logging.Log) Nullable(org.springframework.lang.Nullable) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) LogFactory(org.apache.commons.logging.LogFactory) HttpClient(reactor.netty.http.client.HttpClient) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) HttpHeaders(org.springframework.http.HttpHeaders) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) ReactorNettyWebSocketSession(org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession)

Example 25 with HandshakeInfo

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

the class StandardWebSocketClient method createHandshakeInfo.

private HandshakeInfo createHandshakeInfo(URI url, DefaultConfigurator configurator) {
    HttpHeaders responseHeaders = configurator.getResponseHeaders();
    String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol");
    return new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) 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