Search in sources :

Example 1 with JettyWebSocketSession

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

the class JettyWebSocketClient method createJettyHandler.

private Object createJettyHandler(URI url, WebSocketHandler handler, MonoProcessor<Void> completion) {
    return new JettyWebSocketHandlerAdapter(handler, session -> {
        UpgradeResponse response = session.getUpgradeResponse();
        HttpHeaders responseHeaders = new HttpHeaders();
        response.getHeaders().forEach(responseHeaders::put);
        HandshakeInfo info = afterHandshake(url, responseHeaders);
        return new JettyWebSocketSession(session, info, this.bufferFactory, completion);
    });
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) UpgradeResponse(org.eclipse.jetty.websocket.api.UpgradeResponse) JettyWebSocketSession(org.springframework.web.reactive.socket.adapter.JettyWebSocketSession) JettyWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 2 with JettyWebSocketSession

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

the class JettyRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    HttpServletResponse servletResponse = getHttpServletResponse(response);
    JettyWebSocketHandlerAdapter adapter = new JettyWebSocketHandlerAdapter(handler, session -> {
        HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
        DataBufferFactory factory = response.bufferFactory();
        return new JettyWebSocketSession(session, info, factory);
    });
    startLazily(servletRequest);
    boolean isUpgrade = this.factory.isUpgradeRequest(servletRequest, servletResponse);
    Assert.isTrue(isUpgrade, "Not a WebSocket handshake");
    try {
        adapterHolder.set(new WebSocketHandlerContainer(adapter, subProtocol));
        this.factory.acceptWebSocket(servletRequest, servletResponse);
    } catch (IOException ex) {
        return Mono.error(ex);
    } finally {
        adapterHolder.remove();
    }
    return Mono.empty();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.reactive.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) JettyWebSocketSession(org.springframework.web.reactive.socket.adapter.JettyWebSocketSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.reactive.ServletServerHttpResponse) JettyWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 3 with JettyWebSocketSession

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

Aggregations

HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)3 JettyWebSocketHandlerAdapter (org.springframework.web.reactive.socket.adapter.JettyWebSocketHandlerAdapter)3 JettyWebSocketSession (org.springframework.web.reactive.socket.adapter.JettyWebSocketSession)3 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)2 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)2 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)2 ServletContext (jakarta.servlet.ServletContext)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 IOException (java.io.IOException)1 Supplier (java.util.function.Supplier)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 UpgradeResponse (org.eclipse.jetty.websocket.api.UpgradeResponse)1 JettyWebSocketCreator (org.eclipse.jetty.websocket.server.JettyWebSocketCreator)1 JettyWebSocketServerContainer (org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ServerHttpRequestDecorator (org.springframework.http.server.reactive.ServerHttpRequestDecorator)1 ServerHttpResponseDecorator (org.springframework.http.server.reactive.ServerHttpResponseDecorator)1 ServletServerHttpRequest (org.springframework.http.server.reactive.ServletServerHttpRequest)1