Search in sources :

Example 6 with Extension

use of javax.websocket.Extension in project tomcat by apache.

the class TesterWsClientAutobahn method executeTestCase.

private static void executeTestCase(WebSocketContainer wsc, int testCase) throws Exception {
    URI uri = new URI("ws://" + HOST + ":" + PORT + "/runCase?case=" + testCase + "&agent=" + USER_AGENT);
    TestCaseClient testCaseClient = new TestCaseClient();
    Extension permessageDeflate = new WsExtension("permessage-deflate");
    // Advertise support for client_max_window_bits
    // Client only supports some values so there will be some failures here
    // Note Autobahn returns a 400 response if you provide a value for
    // client_max_window_bits
    permessageDeflate.getParameters().add(new WsExtensionParameter("client_max_window_bits", null));
    List<Extension> extensions = new ArrayList<>(1);
    extensions.add(permessageDeflate);
    Endpoint ep = new PojoEndpointClient(testCaseClient, null);
    ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
    ClientEndpointConfig config = builder.extensions(extensions).build();
    wsc.connectToServer(ep, config, uri);
    testCaseClient.waitForClose();
}
Also used : Extension(javax.websocket.Extension) Endpoint(javax.websocket.Endpoint) ClientEndpoint(javax.websocket.ClientEndpoint) PojoEndpointClient(org.apache.tomcat.websocket.pojo.PojoEndpointClient) ArrayList(java.util.ArrayList) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) URI(java.net.URI)

Example 7 with Extension

use of javax.websocket.Extension in project tomcat by apache.

the class TestWsWebSocketContainer method doTestPerMessageDeflateClient.

private void doTestPerMessageDeflateClient(String msg, int count) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    Extension perMessageDeflate = new WsExtension(PerMessageDeflate.NAME);
    List<Extension> extensions = new ArrayList<>(1);
    extensions.add(perMessageDeflate);
    ClientEndpointConfig clientConfig = ClientEndpointConfig.Builder.create().extensions(extensions).build();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientConfig, new URI("ws://" + getHostName() + ":" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
    CountDownLatch latch = new CountDownLatch(count);
    BasicText handler = new BasicText(latch, msg);
    wsSession.addMessageHandler(handler);
    for (int i = 0; i < count; i++) {
        wsSession.getBasicRemote().sendText(msg);
    }
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    ((WsWebSocketContainer) wsContainer).destroy();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) Extension(javax.websocket.Extension) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session)

Example 8 with Extension

use of javax.websocket.Extension in project jetty.project by eclipse.

the class ClientContainer method getInstalledExtensions.

@Override
public Set<Extension> getInstalledExtensions() {
    Set<Extension> ret = new HashSet<>();
    ExtensionFactory extensions = client.getExtensionFactory();
    for (String name : extensions.getExtensionNames()) {
        ret.add(new JsrExtension(name));
    }
    return ret;
}
Also used : Extension(javax.websocket.Extension) ExtensionFactory(org.eclipse.jetty.websocket.api.extensions.ExtensionFactory) HashSet(java.util.HashSet)

Example 9 with Extension

use of javax.websocket.Extension 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;
    }
    XnioWorker worker = info.getWorker();
    if (worker == null) {
        ServerWebSocketContainer defaultContainer = UndertowContainerProvider.getDefaultContainer();
        if (defaultContainer == null) {
            throw JsrWebSocketLogger.ROOT_LOGGER.xnioWorkerWasNullAndNoDefault();
        }
        JsrWebSocketLogger.ROOT_LOGGER.xnioWorkerWasNull();
        worker = defaultContainer.getXnioWorker();
    }
    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));
}
Also used : ByteBufferPool(io.undertow.connector.ByteBufferPool) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) 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) ServerContainer(javax.websocket.server.ServerContainer)

Example 10 with Extension

use of javax.websocket.Extension in project undertow by undertow-io.

the class ServerWebSocketContainer method connectToServer.

