Example usage for org.springframework.http HttpMethod OPTIONS

List of usage examples for org.springframework.http HttpMethod OPTIONS

Introduction

In this page you can find the example usage for org.springframework.http HttpMethod OPTIONS.

Prototype

HttpMethod OPTIONS

To view the source code for org.springframework.http HttpMethod OPTIONS.

Click Source Link

Usage

From source file:org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService.java

@Override
protected void handleTransportRequest(ServerHttpRequest request, ServerHttpResponse response,
        WebSocketHandler handler, String sessionId, String transport) throws SockJsException {

    TransportType transportType = TransportType.fromValue(transport);
    if (transportType == null) {
        logger.error("Unknown transport type for " + request.getURI());
        response.setStatusCode(HttpStatus.NOT_FOUND);
        return;/*from   w ww . j a  v  a  2  s.  c o  m*/
    }

    TransportHandler transportHandler = this.handlers.get(transportType);
    if (transportHandler == null) {
        logger.error("No TransportHandler for " + request.getURI());
        response.setStatusCode(HttpStatus.NOT_FOUND);
        return;
    }

    HttpMethod supportedMethod = transportType.getHttpMethod();
    if (!supportedMethod.equals(request.getMethod())) {
        if (HttpMethod.OPTIONS.equals(request.getMethod()) && transportType.supportsCors()) {
            response.setStatusCode(HttpStatus.NO_CONTENT);
            addCorsHeaders(request, response, HttpMethod.OPTIONS, supportedMethod);
            addCacheHeaders(response);
        } else if (transportType.supportsCors()) {
            sendMethodNotAllowed(response, supportedMethod, HttpMethod.OPTIONS);
        } else {
            sendMethodNotAllowed(response, supportedMethod);
        }
        return;
    }

    HandshakeInterceptorChain chain = new HandshakeInterceptorChain(this.interceptors, handler);
    SockJsException failure = null;

    try {
        SockJsSession session = this.sessions.get(sessionId);
        if (session == null) {
            if (transportHandler instanceof SockJsSessionFactory) {
                Map<String, Object> attributes = new HashMap<String, Object>();
                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;
            }
        }

        if (transportType.sendsNoCacheInstruction()) {
            addNoCacheHeaders(response);
        }

        if (transportType.supportsCors()) {
            addCorsHeaders(request, response);
        }

        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;
        }
    }
}