Search in sources :

Example 1 with HandshakeFailureException

use of org.springframework.web.socket.server.HandshakeFailureException 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 HandshakeFailureException

use of org.springframework.web.socket.server.HandshakeFailureException in project spring-framework by spring-projects.

the class AbstractTyrusRequestUpgradeStrategy method createEndpoint.

private Object createEndpoint(ServerEndpointRegistration registration, ComponentProviderService provider, WebSocketContainer container, TyrusWebSocketEngine engine) throws DeploymentException {
    DirectFieldAccessor accessor = new DirectFieldAccessor(engine);
    Object sessionListener = accessor.getPropertyValue("sessionListener");
    Object clusterContext = accessor.getPropertyValue("clusterContext");
    try {
        if (constructorWithBooleanArgument) {
            // Tyrus 1.11+
            return constructor.newInstance(registration.getEndpoint(), registration, provider, container, "/", registration.getConfigurator(), sessionListener, clusterContext, null, Boolean.TRUE);
        } else {
            return constructor.newInstance(registration.getEndpoint(), registration, provider, container, "/", registration.getConfigurator(), sessionListener, clusterContext, null);
        }
    } catch (Exception ex) {
        throw new HandshakeFailureException("Failed to register " + registration, ex);
    }
}
Also used : DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) DeploymentException(jakarta.websocket.DeploymentException)

Example 3 with HandshakeFailureException

use of org.springframework.web.socket.server.HandshakeFailureException in project spring-framework by spring-projects.

the class UndertowRequestUpgradeStrategy method upgradeInternal.

@Override
protected void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint) throws HandshakeFailureException {
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    HttpServletResponse servletResponse = getHttpServletResponse(response);
    StringBuffer requestUrl = servletRequest.getRequestURL();
    // shouldn't matter
    String path = servletRequest.getRequestURI();
    Map<String, String> pathParams = Collections.emptyMap();
    ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
    endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
    endpointConfig.setExtensions(selectedExtensions);
    try {
        getContainer(servletRequest).doUpgrade(servletRequest, servletResponse, endpointConfig, pathParams);
    } catch (ServletException ex) {
        throw new HandshakeFailureException("Servlet request failed to upgrade to WebSocket: " + requestUrl, ex);
    } catch (IOException ex) {
        throw new HandshakeFailureException("Response update failed during upgrade to WebSocket: " + requestUrl, ex);
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletException(jakarta.servlet.ServletException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 4 with HandshakeFailureException

use of org.springframework.web.socket.server.HandshakeFailureException in project spring-framework by spring-projects.

the class TransportHandlingSockJsService method handleRawWebSocketRequest.

@Override
protected void handleRawWebSocketRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler) throws IOException {
    TransportHandler transportHandler = this.handlers.get(TransportType.WEBSOCKET);
    if (!(transportHandler instanceof HandshakeHandler)) {
        logger.error("No handler configured for raw WebSocket messages");
        response.setStatusCode(HttpStatus.NOT_FOUND);
        return;
    }
    HandshakeInterceptorChain chain = new HandshakeInterceptorChain(this.interceptors, handler);
    HandshakeFailureException failure = null;
    try {
        Map<String, Object> attributes = new HashMap<>();
        if (!chain.applyBeforeHandshake(request, response, attributes)) {
            return;
        }
        ((HandshakeHandler) transportHandler).doHandshake(request, response, handler, attributes);
        chain.applyAfterHandshake(request, response, null);
    } catch (HandshakeFailureException ex) {
        failure = ex;
    } catch (Exception ex) {
        failure = new HandshakeFailureException("Uncaught failure for request " + request.getURI(), ex);
    } finally {
        if (failure != null) {
            chain.applyAfterHandshake(request, response, failure);
            throw failure;
        }
    }
}
Also used : HandshakeHandler(org.springframework.web.socket.server.HandshakeHandler) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) HandshakeInterceptorChain(org.springframework.web.socket.server.support.HandshakeInterceptorChain) SockJsException(org.springframework.web.socket.sockjs.SockJsException) IOException(java.io.IOException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException)

Example 5 with HandshakeFailureException

use of org.springframework.web.socket.server.HandshakeFailureException in project spring-framework by spring-projects.

the class AbstractTyrusRequestUpgradeStrategy method upgradeInternal.

@Override
public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<Extension> extensions, Endpoint endpoint) throws HandshakeFailureException {
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    HttpServletResponse servletResponse = getHttpServletResponse(response);
    TyrusServerContainer serverContainer = (TyrusServerContainer) getContainer(servletRequest);
    TyrusWebSocketEngine engine = (TyrusWebSocketEngine) serverContainer.getWebSocketEngine();
    Object tyrusEndpoint = null;
    boolean success;
    try {
        // Shouldn't matter for processing but must be unique
        String path = "/" + random.nextLong();
        tyrusEndpoint = createTyrusEndpoint(endpoint, path, selectedProtocol, extensions, serverContainer, engine);
        register(engine, tyrusEndpoint);
        HttpHeaders headers = request.getHeaders();
        RequestContext requestContext = createRequestContext(servletRequest, path, headers);
        TyrusUpgradeResponse upgradeResponse = new TyrusUpgradeResponse();
        UpgradeInfo upgradeInfo = engine.upgrade(requestContext, upgradeResponse);
        success = SUCCESS.equals(upgradeInfo.getStatus());
        if (success) {
            if (logger.isTraceEnabled()) {
                logger.trace("Successful request upgrade: " + upgradeResponse.getHeaders());
            }
            handleSuccess(servletRequest, servletResponse, upgradeInfo, upgradeResponse);
        }
    } catch (Exception ex) {
        unregisterTyrusEndpoint(engine, tyrusEndpoint);
        throw new HandshakeFailureException("Error during handshake: " + request.getURI(), ex);
    }
    unregisterTyrusEndpoint(engine, tyrusEndpoint);
    if (!success) {
        throw new HandshakeFailureException("Unexpected handshake failure: " + request.getURI());
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) DeploymentException(jakarta.websocket.DeploymentException) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) TyrusServerContainer(org.glassfish.tyrus.server.TyrusServerContainer) TyrusUpgradeResponse(org.glassfish.tyrus.core.TyrusUpgradeResponse) TyrusWebSocketEngine(org.glassfish.tyrus.core.TyrusWebSocketEngine) UpgradeInfo(org.glassfish.tyrus.spi.WebSocketEngine.UpgradeInfo) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) RequestContext(org.glassfish.tyrus.core.RequestContext)

Aggregations

HandshakeFailureException (org.springframework.web.socket.server.HandshakeFailureException)10 IOException (java.io.IOException)7 ServletException (jakarta.servlet.ServletException)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)5 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)5 WebSocketExtension (org.springframework.web.socket.WebSocketExtension)3 DeploymentException (jakarta.websocket.DeploymentException)2 Principal (java.security.Principal)2 HashMap (java.util.HashMap)2 HttpHeaders (org.springframework.http.HttpHeaders)2 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)2 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)2 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)2 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)2 ServletContext (jakarta.servlet.ServletContext)1 Extension (jakarta.websocket.Extension)1 ServerContainer (jakarta.websocket.server.ServerContainer)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1