Search in sources :

Example 1 with TomcatWebSocketSession

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

the class TomcatRequestUpgradeStrategy method upgrade.

// for old doUpgrade variant in Tomcat 9.0.55
@SuppressWarnings("deprecation")
@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);
    HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
    DataBufferFactory bufferFactory = response.bufferFactory();
    // Trigger WebFlux preCommit actions and upgrade
    return exchange.getResponse().setComplete().then(Mono.deferContextual(contextView -> {
        Endpoint endpoint = new StandardWebSocketHandlerAdapter(ContextWebSocketHandler.decorate(handler, contextView), session -> new TomcatWebSocketSession(session, handshakeInfo, bufferFactory));
        String requestURI = servletRequest.getRequestURI();
        DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint);
        config.setSubprotocols(subProtocol != null ? Collections.singletonList(subProtocol) : Collections.emptyList());
        WsServerContainer container = getContainer(servletRequest);
        try {
            container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap());
        } catch (Exception ex) {
            return Mono.error(ex);
        }
        return Mono.empty();
    }));
}
Also used : 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) ServerContainer(jakarta.websocket.server.ServerContainer) Supplier(java.util.function.Supplier) TomcatWebSocketSession(org.springframework.web.reactive.socket.adapter.TomcatWebSocketSession) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Endpoint(jakarta.websocket.Endpoint) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) WsServerContainer(org.apache.tomcat.websocket.server.WsServerContainer) 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) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) StandardWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Assert(org.springframework.util.Assert) WsServerContainer(org.apache.tomcat.websocket.server.WsServerContainer) StandardWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) TomcatWebSocketSession(org.springframework.web.reactive.socket.adapter.TomcatWebSocketSession) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Endpoint(jakarta.websocket.Endpoint) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Aggregations

HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 Endpoint (jakarta.websocket.Endpoint)1 ServerContainer (jakarta.websocket.server.ServerContainer)1 Collections (java.util.Collections)1 Supplier (java.util.function.Supplier)1 WsServerContainer (org.apache.tomcat.websocket.server.WsServerContainer)1 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 ServerHttpRequestDecorator (org.springframework.http.server.reactive.ServerHttpRequestDecorator)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1 ServerHttpResponseDecorator (org.springframework.http.server.reactive.ServerHttpResponseDecorator)1 Nullable (org.springframework.lang.Nullable)1 Assert (org.springframework.util.Assert)1 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)1 WebSocketHandler (org.springframework.web.reactive.socket.WebSocketHandler)1 ContextWebSocketHandler (org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler)1 StandardWebSocketHandlerAdapter (org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter)1 TomcatWebSocketSession (org.springframework.web.reactive.socket.adapter.TomcatWebSocketSession)1 RequestUpgradeStrategy (org.springframework.web.reactive.socket.server.RequestUpgradeStrategy)1