public Session connectToServer(final Endpoint endpointInstance, final ClientEndpointConfig config, WebSocketClient.ConnectionBuilder connectionBuilder) throws DeploymentException, IOException {
    if (closed) {
        throw new ClosedChannelException();
    }
    ClientEndpointConfig cec = config != null ? config : ClientEndpointConfig.Builder.create().build();
    WebSocketClientNegotiation clientNegotiation = connectionBuilder.getClientNegotiation();
    IoFuture<WebSocketChannel> session = connectionBuilder.connect();
    Number timeout = (Number) cec.getUserProperties().get(TIMEOUT);
    if (session.await(timeout == null ? DEFAULT_WEB_SOCKET_TIMEOUT_SECONDS : timeout.intValue(), TimeUnit.SECONDS) == IoFuture.Status.WAITING) {
        //add a notifier to close the channel if the connection actually completes
        session.cancel();
        session.addNotifier(new IoFuture.HandlingNotifier<WebSocketChannel, Object>() {

            @Override
            public void handleDone(WebSocketChannel data, Object attachment) {
                IoUtils.safeClose(data);
            }
        }, null);
        throw JsrWebSocketMessages.MESSAGES.connectionTimedOut();
    }
    WebSocketChannel channel;
    try {
        channel = session.get();
    } catch (UpgradeFailedException e) {
        throw new DeploymentException(e.getMessage(), e);
    }
    EndpointSessionHandler sessionHandler = new EndpointSessionHandler(this);
    final List<Extension> extensions = new ArrayList<>();
    final Map<String, Extension> extMap = new HashMap<>();
    for (Extension ext : cec.getExtensions()) {
        extMap.put(ext.getName(), ext);
    }
    for (WebSocketExtension e : clientNegotiation.getSelectedExtensions()) {
        Extension ext = extMap.get(e.getName());
        if (ext == null) {
            throw JsrWebSocketMessages.MESSAGES.extensionWasNotPresentInClientHandshake(e.getName(), clientNegotiation.getSupportedExtensions());
        }
        extensions.add(ExtensionImpl.create(e));
    }
    ConfiguredClientEndpoint configured = clientEndpoints.get(endpointInstance.getClass());
    if (configured == null) {
        synchronized (clientEndpoints) {
            configured = clientEndpoints.get(endpointInstance.getClass());
            if (configured == null) {
                clientEndpoints.put(endpointInstance.getClass(), configured = new ConfiguredClientEndpoint());
            }
        }
    }
    EncodingFactory encodingFactory = EncodingFactory.createFactory(classIntrospecter, cec.getDecoders(), cec.getEncoders());
    UndertowSession undertowSession = new UndertowSession(channel, connectionBuilder.getUri(), Collections.<String, String>emptyMap(), Collections.<String, List<String>>emptyMap(), sessionHandler, null, new ImmediateInstanceHandle<>(endpointInstance), cec, connectionBuilder.getUri().getQuery(), encodingFactory.createEncoding(cec), configured, clientNegotiation.getSelectedSubProtocol(), extensions, connectionBuilder);
    endpointInstance.onOpen(undertowSession, cec);
    channel.resumeReceives();
    return undertowSession;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) WebSocketExtension(io.undertow.websockets.WebSocketExtension) HashMap(java.util.HashMap) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) ArrayList(java.util.ArrayList) IoFuture(org.xnio.IoFuture) WebSocketExtension(io.undertow.websockets.WebSocketExtension) Extension(javax.websocket.Extension) WebSocketClientNegotiation(io.undertow.websockets.client.WebSocketClientNegotiation) UpgradeFailedException(org.xnio.http.UpgradeFailedException) DeploymentException(javax.websocket.DeploymentException) ClientEndpointConfig(javax.websocket.ClientEndpointConfig)

Aggregations

Extension (javax.websocket.Extension)22 ArrayList (java.util.ArrayList)16 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)6 URI (java.net.URI)5 WebSocketExtension (io.undertow.websockets.WebSocketExtension)4 DeploymentException (javax.websocket.DeploymentException)4 Endpoint (javax.websocket.Endpoint)4 Parameter (javax.websocket.Extension.Parameter)4 Session (javax.websocket.Session)4 IOException (java.io.IOException)3 InetSocketAddress (java.net.InetSocketAddress)3 HashMap (java.util.HashMap)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)3 JsrExtension (org.eclipse.jetty.websocket.jsr356.JsrExtension)3 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)2 ExtensionHandshake (io.undertow.websockets.extensions.ExtensionHandshake)2 HashSet (java.util.HashSet)2 ExecutionException (java.util.concurrent.ExecutionException)2