Search in sources :

Example 6 with SockJsException

use of org.springframework.web.socket.sockjs.SockJsException 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

SockJsException (org.springframework.web.socket.sockjs.SockJsException)6 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 HttpMethod (org.springframework.http.HttpMethod)1 InvalidMediaTypeException (org.springframework.http.InvalidMediaTypeException)1 MediaType (org.springframework.http.MediaType)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 HandshakeInterceptorChain (org.springframework.web.socket.server.support.HandshakeInterceptorChain)1 SockJsTransportFailureException (org.springframework.web.socket.sockjs.SockJsTransportFailureException)1