Search in sources :

Example 1 with Handshake

use of io.undertow.websockets.core.protocol.Handshake in project spring-framework by spring-projects.

the class UndertowRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
    ServerHttpRequest request = exchange.getRequest();
    Assert.isInstanceOf(UndertowServerHttpRequest.class, request, "UndertowServerHttpRequest required");
    HttpServerExchange httpExchange = ((UndertowServerHttpRequest) request).getUndertowExchange();
    Set<String> protocols = subProtocol.map(Collections::singleton).orElse(Collections.emptySet());
    Hybi13Handshake handshake = new Hybi13Handshake(protocols, false);
    List<Handshake> handshakes = Collections.singletonList(handshake);
    URI url = request.getURI();
    HttpHeaders headers = request.getHeaders();
    Mono<Principal> principal = exchange.getPrincipal();
    HandshakeInfo info = new HandshakeInfo(url, headers, principal, subProtocol);
    DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory();
    try {
        DefaultCallback callback = new DefaultCallback(info, handler, bufferFactory);
        new WebSocketProtocolHandshakeHandler(handshakes, callback).handleRequest(httpExchange);
    } catch (Exception ex) {
        return Mono.error(ex);
    }
    return Mono.empty();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) UndertowServerHttpRequest(org.springframework.http.server.reactive.UndertowServerHttpRequest) URI(java.net.URI) HttpServerExchange(io.undertow.server.HttpServerExchange) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) UndertowServerHttpRequest(org.springframework.http.server.reactive.UndertowServerHttpRequest) Principal(java.security.Principal) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) Handshake(io.undertow.websockets.core.protocol.Handshake)

Example 2 with Handshake

use of io.undertow.websockets.core.protocol.Handshake in project undertow by undertow-io.

the class ServerWebSocketContainer method handshakes.

static WebSocketHandshakeHolder handshakes(ConfiguredServerEndpoint config, List<ExtensionHandshake> extensions) {
    List<Handshake> handshakes = new ArrayList<>();
    Handshake jsrHybi13Handshake = new JsrHybi13Handshake(config);
    Handshake jsrHybi08Handshake = new JsrHybi08Handshake(config);
    Handshake jsrHybi07Handshake = new JsrHybi07Handshake(config);
    for (ExtensionHandshake extension : extensions) {
        jsrHybi13Handshake.addExtension(extension);
        jsrHybi08Handshake.addExtension(extension);
        jsrHybi07Handshake.addExtension(extension);
    }
    handshakes.add(jsrHybi13Handshake);
    handshakes.add(jsrHybi08Handshake);
    handshakes.add(jsrHybi07Handshake);
    return new WebSocketHandshakeHolder(handshakes, config);
}
Also used : JsrHybi07Handshake(io.undertow.websockets.jsr.handshake.JsrHybi07Handshake) JsrHybi08Handshake(io.undertow.websockets.jsr.handshake.JsrHybi08Handshake) ArrayList(java.util.ArrayList) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake) JsrHybi13Handshake(io.undertow.websockets.jsr.handshake.JsrHybi13Handshake) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake) JsrHybi13Handshake(io.undertow.websockets.jsr.handshake.JsrHybi13Handshake) JsrHybi08Handshake(io.undertow.websockets.jsr.handshake.JsrHybi08Handshake) Handshake(io.undertow.websockets.core.protocol.Handshake) JsrHybi07Handshake(io.undertow.websockets.jsr.handshake.JsrHybi07Handshake)

Example 3 with Handshake

use of io.undertow.websockets.core.protocol.Handshake in project undertow by undertow-io.

the class WebSocketServlet method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    final ServletWebSocketHttpExchange facade = new ServletWebSocketHttpExchange(req, resp, peerConnections);
    Handshake handshaker = null;
    for (Handshake method : handshakes) {
        if (method.matches(facade)) {
            handshaker = method;
            break;
        }
    }
    if (handshaker == null) {
        UndertowLogger.REQUEST_LOGGER.debug("Could not find hand shaker for web socket request");
        resp.sendError(StatusCodes.BAD_REQUEST);
        return;
    }
    final Handshake selected = handshaker;
    facade.upgradeChannel(new HttpUpgradeListener() {

        @Override
        public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
            WebSocketChannel channel = selected.createChannel(facade, streamConnection, facade.getBufferPool());
            peerConnections.add(channel);
            callback.onConnect(facade, channel);
        }
    });
    handshaker.handshake(facade);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) HttpUpgradeListener(io.undertow.server.HttpUpgradeListener) StreamConnection(org.xnio.StreamConnection) Hybi07Handshake(io.undertow.websockets.core.protocol.version07.Hybi07Handshake) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) Handshake(io.undertow.websockets.core.protocol.Handshake) Hybi08Handshake(io.undertow.websockets.core.protocol.version08.Hybi08Handshake)

