Example usage for org.springframework.web.socket WebSocketSession getAcceptedProtocol

List of usage examples for org.springframework.web.socket WebSocketSession getAcceptedProtocol

Introduction

In this page you can find the example usage for org.springframework.web.socket WebSocketSession getAcceptedProtocol.

Prototype

@Nullable
String getAcceptedProtocol();

Source Link

Document

Return the negotiated sub-protocol.

Usage

From source file:org.springframework.integration.websocket.support.SubProtocolHandlerRegistry.java

/**
 * Resolves the {@link SubProtocolHandler} for the given {@code session} using
 * its {@link WebSocketSession#getAcceptedProtocol() accepted sub-protocol}.
 * @param session The session to resolve the sub-protocol handler for
 * @return The sub-protocol handler/*from   ww  w.  j a va 2  s.  co  m*/
 * @throws IllegalStateException if a protocol handler cannot be resolved
 */
public SubProtocolHandler findProtocolHandler(WebSocketSession session) {
    SubProtocolHandler handler;
    String protocol = session.getAcceptedProtocol();
    if (StringUtils.hasText(protocol)) {
        handler = this.protocolHandlers.get(protocol);
        Assert.state(handler != null,
                "No handler for sub-protocol '" + protocol + "', handlers = " + this.protocolHandlers);
    } else {
        handler = this.defaultProtocolHandler;
        Assert.state(handler != null,
                "No sub-protocol was requested and a default sub-protocol handler was not configured");
    }
    return handler;
}

From source file:org.springframework.messaging.handler.websocket.SubProtocolWebSocketHandler.java

protected final SubProtocolHandler findProtocolHandler(WebSocketSession session) {
    SubProtocolHandler handler;/*  www.j  ava  2  s. c o  m*/
    String protocol = session.getAcceptedProtocol();
    if (!StringUtils.isEmpty(protocol)) {
        handler = this.protocolHandlers.get(protocol);
        Assert.state(handler != null,
                "No handler for sub-protocol '" + protocol + "', handlers=" + this.protocolHandlers);
    } else {
        if (this.defaultProtocolHandler != null) {
            handler = this.defaultProtocolHandler;
        } else {
            Set<SubProtocolHandler> handlers = new HashSet<SubProtocolHandler>(this.protocolHandlers.values());
            if (handlers.size() == 1) {
                handler = handlers.iterator().next();
            } else {
                throw new IllegalStateException(
                        "No sub-protocol was requested and a default sub-protocol handler was not configured");
            }
        }
    }
    return handler;
}

From source file:org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.java

/**
 * Find a {@link SubProtocolHandler} for the given session.
 * @param session the {@code WebSocketSession} to find a handler for
 *///from   ww  w  .  j av a  2s.c  o m
protected final SubProtocolHandler findProtocolHandler(WebSocketSession session) {
    String protocol = null;
    try {
        protocol = session.getAcceptedProtocol();
    } catch (Exception ex) {
        // Shouldn't happen
        logger.error("Failed to obtain session.getAcceptedProtocol(): "
                + "will use the default protocol handler (if configured).", ex);
    }

    SubProtocolHandler handler;
    if (!StringUtils.isEmpty(protocol)) {
        handler = this.protocolHandlerLookup.get(protocol);
        if (handler == null) {
            throw new IllegalStateException(
                    "No handler for '" + protocol + "' among " + this.protocolHandlerLookup);
        }
    } else {
        if (this.defaultProtocolHandler != null) {
            handler = this.defaultProtocolHandler;
        } else if (this.protocolHandlers.size() == 1) {
            handler = this.protocolHandlers.iterator().next();
        } else {
            throw new IllegalStateException("Multiple protocol handlers configured and "
                    + "no protocol was negotiated. Consider configuring a default SubProtocolHandler.");
        }
    }
    return handler;
}