Search in sources :

Example 1 with Parameter

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

the class PerMessageDeflate method negotiate.

static PerMessageDeflate negotiate(List<List<Parameter>> preferences, boolean isServer) {
    // Accept the first preference that the endpoint is able to support
    for (List<Parameter> preference : preferences) {
        boolean ok = true;
        boolean serverContextTakeover = true;
        int serverMaxWindowBits = -1;
        boolean clientContextTakeover = true;
        int clientMaxWindowBits = -1;
        for (Parameter param : preference) {
            if (SERVER_NO_CONTEXT_TAKEOVER.equals(param.getName())) {
                if (serverContextTakeover) {
                    serverContextTakeover = false;
                } else {
                    // Duplicate definition
                    throw new IllegalArgumentException(sm.getString("perMessageDeflate.duplicateParameter", SERVER_NO_CONTEXT_TAKEOVER));
                }
            } else if (CLIENT_NO_CONTEXT_TAKEOVER.equals(param.getName())) {
                if (clientContextTakeover) {
                    clientContextTakeover = false;
                } else {
                    // Duplicate definition
                    throw new IllegalArgumentException(sm.getString("perMessageDeflate.duplicateParameter", CLIENT_NO_CONTEXT_TAKEOVER));
                }
            } else if (SERVER_MAX_WINDOW_BITS.equals(param.getName())) {
                if (serverMaxWindowBits == -1) {
                    serverMaxWindowBits = Integer.parseInt(param.getValue());
                    if (serverMaxWindowBits < 8 || serverMaxWindowBits > 15) {
                        throw new IllegalArgumentException(sm.getString("perMessageDeflate.invalidWindowSize", SERVER_MAX_WINDOW_BITS, Integer.valueOf(serverMaxWindowBits)));
                    }
                    // to 15
                    if (isServer && serverMaxWindowBits != 15) {
                        ok = false;
                        break;
                    // Note server window size is not an issue for the
                    // client since the client will assume 15 and if the
                    // server uses a smaller window everything will
                    // still work
                    }
                } else {
                    // Duplicate definition
                    throw new IllegalArgumentException(sm.getString("perMessageDeflate.duplicateParameter", SERVER_MAX_WINDOW_BITS));
                }
            } else if (CLIENT_MAX_WINDOW_BITS.equals(param.getName())) {
                if (clientMaxWindowBits == -1) {
                    if (param.getValue() == null) {
                        // Hint to server that the client supports this
                        // option. Java SE API (as of Java 8) does not
                        // expose the API to control the Window size. It is
                        // effectively hard-coded to 15
                        clientMaxWindowBits = 15;
                    } else {
                        clientMaxWindowBits = Integer.parseInt(param.getValue());
                        if (clientMaxWindowBits < 8 || clientMaxWindowBits > 15) {
                            throw new IllegalArgumentException(sm.getString("perMessageDeflate.invalidWindowSize", CLIENT_MAX_WINDOW_BITS, Integer.valueOf(clientMaxWindowBits)));
                        }
                    }
                    // to 15
                    if (!isServer && clientMaxWindowBits != 15) {
                        ok = false;
                        break;
                    // Note client window size is not an issue for the
                    // server since the server will assume 15 and if the
                    // client uses a smaller window everything will
                    // still work
                    }
                } else {
                    // Duplicate definition
                    throw new IllegalArgumentException(sm.getString("perMessageDeflate.duplicateParameter", CLIENT_MAX_WINDOW_BITS));
                }
            } else {
                // Unknown parameter
                throw new IllegalArgumentException(sm.getString("perMessageDeflate.unknownParameter", param.getName()));
            }
        }
        if (ok) {
            return new PerMessageDeflate(serverContextTakeover, serverMaxWindowBits, clientContextTakeover, clientMaxWindowBits, isServer);
        }
    }
    // Failed to negotiate agreeable terms
    return null;
}
Also used : Parameter(javax.websocket.Extension.Parameter)

Example 2 with Parameter

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

