Search in sources :

Example 1 with JettyWebSocketHandlerAdapter

use of org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter in project spring-framework by spring-projects.

the class JettyRequestUpgradeStrategy method upgrade.

@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<WebSocketExtension> selectedExtensions, @Nullable Principal user, WebSocketHandler handler, Map<String, Object> attributes) throws HandshakeFailureException {
    Assert.isInstanceOf(ServletServerHttpRequest.class, request, "ServletServerHttpRequest required");
    HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
    ServletContext servletContext = servletRequest.getServletContext();
    Assert.isInstanceOf(ServletServerHttpResponse.class, response, "ServletServerHttpResponse required");
    HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse();
    JettyWebSocketSession session = new JettyWebSocketSession(attributes, user);
    JettyWebSocketHandlerAdapter handlerAdapter = new JettyWebSocketHandlerAdapter(handler, session);
    JettyWebSocketCreator webSocketCreator = (upgradeRequest, upgradeResponse) -> {
        if (selectedProtocol != null) {
            upgradeResponse.setAcceptedSubProtocol(selectedProtocol);
        }
        return handlerAdapter;
    };
    JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
    try {
        container.upgrade(webSocketCreator, servletRequest, servletResponse);
    } catch (UndeclaredThrowableException ex) {
        throw new HandshakeFailureException("Failed to upgrade", ex.getUndeclaredThrowable());
    } catch (Exception ex) {
        throw new HandshakeFailureException("Failed to upgrade", ex);
    }
}
Also used : WebSocketHandler(org.springframework.web.socket.WebSocketHandler) RequestUpgradeStrategy(org.springframework.web.socket.server.RequestUpgradeStrategy) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) List(java.util.List) Principal(java.security.Principal) JettyWebSocketCreator(org.eclipse.jetty.websocket.server.JettyWebSocketCreator) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) JettyWebSocketHandlerAdapter(org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter) JettyWebSocketServerContainer(org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) Map(java.util.Map) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) Nullable(org.springframework.lang.Nullable) ServletContext(jakarta.servlet.ServletContext) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) WebSocketExtension(org.springframework.web.socket.WebSocketExtension) Collections(java.util.Collections) Assert(org.springframework.util.Assert) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) JettyWebSocketCreator(org.eclipse.jetty.websocket.server.JettyWebSocketCreator) JettyWebSocketHandlerAdapter(org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) JettyWebSocketServerContainer(org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) ServletContext(jakarta.servlet.ServletContext)

Example 2 with JettyWebSocketHandlerAdapter

use of org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter in project spring-framework by spring-projects.

the class JettyWebSocketClient method doHandshakeInternal.

@Override
public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) {
    final ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setSubProtocols(protocols);
    for (WebSocketExtension extension : extensions) {
        request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(extension));
    }
    request.setHeaders(headers);
    Principal user = getUser();
    JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user);
    Callable<WebSocketSession> connectTask = () -> {
        JettyWebSocketHandlerAdapter adapter = new JettyWebSocketHandlerAdapter(wsHandler, wsSession);
        Future<Session> future = this.client.connect(adapter, uri, request);
        future.get(this.client.getConnectTimeout() + 2000, TimeUnit.MILLISECONDS);
        return wsSession;
    };
    if (this.taskExecutor != null) {
        return this.taskExecutor.submitListenable(connectTask);
    } else {
        ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
        task.run();
        return task;
    }
}
Also used : WebSocketExtension(org.springframework.web.socket.WebSocketExtension) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) WebSocketToJettyExtensionConfigAdapter(org.springframework.web.socket.adapter.jetty.WebSocketToJettyExtensionConfigAdapter) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) Future(java.util.concurrent.Future) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) Principal(java.security.Principal) JettyWebSocketHandlerAdapter(org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter) WebSocketSession(org.springframework.web.socket.WebSocketSession) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession)

Aggregations

Principal (java.security.Principal)2 WebSocketExtension (org.springframework.web.socket.WebSocketExtension)2 JettyWebSocketHandlerAdapter (org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter)2 JettyWebSocketSession (org.springframework.web.socket.adapter.jetty.JettyWebSocketSession)2 ServletContext (jakarta.servlet.ServletContext)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Future (java.util.concurrent.Future)1 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)1 JettyWebSocketCreator (org.eclipse.jetty.websocket.server.JettyWebSocketCreator)1 JettyWebSocketServerContainer (org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer)1 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)1 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)1 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)1 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)1 Nullable (org.springframework.lang.Nullable)1