Search in sources :

Example 1 with ExtensionFunction

use of io.undertow.websockets.extensions.ExtensionFunction 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)

Example 2 with ExtensionFunction

use of io.undertow.websockets.extensions.ExtensionFunction 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)

Aggregations

WebSocketExtension (io.undertow.websockets.WebSocketExtension)2 ExtensionFunction (io.undertow.websockets.extensions.ExtensionFunction)2 ExtensionHandshake (io.undertow.websockets.extensions.ExtensionHandshake)2 ArrayList (java.util.ArrayList)2 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)1 WebSocket13Channel (io.undertow.websockets.core.protocol.version13.WebSocket13Channel)1 CompositeExtensionFunction (io.undertow.websockets.extensions.CompositeExtensionFunction)1 NoopExtensionFunction (io.undertow.websockets.extensions.NoopExtensionFunction)1