Search in sources :

Example 26 with ClientEndpointConfig

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

the class ClientContainer method newClientEndpointInstance.

public EndpointInstance newClientEndpointInstance(Object endpoint, ClientEndpointConfig config) {
    EndpointMetadata metadata = getClientEndpointMetadata(endpoint.getClass(), config);
    ClientEndpointConfig cec = config;
    if (config == null) {
        if (metadata instanceof AnnotatedClientEndpointMetadata) {
            cec = ((AnnotatedClientEndpointMetadata) metadata).getConfig();
        } else {
            cec = new EmptyClientEndpointConfig();
        }
    }
    return new EndpointInstance(endpoint, cec, metadata);
}
Also used : EmptyClientEndpointConfig(org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig) EndpointInstance(org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance) EmptyClientEndpointConfig(org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) AnnotatedClientEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata) SimpleEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.SimpleEndpointMetadata) AnnotatedClientEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata) EndpointMetadata(org.eclipse.jetty.websocket.jsr356.metadata.EndpointMetadata)

Example 27 with ClientEndpointConfig

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

the class ClientContainer method connect.

private Session connect(EndpointInstance instance, URI path) throws IOException {
    Objects.requireNonNull(instance, "EndpointInstance cannot be null");
    Objects.requireNonNull(path, "Path cannot be null");
    ClientEndpointConfig config = (ClientEndpointConfig) instance.getConfig();
    ClientUpgradeRequest req = new ClientUpgradeRequest();
    UpgradeListener upgradeListener = null;
    for (Extension ext : config.getExtensions()) {
        req.addExtensions(new JsrExtensionConfig(ext));
    }
    if (config.getPreferredSubprotocols().size() > 0) {
        req.setSubProtocols(config.getPreferredSubprotocols());
    }
    if (config.getConfigurator() != null) {
        upgradeListener = new JsrUpgradeListener(config.getConfigurator());
    }
    Future<org.eclipse.jetty.websocket.api.Session> futSess = client.connect(instance, path, req, upgradeListener);
    try {
        return (JsrSession) futSess.get();
    } catch (InterruptedException e) {
        throw new IOException("Connect failure", e);
    } catch (ExecutionException e) {
        // Unwrap Actual Cause
        Throwable cause = e.getCause();
        if (cause instanceof IOException) {
            // Just rethrow
            throw (IOException) cause;
        } else {
            throw new IOException("Connect failure", cause);
        }
    }
}
Also used : IOException(java.io.IOException) Extension(javax.websocket.Extension) UpgradeListener(org.eclipse.jetty.websocket.client.io.UpgradeListener) EmptyClientEndpointConfig(org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) ExecutionException(java.util.concurrent.ExecutionException) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Session(javax.websocket.Session)

Example 28 with ClientEndpointConfig

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

the class AnnotatedEndpointConfigTest method testConfigurator.

@Test
public void testConfigurator() throws Exception {
    ClientEndpointConfig ceconfig = (ClientEndpointConfig) config;
    Assert.assertThat("Client Configurator", ceconfig.getConfigurator(), instanceOf(AnnotatedEndpointConfigurator.class));
}
Also used : ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Test(org.junit.Test)

Example 29 with ClientEndpointConfig

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

the class AnnotatedEndpointConfigTest method startEnv.

@BeforeClass
public static void startEnv() throws Exception {
    // Server
    server = new Server();
    ServerConnector connector = new ServerConnector(server);
    server.addConnector(connector);
    handler = new EchoHandler();
    ContextHandler context = new ContextHandler();
    context.setContextPath("/");
    context.setHandler(handler);
    server.setHandler(context);
    // Start Server
    server.start();
    String host = connector.getHost();
    if (host == null) {
        host = "localhost";
    }
    int port = connector.getLocalPort();
    serverUri = new URI(String.format("ws://%s:%d/", host, port));
    // Connect client
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    socket = new AnnotatedEndpointClient();
    session = container.connectToServer(socket, serverUri);
    Assert.assertThat("Session", session, notNullValue());
    config = socket.config;
    Assert.assertThat("EndpointConfig", config, notNullValue());
    Assert.assertThat("EndpointConfig", config, instanceOf(ClientEndpointConfig.class));
    ceconfig = (ClientEndpointConfig) config;
    Assert.assertThat("EndpointConfig", ceconfig, notNullValue());
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Server(org.eclipse.jetty.server.Server) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) URI(java.net.URI) BeforeClass(org.junit.BeforeClass)

Example 30 with ClientEndpointConfig

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

the class EncoderTest method testSingleQuotes.

@Test
public void testSingleQuotes() throws Exception {
    EchoServer eserver = new EchoServer(server);
    try {
        eserver.start();
        QuotesSocket quoter = new QuotesSocket();
        ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
        List<Class<? extends Encoder>> encoders = new ArrayList<>();
        encoders.add(QuotesEncoder.class);
        builder.encoders(encoders);
        ClientEndpointConfig cec = builder.build();
        client.connectToServer(quoter, cec, server.getWsUri());
        Quotes ben = getQuotes("quotes-ben.txt");
        quoter.write(ben);
        quoter.messageQueue.awaitEventCount(1, 1000, TimeUnit.MILLISECONDS);
        String result = quoter.messageQueue.poll();
        assertReceivedQuotes(result, ben);
    } finally {
        eserver.stop();
    }
}
Also used : Encoder(javax.websocket.Encoder) ArrayList(java.util.ArrayList) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

ClientEndpointConfig (javax.websocket.ClientEndpointConfig)44 Test (org.junit.Test)26 URI (java.net.URI)25 Session (javax.websocket.Session)19 Endpoint (javax.websocket.Endpoint)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 WebSocketContainer (javax.websocket.WebSocketContainer)12 ClientEndpoint (javax.websocket.ClientEndpoint)8 Context (org.apache.catalina.Context)7 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)7 Tomcat (org.apache.catalina.startup.Tomcat)7 ArrayList (java.util.ArrayList)6 EndpointConfig (javax.websocket.EndpointConfig)6 Extension (javax.websocket.Extension)6 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)6 AnnotatedClientEndpointMetadata (org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata)5 List (java.util.List)4 ServerEndpoint (javax.websocket.server.ServerEndpoint)4 AnnotatedEndpointScanner (org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner)4 EmptyClientEndpointConfig (org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig)4