Search in sources :

Example 1 with ExtensionHandshake

use of io.undertow.websockets.extensions.ExtensionHandshake 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 2 with ExtensionHandshake

use of io.undertow.websockets.extensions.ExtensionHandshake in project undertow by undertow-io.

the class Bootstrap method handleDeployment.

@Override
public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {
    WebSocketDeploymentInfo info = (WebSocketDeploymentInfo) deploymentInfo.getServletContextAttributes().get(WebSocketDeploymentInfo.ATTRIBUTE_NAME);
    if (info == null) {
        return;
    }
    Supplier<XnioWorker> worker = info.getWorker();
    ByteBufferPool buffers = info.getBuffers();
    if (buffers == null) {
        ServerWebSocketContainer defaultContainer = UndertowContainerProvider.getDefaultContainer();
        if (defaultContainer == null) {
            throw JsrWebSocketLogger.ROOT_LOGGER.bufferPoolWasNullAndNoDefault();
        }
        JsrWebSocketLogger.ROOT_LOGGER.bufferPoolWasNull();
        buffers = defaultContainer.getBufferPool();
    }
    final List<ThreadSetupHandler> setup = new ArrayList<>();
    setup.add(new ContextClassLoaderSetupAction(deploymentInfo.getClassLoader()));
    setup.addAll(deploymentInfo.getThreadSetupActions());
    InetSocketAddress bind = null;
    if (info.getClientBindAddress() != null) {
        bind = new InetSocketAddress(info.getClientBindAddress(), 0);
    }
    List<Extension> extensions = new ArrayList<>();
    for (ExtensionHandshake e : info.getExtensions()) {
        extensions.add(new ExtensionImpl(e.getName(), Collections.emptyList()));
    }
    ServerWebSocketContainer container = new ServerWebSocketContainer(deploymentInfo.getClassIntrospecter(), servletContext.getClassLoader(), worker, buffers, setup, info.isDispatchToWorkerThread(), bind, info.getReconnectHandler(), extensions);
    try {
        for (Class<?> annotation : info.getAnnotatedEndpoints()) {
            container.addEndpoint(annotation);
        }
        for (ServerEndpointConfig programatic : info.getProgramaticEndpoints()) {
            container.addEndpoint(programatic);
        }
    } catch (DeploymentException e) {
        throw new RuntimeException(e);
    }
    servletContext.setAttribute(ServerContainer.class.getName(), container);
    info.containerReady(container);
    SecurityActions.addContainer(deploymentInfo.getClassLoader(), container);
    deploymentInfo.addListener(Servlets.listener(WebSocketListener.class));
    deploymentInfo.addDeploymentCompleteListener(new ServletContextListener() {

        @Override
        public void contextInitialized(ServletContextEvent sce) {
            container.validateDeployment();
        }

        @Override
        public void contextDestroyed(ServletContextEvent sce) {
        }
    });
}
Also used : ByteBufferPool(io.undertow.connector.ByteBufferPool) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) ServletContextListener(javax.servlet.ServletContextListener) XnioWorker(org.xnio.XnioWorker) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) ServletExtension(io.undertow.servlet.ServletExtension) Extension(javax.websocket.Extension) ThreadSetupHandler(io.undertow.servlet.api.ThreadSetupHandler) ContextClassLoaderSetupAction(io.undertow.servlet.core.ContextClassLoaderSetupAction) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake) DeploymentException(javax.websocket.DeploymentException) ServletContextEvent(javax.servlet.ServletContextEvent) ServerContainer(javax.websocket.server.ServerContainer)

Example 3 with ExtensionHandshake

use of io.undertow.websockets.extensions.ExtensionHandshake in project undertow by undertow-io.

the class JsrHybi13Handshake method selectedExtension.

