Search in sources :

Example 11 with HandshakeInfo

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

Example 12 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, Sinks.Empty<Void> completionSink, DefaultNegotiation negotiation, WebSocketChannel channel) {
    HandshakeInfo info = createHandshakeInfo(url, negotiation);
    DataBufferFactory bufferFactory = DefaultDataBufferFactory.sharedInstance;
    UndertowWebSocketSession session = new UndertowWebSocketSession(channel, info, bufferFactory, completionSink);
    UndertowWebSocketHandlerAdapter adapter = new UndertowWebSocketHandlerAdapter(session);
    channel.getReceiveSetter().set(adapter);
    channel.resumeReceives();
    handler.handle(session).checkpoint(url + " [UndertowWebSocketClient]").subscribe(session);
}
Also used : UndertowWebSocketSession(org.springframework.web.reactive.socket.adapter.UndertowWebSocketSession) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) UndertowWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.UndertowWebSocketHandlerAdapter)

Example 13 with HandshakeInfo

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

the class UndertowWebSocketClient method createHandshakeInfo.

private HandshakeInfo createHandshakeInfo(URI url, DefaultNegotiation negotiation) {
    HttpHeaders responseHeaders = negotiation.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)

Example 14 with HandshakeInfo

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

the class JettyWebSocketClient method createHandshakeInfo.

private HandshakeInfo createHandshakeInfo(URI url, Session jettySession) {
    HttpHeaders headers = new HttpHeaders();
    jettySession.getUpgradeResponse().getHeaders().forEach(headers::put);
    String protocol = headers.getFirst("Sec-WebSocket-Protocol");
    return new HandshakeInfo(url, headers, Mono.empty(), protocol);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 15 with HandshakeInfo

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

the class HandshakeWebSocketService method createHandshakeInfo.

private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttpRequest request, @Nullable String protocol, Map<String, Object> attributes) {
    URI uri = request.getURI();
    // Copy request headers, as they might be pooled and recycled by
    // the server implementation once the handshake HTTP exchange is done.
    HttpHeaders headers = new HttpHeaders();
    headers.addAll(request.getHeaders());
    MultiValueMap<String, HttpCookie> cookies = request.getCookies();
    Mono<Principal> principal = exchange.getPrincipal();
    String logPrefix = exchange.getLogPrefix();
    InetSocketAddress remoteAddress = request.getRemoteAddress();
    return new HandshakeInfo(uri, headers, cookies, principal, protocol, remoteAddress, attributes, logPrefix);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) HttpCookie(org.springframework.http.HttpCookie) Principal(java.security.Principal) 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