Example 4 with Handshake

use of io.undertow.websockets.core.protocol.Handshake in project undertow by undertow-io.

the class WebSocketProtocolHandshakeHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    if (!exchange.getRequestMethod().equals(Methods.GET)) {
        // Only GET is supported to start the handshake
        next.handleRequest(exchange);
        return;
    }
    final AsyncWebSocketHttpServerExchange facade = new AsyncWebSocketHttpServerExchange(exchange, peerConnections);
    Handshake handshaker = null;
    for (Handshake method : handshakes) {
        if (method.matches(facade)) {
            handshaker = method;
            break;
        }
    }
    if (handshaker == null) {
        next.handleRequest(exchange);
    } else {
        WebSocketLogger.REQUEST_LOGGER.debugf("Attempting websocket handshake with %s on %s", handshaker, exchange);
        final Handshake selected = handshaker;
        if (upgradeListener == null) {
            exchange.upgradeChannel(new HttpUpgradeListener() {

                @Override
                public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
                    WebSocketChannel channel = selected.createChannel(facade, streamConnection, facade.getBufferPool());
                    peerConnections.add(channel);
                    callback.onConnect(facade, channel);
                }
            });
        } else {
            exchange.upgradeChannel(upgradeListener);
        }
        handshaker.handshake(facade);
    }
}
Also used : AsyncWebSocketHttpServerExchange(io.undertow.websockets.spi.AsyncWebSocketHttpServerExchange) HttpServerExchange(io.undertow.server.HttpServerExchange) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AsyncWebSocketHttpServerExchange(io.undertow.websockets.spi.AsyncWebSocketHttpServerExchange) HttpUpgradeListener(io.undertow.server.HttpUpgradeListener) StreamConnection(org.xnio.StreamConnection) Hybi07Handshake(io.undertow.websockets.core.protocol.version07.Hybi07Handshake) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake) Handshake(io.undertow.websockets.core.protocol.Handshake) Hybi08Handshake(io.undertow.websockets.core.protocol.version08.Hybi08Handshake)

Example 5 with Handshake

use of io.undertow.websockets.core.protocol.Handshake in project undertow by undertow-io.

the class WebSocketServlet method handshakes.

protected List<Handshake> handshakes() {
    List<Handshake> handshakes = new ArrayList<>();
    handshakes.add(new Hybi13Handshake());
    handshakes.add(new Hybi08Handshake());
    handshakes.add(new Hybi07Handshake());
    return handshakes;
}
Also used : Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) ArrayList(java.util.ArrayList) Hybi07Handshake(io.undertow.websockets.core.protocol.version07.Hybi07Handshake) Hybi08Handshake(io.undertow.websockets.core.protocol.version08.Hybi08Handshake) Hybi07Handshake(io.undertow.websockets.core.protocol.version07.Hybi07Handshake) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) Handshake(io.undertow.websockets.core.protocol.Handshake) Hybi08Handshake(io.undertow.websockets.core.protocol.version08.Hybi08Handshake)

Aggregations

Handshake (io.undertow.websockets.core.protocol.Handshake)9 HttpServerExchange (io.undertow.server.HttpServerExchange)5 HttpUpgradeListener (io.undertow.server.HttpUpgradeListener)4 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)4 Hybi13Handshake (io.undertow.websockets.core.protocol.version13.Hybi13Handshake)4 ExtensionHandshake (io.undertow.websockets.extensions.ExtensionHandshake)4 JsrHybi07Handshake (io.undertow.websockets.jsr.handshake.JsrHybi07Handshake)4 JsrHybi08Handshake (io.undertow.websockets.jsr.handshake.JsrHybi08Handshake)4 JsrHybi13Handshake (io.undertow.websockets.jsr.handshake.JsrHybi13Handshake)4 StreamConnection (org.xnio.StreamConnection)4 Hybi07Handshake (io.undertow.websockets.core.protocol.version07.Hybi07Handshake)3 Hybi08Handshake (io.undertow.websockets.core.protocol.version08.Hybi08Handshake)3 ArrayList (java.util.ArrayList)3 ServletWebSocketHttpExchange (io.undertow.servlet.websockets.ServletWebSocketHttpExchange)2 InstanceFactory (io.undertow.servlet.api.InstanceFactory)1 ConstructorInstanceFactory (io.undertow.servlet.util.ConstructorInstanceFactory)1 PathTemplate (io.undertow.util.PathTemplate)1 PathTemplateMatcher (io.undertow.util.PathTemplateMatcher)1 WebSocketProtocolHandshakeHandler (io.undertow.websockets.WebSocketProtocolHandshakeHandler)1 WebSocketHandshakeHolder (io.undertow.websockets.jsr.ServerWebSocketContainer.WebSocketHandshakeHolder)1