the class TestUtil method doTestParseExtensionHeaderSimple.

private void doTestParseExtensionHeaderSimple(String header) {
    // Simple test
    List<Extension> result = new ArrayList<>();
    Util.parseExtensionHeader(result, header);
    Assert.assertEquals(1, result.size());
    Extension ext = result.get(0);
    Assert.assertEquals("ext", ext.getName());
    List<Parameter> params = ext.getParameters();
    Assert.assertEquals(2, params.size());
    Parameter paramA = params.get(0);
    Assert.assertEquals("a", paramA.getName());
    Assert.assertEquals("1", paramA.getValue());
    Parameter paramB = params.get(1);
    Assert.assertEquals("b", paramB.getName());
    Assert.assertEquals("2", paramB.getValue());
}
Also used : Extension(javax.websocket.Extension) ArrayList(java.util.ArrayList) Parameter(javax.websocket.Extension.Parameter)

Example 3 with Parameter

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

the class TestUtil method doTestParseExtensionHeaderMultiple.

private void doTestParseExtensionHeaderMultiple(String header) {
    // Simple test
    List<Extension> result = new ArrayList<>();
    Util.parseExtensionHeader(result, header);
    Assert.assertEquals(3, result.size());
    Extension ext = result.get(0);
    Assert.assertEquals("ext", ext.getName());
    List<Parameter> params = ext.getParameters();
    Assert.assertEquals(2, params.size());
    Parameter paramA = params.get(0);
    Assert.assertEquals("a", paramA.getName());
    Assert.assertEquals("1", paramA.getValue());
    Parameter paramB = params.get(1);
    Assert.assertEquals("b", paramB.getName());
    Assert.assertEquals("2", paramB.getValue());
    Extension ext2 = result.get(1);
    Assert.assertEquals("ext2", ext2.getName());
    List<Parameter> params2 = ext2.getParameters();
    Assert.assertEquals(2, params2.size());
    Parameter paramC = params2.get(0);
    Assert.assertEquals("c", paramC.getName());
    Assert.assertNull(paramC.getValue());
    Parameter paramD = params2.get(1);
    Assert.assertEquals("d", paramD.getName());
    Assert.assertEquals("xyz", paramD.getValue());
    Extension ext3 = result.get(2);
    Assert.assertEquals("ext3", ext3.getName());
    List<Parameter> params3 = ext3.getParameters();
    Assert.assertEquals(0, params3.size());
}
Also used : Extension(javax.websocket.Extension) ArrayList(java.util.ArrayList) Parameter(javax.websocket.Extension.Parameter)

Example 4 with Parameter

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

the class JsrCreator method createWebSocket.

