Search in sources :

Example 6 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 7 with ExtensionHandshake

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

the class WebSocket13ClientHandshake method createChannel.

@Override
public WebSocketChannel createChannel(final StreamConnection channel, final String wsUri, final ByteBufferPool bufferPool, OptionMap options) {
    if (negotiation != null && negotiation.getSelectedExtensions() != null && !negotiation.getSelectedExtensions().isEmpty()) {
        List<WebSocketExtension> selected = negotiation.getSelectedExtensions();
        List<ExtensionFunction> negotiated = new ArrayList<>();
        if (selected != null && !selected.isEmpty()) {
            for (WebSocketExtension ext : selected) {
                for (ExtensionHandshake extHandshake : extensions) {
                    if (ext.getName().equals(extHandshake.getName())) {
                        negotiated.add(extHandshake.create());
                    }
                }
            }
        }
        return new WebSocket13Channel(channel, bufferPool, wsUri, negotiation.getSelectedSubProtocol(), true, !negotiated.isEmpty(), CompositeExtensionFunction.compose(negotiated), new HashSet<WebSocketChannel>(), options);
    } else {
        return new WebSocket13Channel(channel, bufferPool, wsUri, negotiation != null ? negotiation.getSelectedSubProtocol() : "", true, false, NoopExtensionFunction.INSTANCE, new HashSet<WebSocketChannel>(), options);
    }
}
Also used : WebSocketExtension(io.undertow.websockets.WebSocketExtension) NoopExtensionFunction(io.undertow.websockets.extensions.NoopExtensionFunction) ExtensionFunction(io.undertow.websockets.extensions.ExtensionFunction) CompositeExtensionFunction(io.undertow.websockets.extensions.CompositeExtensionFunction) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) ArrayList(java.util.ArrayList) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake) WebSocket13Channel(io.undertow.websockets.core.protocol.version13.WebSocket13Channel)

Example 8 with ExtensionHandshake

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

the class JsrWebsocketExtensionTestCase method testExtensionsHeaders.

@Test
public void testExtensionsHeaders() throws Exception {
    final String SEC_WEBSOCKET_EXTENSIONS = "permessage-deflate; client_no_context_takeover; client_max_window_bits";
    // List format
    final String SEC_WEBSOCKET_EXTENSIONS_EXPECTED = "[permessage-deflate; client_no_context_takeover]";
    List<WebSocketExtension> extensions = WebSocketExtension.parse(SEC_WEBSOCKET_EXTENSIONS);
    final WebSocketClientNegotiation negotiation = new WebSocketClientNegotiation(null, extensions);
    Set<ExtensionHandshake> extensionHandshakes = new HashSet<>();
    extensionHandshakes.add(new PerMessageDeflateHandshake(true));
    final WebSocketChannel clientChannel = WebSocketClient.connect(DefaultServer.getWorker(), null, DefaultServer.getBufferPool(), OptionMap.EMPTY, new URI(DefaultServer.getDefaultServerURL()), WebSocketVersion.V13, negotiation, extensionHandshakes).get();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> result = new AtomicReference<>();
    clientChannel.getReceiveSetter().set(new AbstractReceiveListener() {

        @Override
        protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
            String data = message.getData();
            WebSocketLogger.ROOT_LOGGER.info("onFullTextMessage - Client - Received: " + data.getBytes().length + " bytes . Data: " + data);
            result.set(data);
            latch.countDown();
        }

        @Override
        protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
            message.getData().close();
            WebSocketLogger.ROOT_LOGGER.info("onFullCloseMessage");
        }

        @Override
        protected void onError(WebSocketChannel channel, Throwable error) {
            WebSocketLogger.ROOT_LOGGER.info("onError");
            super.onError(channel, error);
            error.printStackTrace();
            latch.countDown();
        }
    });
    clientChannel.resumeReceives();
    StreamSinkFrameChannel sendChannel = clientChannel.send(WebSocketFrameType.TEXT);
    new StringWriteChannelListener("Hello, World!").setup(sendChannel);
    latch.await(10, TimeUnit.SECONDS);
    Assert.assertEquals("Hello, World!", result.get());
    clientChannel.sendClose();
    Assert.assertEquals(SEC_WEBSOCKET_EXTENSIONS_EXPECTED, debug.getResponseExtensions().toString());
}
Also used : WebSocketExtension(io.undertow.websockets.WebSocketExtension) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) StreamSinkFrameChannel(io.undertow.websockets.core.StreamSinkFrameChannel) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) WebSocketClientNegotiation(io.undertow.websockets.client.WebSocketClientNegotiation) PerMessageDeflateHandshake(io.undertow.websockets.extensions.PerMessageDeflateHandshake) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake) BufferedBinaryMessage(io.undertow.websockets.core.BufferedBinaryMessage) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) HashSet(java.util.HashSet) Test(org.junit.Test) BinaryEndpointTest(io.undertow.websockets.jsr.test.BinaryEndpointTest)

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 IOException (java.io.IOException)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