Example usage for org.springframework.web.socket WebSocketHttpHeaders getSecWebSocketProtocol

List of usage examples for org.springframework.web.socket WebSocketHttpHeaders getSecWebSocketProtocol

Introduction

In this page you can find the example usage for org.springframework.web.socket WebSocketHttpHeaders getSecWebSocketProtocol.

Prototype

public List<String> getSecWebSocketProtocol() 

Source Link

Document

Returns the value of the Sec-WebSocket-Key header.

Usage

From source file:org.springframework.web.socket.client.AbstractWebSocketClient.java

@Override
public final ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler,
        @Nullable WebSocketHttpHeaders headers, URI uri) {

    Assert.notNull(webSocketHandler, "WebSocketHandler must not be null");
    assertUri(uri);/*from   ww w  .  j a v a  2  s.c o m*/

    if (logger.isDebugEnabled()) {
        logger.debug("Connecting to " + uri);
    }

    HttpHeaders headersToUse = new HttpHeaders();
    if (headers != null) {
        for (String header : headers.keySet()) {
            List<String> values = headers.get(header);
            if (values != null && !specialHeaders.contains(header.toLowerCase())) {
                headersToUse.put(header, values);
            }
        }
    }

    List<String> subProtocols = (headers != null ? headers.getSecWebSocketProtocol() : Collections.emptyList());
    List<WebSocketExtension> extensions = (headers != null ? headers.getSecWebSocketExtensions()
            : Collections.emptyList());

    return doHandshakeInternal(webSocketHandler, headersToUse, uri, subProtocols, extensions,
            Collections.emptyMap());
}

From source file:org.springframework.web.socket.server.support.AbstractHandshakeHandler.java

@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);
    }//from   w w w . j  a v  a2 s .  c  o  m
    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;
}

From source file:org.springframework.web.socket.server.support.DefaultHandshakeHandler.java

@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);
    }//from  w ww . j  ava2s . c  o  m
    try {
        if (!HttpMethod.GET.equals(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, uri=" + 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;
}