Search in sources :

Example 1 with HandshakeInterceptorChain

use of org.springframework.web.socket.server.support.HandshakeInterceptorChain 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 (Throwable 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)

Example 2 with HandshakeInterceptorChain

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

the class TransportHandlingSockJsService method handleTransportRequest.

@Override
protected void handleTransportRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler, String sessionId, String transport) throws SockJsException {
    TransportType transportType = TransportType.fromValue(transport);
    if (transportType == null) {
        if (logger.isWarnEnabled()) {
            logger.warn("Unknown transport type for " + request.getURI());
        }
        response.setStatusCode(HttpStatus.NOT_FOUND);
        return;
    }
    TransportHandler transportHandler = this.handlers.get(transportType);
    if (transportHandler == null) {
        if (logger.isWarnEnabled()) {
            logger.warn("No TransportHandler for " + request.getURI());
        }
        response.setStatusCode(HttpStatus.NOT_FOUND);
        return;
    }
    SockJsException failure = null;
    HandshakeInterceptorChain chain = new HandshakeInterceptorChain(this.interceptors, handler);
    try {
        HttpMethod supportedMethod = transportType.getHttpMethod();
        if (supportedMethod != request.getMethod()) {
            if (HttpMethod.OPTIONS == request.getMethod() && transportType.supportsCors()) {
                if (checkOrigin(request, response, HttpMethod.OPTIONS, supportedMethod)) {
                    response.setStatusCode(HttpStatus.NO_CONTENT);
                    addCacheHeaders(response);
                }
            } else if (transportType.supportsCors()) {
                sendMethodNotAllowed(response, supportedMethod, HttpMethod.OPTIONS);
            } else {
                sendMethodNotAllowed(response, supportedMethod);
            }
            return;
        }
        SockJsSession session = this.sessions.get(sessionId);
        if (session == null) {
            if (transportHandler instanceof SockJsSessionFactory) {
                Map<String, Object> attributes = new HashMap<>();
                if (!chain.applyBeforeHandshake(request, response, attributes)) {
                    return;
                }
                SockJsSessionFactory sessionFactory = (SockJsSessionFactory) transportHandler;
                session = createSockJsSession(sessionId, sessionFactory, handler, attributes);
            } else {
                response.setStatusCode(HttpStatus.NOT_FOUND);
                if (logger.isDebugEnabled()) {
                    logger.debug("Session not found, sessionId=" + sessionId + ". The session may have been closed " + "(e.g. missed heart-beat) while a message was coming in.");
                }
                return;
            }
        } else {
            if (session.getPrincipal() != null) {
                if (!session.getPrincipal().equals(request.getPrincipal())) {
                    logger.debug("The user for the session does not match the user for the request.");
                    response.setStatusCode(HttpStatus.NOT_FOUND);
                    return;
                }
            }
            if (!transportHandler.checkSessionType(session)) {
                logger.debug("Session type does not match the transport type for the request.");
                response.setStatusCode(HttpStatus.NOT_FOUND);
                return;
            }
        }
        if (transportType.sendsNoCacheInstruction()) {
            addNoCacheHeaders(response);
        }
        if (transportType.supportsCors()) {
            if (!checkOrigin(request, response)) {
                return;
            }
        }
        transportHandler.handleRequest(request, response, handler, session);
        chain.applyAfterHandshake(request, response, null);
    } catch (SockJsException ex) {
        failure = ex;
    } catch (Throwable ex) {
        failure = new SockJsException("Uncaught failure for request " + request.getURI(), sessionId, ex);
    } finally {
        if (failure != null) {
            chain.applyAfterHandshake(request, response, failure);
            throw failure;
        }
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SockJsException(org.springframework.web.socket.sockjs.SockJsException) HandshakeInterceptorChain(org.springframework.web.socket.server.support.HandshakeInterceptorChain) HttpMethod(org.springframework.http.HttpMethod)

Aggregations

HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 HandshakeInterceptorChain (org.springframework.web.socket.server.support.HandshakeInterceptorChain)2 HttpMethod (org.springframework.http.HttpMethod)1 HandshakeFailureException (org.springframework.web.socket.server.HandshakeFailureException)1 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)1 SockJsException (org.springframework.web.socket.sockjs.SockJsException)1