@Override
protected List<WebSocketExtension> selectedExtension(List<WebSocketExtension> extensionList) {
    List<Extension> ext = new ArrayList<>();
    for (WebSocketExtension i : extensionList) {
        ext.add(ExtensionImpl.create(i));
    }
    List<Extension> selected = HandshakeUtil.selectExtensions(config, ext);
    if (selected == null) {
        return Collections.emptyList();
    }
    Map<String, ExtensionHandshake> extensionMap = new HashMap<>();
    for (ExtensionHandshake availible : availableExtensions) {
        extensionMap.put(availible.getName(), availible);
    }
    List<WebSocketExtension> ret = new ArrayList<>();
    List<ExtensionHandshake> accepted = new ArrayList<>();
    for (Extension i : selected) {
        ExtensionHandshake handshake = extensionMap.get(i.getName());
        if (handshake == null) {
            // should not happen
            continue;
        }
        List<WebSocketExtension.Parameter> parameters = new ArrayList<>();
        for (Extension.Parameter p : i.getParameters()) {
            parameters.add(new WebSocketExtension.Parameter(p.getName(), p.getValue()));
        }
        if (!handshake.isIncompatible(accepted)) {
            WebSocketExtension accept = handshake.accept(new WebSocketExtension(i.getName(), parameters));
            if (accept != null) {
                ret.add(accept);
                accepted.add(handshake);
            }
        }
    }
    return ret;
}
Also used : WebSocketExtension(io.undertow.websockets.WebSocketExtension) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WebSocketExtension(io.undertow.websockets.WebSocketExtension) Extension(javax.websocket.Extension) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake)

Example 4 with ExtensionHandshake

use of io.undertow.websockets.extensions.ExtensionHandshake in project undertow by undertow-io.

the class Handshake method selectedExtension.

protected List<WebSocketExtension> selectedExtension(List<WebSocketExtension> extensionList) {
    List<WebSocketExtension> selected = new ArrayList<>();
    List<ExtensionHandshake> configured = new ArrayList<>();
    for (WebSocketExtension ext : extensionList) {
        for (ExtensionHandshake extHandshake : availableExtensions) {
            WebSocketExtension negotiated = extHandshake.accept(ext);
            if (negotiated != null && !extHandshake.isIncompatible(configured)) {
                selected.add(negotiated);
                configured.add(extHandshake);
            }
        }
    }
    return selected;
}
Also used : WebSocketExtension(io.undertow.websockets.WebSocketExtension) ArrayList(java.util.ArrayList) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake)

Example 5 with ExtensionHandshake

use of io.undertow.websockets.extensions.ExtensionHandshake in project undertow by undertow-io.

the class Handshake method initExtensions.

/**
 * Create the {@code ExtensionFunction} list associated with the negotiated extensions defined in the exchange's response.
 *
 * @param exchange the exchange used to retrieve negotiated extensions
 * @return         a list of {@code ExtensionFunction} with the implementation of the extensions
 */
protected final List<ExtensionFunction> initExtensions(final WebSocketHttpExchange exchange) {
    String extHeader = exchange.getResponseHeaders().get(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING) != null ? exchange.getResponseHeaders().get(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING).get(0) : null;
    List<ExtensionFunction> negotiated = new ArrayList<>();
    if (extHeader != null) {
        List<WebSocketExtension> extensions = WebSocketExtension.parse(extHeader);
        if (extensions != null && !extensions.isEmpty()) {
            for (WebSocketExtension ext : extensions) {
                for (ExtensionHandshake extHandshake : availableExtensions) {
                    if (extHandshake.getName().equals(ext.getName())) {
                        negotiated.add(extHandshake.create());
                    }
                }
            }
        }
    }
    return negotiated;
}
Also used : ExtensionFunction(io.undertow.websockets.extensions.ExtensionFunction) WebSocketExtension(io.undertow.websockets.WebSocketExtension) ArrayList(java.util.ArrayList) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake)

Aggregations

ExtensionHandshake (io.undertow.websockets.extensions.ExtensionHandshake)8 WebSocketExtension (io.undertow.websockets.WebSocketExtension)6 ArrayList (java.util.ArrayList)6 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)3 WebSocketClientNegotiation (io.undertow.websockets.client.WebSocketClientNegotiation)2 AbstractReceiveListener (io.undertow.websockets.core.AbstractReceiveListener)2 BufferedBinaryMessage (io.undertow.websockets.core.BufferedBinaryMessage)2 BufferedTextMessage (io.undertow.websockets.core.BufferedTextMessage)2 ExtensionFunction (io.undertow.websockets.extensions.ExtensionFunction)2 PerMessageDeflateHandshake (io.undertow.websockets.extensions.PerMessageDeflateHandshake)2 BinaryEndpointTest (io.undertow.websockets.jsr.test.BinaryEndpointTest)2 URI (java.net.URI)2 HashSet (java.util.HashSet)2 Extension (javax.websocket.Extension)2 Test (org.junit.Test)2 ByteBufferPool (io.undertow.connector.ByteBufferPool)1 ServletExtension (io.undertow.servlet.ServletExtension)1 ThreadSetupHandler (io.undertow.servlet.api.ThreadSetupHandler)1 ContextClassLoaderSetupAction (io.undertow.servlet.core.ContextClassLoaderSetupAction)1 StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)1