@Override
public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
    JsrHandshakeRequest jsrHandshakeRequest = new JsrHandshakeRequest(req);
    JsrHandshakeResponse jsrHandshakeResponse = new JsrHandshakeResponse(resp);
    // Get raw config, as defined when the endpoint was added to the container
    ServerEndpointConfig config = metadata.getConfig();
    // Establish a copy of the config, so that the UserProperties are unique
    // per upgrade request.
    config = new BasicServerEndpointConfig(containerScope, config);
    // Bug 444617 - Expose localAddress and remoteAddress for jsr modify handshake to use
    // This is being implemented as an optional set of userProperties so that
    // it is not JSR api breaking.  A few users on #jetty and a few from cometd
    // have asked for access to this information.
    Map<String, Object> userProperties = config.getUserProperties();
    userProperties.put(PROP_LOCAL_ADDRESS, req.getLocalSocketAddress());
    userProperties.put(PROP_REMOTE_ADDRESS, req.getRemoteSocketAddress());
    userProperties.put(PROP_LOCALES, Collections.list(req.getLocales()));
    // Get Configurator from config object (not guaranteed to be unique per endpoint upgrade)
    ServerEndpointConfig.Configurator configurator = config.getConfigurator();
    // [JSR] Step 1: check origin
    if (!configurator.checkOrigin(req.getOrigin())) {
        try {
            resp.sendForbidden("Origin mismatch");
        } catch (IOException e) {
            if (LOG.isDebugEnabled())
                LOG.debug("Unable to send error response", e);
        }
        return null;
    }
    // [JSR] Step 2: deal with sub protocols
    List<String> supported = config.getSubprotocols();
    List<String> requested = req.getSubProtocols();
    String subprotocol = configurator.getNegotiatedSubprotocol(supported, requested);
    if (StringUtil.isNotBlank(subprotocol)) {
        resp.setAcceptedSubProtocol(subprotocol);
    }
    // [JSR] Step 3: deal with extensions
    List<Extension> installedExtensions = new ArrayList<>();
    for (String extName : extensionFactory.getAvailableExtensions().keySet()) {
        installedExtensions.add(new JsrExtension(extName));
    }
    List<Extension> requestedExts = new ArrayList<>();
    for (ExtensionConfig reqCfg : req.getExtensions()) {
        requestedExts.add(new JsrExtension(reqCfg));
    }
    List<Extension> usedExtensions = configurator.getNegotiatedExtensions(installedExtensions, requestedExts);
    List<ExtensionConfig> configs = new ArrayList<>();
    if (usedExtensions != null) {
        for (Extension used : usedExtensions) {
            ExtensionConfig ecfg = new ExtensionConfig(used.getName());
            for (Parameter param : used.getParameters()) {
                ecfg.setParameter(param.getName(), param.getValue());
            }
            configs.add(ecfg);
        }
    }
    resp.setExtensions(configs);
    // [JSR] Step 4: build out new ServerEndpointConfig
    PathSpec pathSpec = jsrHandshakeRequest.getRequestPathSpec();
    if (pathSpec instanceof UriTemplatePathSpec) {
        // We have a PathParam path spec
        UriTemplatePathSpec wspathSpec = (UriTemplatePathSpec) pathSpec;
        String requestPath = req.getRequestPath();
        // Wrap the config with the path spec information
        config = new PathParamServerEndpointConfig(containerScope, config, wspathSpec, requestPath);
    }
    // [JSR] Step 5: Call modifyHandshake
    configurator.modifyHandshake(config, jsrHandshakeRequest, jsrHandshakeResponse);
    try {
        // [JSR] Step 6: create endpoint class
        Class<?> endpointClass = config.getEndpointClass();
        Object endpoint = config.getConfigurator().getEndpointInstance(endpointClass);
        // This will allow CDI to see Session for injection into Endpoint classes.
        return new EndpointInstance(endpoint, config, metadata);
    } catch (InstantiationException e) {
        if (LOG.isDebugEnabled())
            LOG.debug("Unable to create websocket: " + config.getEndpointClass().getName(), e);
        return null;
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) ArrayList(java.util.ArrayList) EndpointInstance(org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance) IOException(java.io.IOException) UriTemplatePathSpec(org.eclipse.jetty.http.pathmap.UriTemplatePathSpec) PathSpec(org.eclipse.jetty.http.pathmap.PathSpec) JsrExtension(org.eclipse.jetty.websocket.jsr356.JsrExtension) Extension(javax.websocket.Extension) JsrExtension(org.eclipse.jetty.websocket.jsr356.JsrExtension) UriTemplatePathSpec(org.eclipse.jetty.http.pathmap.UriTemplatePathSpec) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) Parameter(javax.websocket.Extension.Parameter)

Aggregations

Parameter (javax.websocket.Extension.Parameter)4 ArrayList (java.util.ArrayList)3 Extension (javax.websocket.Extension)3 IOException (java.io.IOException)1 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)1 PathSpec (org.eclipse.jetty.http.pathmap.PathSpec)1 UriTemplatePathSpec (org.eclipse.jetty.http.pathmap.UriTemplatePathSpec)1 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)1 JsrExtension (org.eclipse.jetty.websocket.jsr356.JsrExtension)1 EndpointInstance (org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance)1