Search in sources :

Example 6 with HandshakeFailureException

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

the class TomcatRequestUpgradeStrategy method upgradeInternal.

// for old doUpgrade variant in Tomcat 9.0.55
@SuppressWarnings("deprecation")
@Override
public 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.<String, String>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 7 with HandshakeFailureException

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

the class WebSocketHttpRequestHandler method handleRequest.

@Override
public void handleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
    ServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
    ServerHttpResponse response = new ServletServerHttpResponse(servletResponse);
    HandshakeInterceptorChain chain = new HandshakeInterceptorChain(this.interceptors, this.wsHandler);
    HandshakeFailureException failure = null;
    try {
        if (logger.isDebugEnabled()) {
            logger.debug(servletRequest.getMethod() + " " + servletRequest.getRequestURI());
        }
        Map<String, Object> attributes = new HashMap<>();
        if (!chain.applyBeforeHandshake(request, response, attributes)) {
            return;
        }
        this.handshakeHandler.doHandshake(request, response, this.wsHandler, 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);
            response.close();
            throw failure;
        }
        response.close();
    }
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HashMap(java.util.HashMap) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException)

Example 8 with HandshakeFailureException

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

the class WebSphereRequestUpgradeStrategy method upgradeInternal.

@Override
public void upgradeInternal(ServerHttpRequest httpRequest, ServerHttpResponse httpResponse, @Nullable String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint) throws HandshakeFailureException {
    HttpServletRequest request = getHttpServletRequest(httpRequest);
    HttpServletResponse response = getHttpServletResponse(httpResponse);
    StringBuffer requestUrl = request.getRequestURL();
    // shouldn't matter
    String path = request.getRequestURI();
    Map<String, String> pathParams = Collections.<String, String>emptyMap();
    ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
    endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
    endpointConfig.setExtensions(selectedExtensions);
    try {
        ServerContainer container = getContainer(request);
        upgradeMethod.invoke(container, request, response, endpointConfig, pathParams);
    } catch (Exception ex) {
        throw new HandshakeFailureException("Servlet request failed to upgrade to WebSocket for " + requestUrl, ex);
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) ServerContainer(jakarta.websocket.server.ServerContainer)

Example 9 with HandshakeFailureException

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

the class AbstractHandshakeHandler method doHandshake.

@Override
public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureException {
    WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHeaders());
    if (logger.isTraceEnabled()) {
        logger.trace("Processing request " + request.getURI() + " with headers=" + headers);
    }
    try {
        if (HttpMethod.GET != request.getMethod()) {
            response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED);
            response.getHeaders().setAllow(Collections.singleton(HttpMethod.GET));
            if (logger.isErrorEnabled()) {
                logger.error("Handshake failed due to unexpected HTTP method: " + request.getMethod());
            }
            return false;
        }
        if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
            handleInvalidUpgradeHeader(request, response);
            return false;
        }
        if (!headers.getConnection().contains("Upgrade") && !headers.getConnection().contains("upgrade")) {
            handleInvalidConnectHeader(request, response);
            return false;
        }
        if (!isWebSocketVersionSupported(headers)) {
            handleWebSocketVersionNotSupported(request, response);
            return false;
        }
        if (!isValidOrigin(request)) {
            response.setStatusCode(HttpStatus.FORBIDDEN);
            return false;
        }
        String wsKey = headers.getSecWebSocketKey();
        if (wsKey == null) {
            if (logger.isErrorEnabled()) {
                logger.error("Missing \"Sec-WebSocket-Key\" header");
            }
            response.setStatusCode(HttpStatus.BAD_REQUEST);
            return false;
        }
    } catch (IOException ex) {
        throw new HandshakeFailureException("Response update failed during upgrade to WebSocket: " + request.getURI(), ex);
    }
    String subProtocol = selectProtocol(headers.getSecWebSocketProtocol(), wsHandler);
    List<WebSocketExtension> requested = headers.getSecWebSocketExtensions();
    List<WebSocketExtension> supported = this.requestUpgradeStrategy.getSupportedExtensions(request);
    List<WebSocketExtension> extensions = filterRequestedExtensions(request, requested, supported);
    Principal user = determineUser(request, wsHandler, attributes);
    if (logger.isTraceEnabled()) {
        logger.trace("Upgrading to WebSocket, subProtocol=" + subProtocol + ", extensions=" + extensions);
    }
    this.requestUpgradeStrategy.upgrade(request, response, subProtocol, extensions, user, wsHandler, attributes);
    return true;
}
Also used : WebSocketExtension(org.springframework.web.socket.WebSocketExtension) WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) IOException(java.io.IOException) Principal(java.security.Principal)

Example 10 with HandshakeFailureException

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

the class AbstractStandardUpgradeStrategy method upgrade.

@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<WebSocketExtension> selectedExtensions, @Nullable Principal user, WebSocketHandler wsHandler, Map<String, Object> attrs) throws HandshakeFailureException {
    HttpHeaders headers = request.getHeaders();
    InetSocketAddress localAddr = null;
    try {
        localAddr = request.getLocalAddress();
    } catch (Exception ex) {
    // Ignore
    }
    InetSocketAddress remoteAddr = null;
    try {
        remoteAddr = request.getRemoteAddress();
    } catch (Exception ex) {
    // Ignore
    }
    StandardWebSocketSession session = new StandardWebSocketSession(headers, attrs, localAddr, remoteAddr, user);
    StandardWebSocketHandlerAdapter endpoint = new StandardWebSocketHandlerAdapter(wsHandler, session);
    List<Extension> extensions = new ArrayList<>();
    for (WebSocketExtension extension : selectedExtensions) {
        extensions.add(new WebSocketToStandardExtensionAdapter(extension));
    }
    upgradeInternal(request, response, selectedProtocol, extensions, endpoint);
}
Also used : Extension(jakarta.websocket.Extension) WebSocketExtension(org.springframework.web.socket.WebSocketExtension) HttpHeaders(org.springframework.http.HttpHeaders) WebSocketExtension(org.springframework.web.socket.WebSocketExtension) StandardWebSocketHandlerAdapter(org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter) StandardWebSocketSession(org.springframework.web.socket.adapter.standard.StandardWebSocketSession) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) WebSocketToStandardExtensionAdapter(org.springframework.web.socket.adapter.standard.WebSocketToStandardExtensionAdapter)

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